mirror of
https://github.com/bbernhard/signal-cli-rest-api.git
synced 2026-05-21 13:54:18 +00:00
code improvements
* restructured the code a bit to make it more readable
This commit is contained in:
parent
193a9f1e5b
commit
6522dcf8c8
@ -190,6 +190,22 @@ type ListInstalledStickerPacksResponse struct {
|
|||||||
Author string `json:"author"`
|
Author string `json:"author"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type SignalCliSendRequest struct {
|
||||||
|
Number string
|
||||||
|
Message string
|
||||||
|
Recipients []string
|
||||||
|
Base64Attachments []string
|
||||||
|
IsGroup bool
|
||||||
|
Sticker string
|
||||||
|
Mentions []MessageMention
|
||||||
|
QuoteTimestamp *int64
|
||||||
|
QuoteAuthor *string
|
||||||
|
QuoteMessage *string
|
||||||
|
QuoteMentions []MessageMention
|
||||||
|
TextMode *string
|
||||||
|
EditTimestamp *int64
|
||||||
|
}
|
||||||
|
|
||||||
func cleanupTmpFiles(paths []string) {
|
func cleanupTmpFiles(paths []string) {
|
||||||
for _, path := range paths {
|
for _, path := range paths {
|
||||||
os.Remove(path)
|
os.Remove(path)
|
||||||
@ -349,29 +365,25 @@ func (s *MessageMention) toString() string {
|
|||||||
return fmt.Sprintf("%d:%d:%s", s.Start, s.Length, s.Author)
|
return fmt.Sprintf("%d:%d:%s", s.Start, s.Length, s.Author)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SignalClient) send(number string, message string,
|
func(s *SignalClient) send(signalCliSendRequest SignalCliSendRequest) (*SendResponse, error) {
|
||||||
recipients []string, base64Attachments []string, isGroup bool, sticker string, mentions []MessageMention,
|
|
||||||
quoteTimestamp *int64, quoteAuthor *string, quoteMessage *string, quoteMentions []MessageMention, textMode *string,
|
|
||||||
editTimestamp *int64) (*SendResponse, error) {
|
|
||||||
|
|
||||||
var resp SendResponse
|
var resp SendResponse
|
||||||
|
|
||||||
if len(recipients) == 0 {
|
if len(signalCliSendRequest.Recipients) == 0 {
|
||||||
return nil, errors.New("Please specify at least one recipient")
|
return nil, errors.New("Please specify at least one recipient")
|
||||||
}
|
}
|
||||||
|
|
||||||
signalCliTextFormatStrings := []string{}
|
signalCliTextFormatStrings := []string{}
|
||||||
if textMode != nil && *textMode == "styled" {
|
if signalCliSendRequest.TextMode != nil && *signalCliSendRequest.TextMode == "styled" {
|
||||||
message, signalCliTextFormatStrings = utils.ParseMarkdownMessage(message)
|
signalCliSendRequest.Message, signalCliTextFormatStrings = utils.ParseMarkdownMessage(signalCliSendRequest.Message)
|
||||||
}
|
}
|
||||||
|
|
||||||
var groupId string = ""
|
var groupId string = ""
|
||||||
if isGroup {
|
if signalCliSendRequest.IsGroup {
|
||||||
if len(recipients) > 1 {
|
if len(signalCliSendRequest.Recipients) > 1 {
|
||||||
return nil, errors.New("More than one recipient is currently not allowed")
|
return nil, errors.New("More than one recipient is currently not allowed")
|
||||||
}
|
}
|
||||||
|
|
||||||
grpId, err := base64.StdEncoding.DecodeString(recipients[0])
|
grpId, err := base64.StdEncoding.DecodeString(signalCliSendRequest.Recipients[0])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.New("Invalid group id")
|
return nil, errors.New("Invalid group id")
|
||||||
}
|
}
|
||||||
@ -379,7 +391,7 @@ func (s *SignalClient) send(number string, message string,
|
|||||||
}
|
}
|
||||||
|
|
||||||
attachmentEntries := []AttachmentEntry{}
|
attachmentEntries := []AttachmentEntry{}
|
||||||
for _, base64Attachment := range base64Attachments {
|
for _, base64Attachment := range signalCliSendRequest.Base64Attachments {
|
||||||
attachmentEntry := NewAttachmentEntry(base64Attachment, s.attachmentTmpDir)
|
attachmentEntry := NewAttachmentEntry(base64Attachment, s.attachmentTmpDir)
|
||||||
|
|
||||||
err := attachmentEntry.storeBase64AsTemporaryFile()
|
err := attachmentEntry.storeBase64AsTemporaryFile()
|
||||||
@ -413,11 +425,11 @@ func (s *SignalClient) send(number string, message string,
|
|||||||
NotifySelf bool `json:"notify-self,omitempty"`
|
NotifySelf bool `json:"notify-self,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
request := Request{Message: message}
|
request := Request{Message: signalCliSendRequest.Message}
|
||||||
if isGroup {
|
if signalCliSendRequest.IsGroup {
|
||||||
request.GroupId = groupId
|
request.GroupId = groupId
|
||||||
} else {
|
} else {
|
||||||
request.Recipients = recipients
|
request.Recipients = signalCliSendRequest.Recipients
|
||||||
}
|
}
|
||||||
for _, attachmentEntry := range attachmentEntries {
|
for _, attachmentEntry := range attachmentEntries {
|
||||||
request.Attachments = append(request.Attachments, attachmentEntry.toDataForSignal())
|
request.Attachments = append(request.Attachments, attachmentEntry.toDataForSignal())
|
||||||
@ -425,33 +437,33 @@ func (s *SignalClient) send(number string, message string,
|
|||||||
|
|
||||||
request.NotifySelf = true
|
request.NotifySelf = true
|
||||||
|
|
||||||
request.Sticker = sticker
|
request.Sticker = signalCliSendRequest.Sticker
|
||||||
if mentions != nil {
|
if signalCliSendRequest.Mentions != nil {
|
||||||
request.Mentions = make([]string, len(mentions))
|
request.Mentions = make([]string, len(signalCliSendRequest.Mentions))
|
||||||
for i, mention := range mentions {
|
for i, mention := range signalCliSendRequest.Mentions {
|
||||||
request.Mentions[i] = mention.toString()
|
request.Mentions[i] = mention.toString()
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
request.Mentions = nil
|
request.Mentions = nil
|
||||||
}
|
}
|
||||||
request.QuoteTimestamp = quoteTimestamp
|
request.QuoteTimestamp = signalCliSendRequest.QuoteTimestamp
|
||||||
request.QuoteAuthor = quoteAuthor
|
request.QuoteAuthor = signalCliSendRequest.QuoteAuthor
|
||||||
request.QuoteMessage = quoteMessage
|
request.QuoteMessage = signalCliSendRequest.QuoteMessage
|
||||||
if quoteMentions != nil {
|
if signalCliSendRequest.QuoteMentions != nil {
|
||||||
request.QuoteMentions = make([]string, len(quoteMentions))
|
request.QuoteMentions = make([]string, len(signalCliSendRequest.QuoteMentions))
|
||||||
for i, mention := range quoteMentions {
|
for i, mention := range signalCliSendRequest.QuoteMentions {
|
||||||
request.QuoteMentions[i] = mention.toString()
|
request.QuoteMentions[i] = mention.toString()
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
request.QuoteMentions = nil
|
request.QuoteMentions = nil
|
||||||
}
|
}
|
||||||
request.EditTimestamp = editTimestamp
|
request.EditTimestamp = signalCliSendRequest.EditTimestamp
|
||||||
|
|
||||||
if len(signalCliTextFormatStrings) > 0 {
|
if len(signalCliTextFormatStrings) > 0 {
|
||||||
request.TextStyles = signalCliTextFormatStrings
|
request.TextStyles = signalCliTextFormatStrings
|
||||||
}
|
}
|
||||||
|
|
||||||
rawData, err := jsonRpc2Client.getRaw("send", &number, request)
|
rawData, err := jsonRpc2Client.getRaw("send", &signalCliSendRequest.Number, request)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cleanupAttachmentEntries(attachmentEntries)
|
cleanupAttachmentEntries(attachmentEntries)
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -467,9 +479,9 @@ func (s *SignalClient) send(number string, message string,
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
cmd := []string{"--config", s.signalCliConfig, "-a", number, "send", "--message-from-stdin"}
|
cmd := []string{"--config", s.signalCliConfig, "-a", signalCliSendRequest.Number, "send", "--message-from-stdin"}
|
||||||
if !isGroup {
|
if !signalCliSendRequest.IsGroup {
|
||||||
cmd = append(cmd, recipients...)
|
cmd = append(cmd, signalCliSendRequest.Recipients...)
|
||||||
} else {
|
} else {
|
||||||
cmd = append(cmd, []string{"-g", groupId}...)
|
cmd = append(cmd, []string{"-g", groupId}...)
|
||||||
}
|
}
|
||||||
@ -486,44 +498,44 @@ func (s *SignalClient) send(number string, message string,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, mention := range mentions {
|
for _, mention := range signalCliSendRequest.Mentions {
|
||||||
cmd = append(cmd, "--mention")
|
cmd = append(cmd, "--mention")
|
||||||
cmd = append(cmd, mention.toString())
|
cmd = append(cmd, mention.toString())
|
||||||
}
|
}
|
||||||
|
|
||||||
if sticker != "" {
|
if signalCliSendRequest.Sticker != "" {
|
||||||
cmd = append(cmd, "--sticker")
|
cmd = append(cmd, "--sticker")
|
||||||
cmd = append(cmd, sticker)
|
cmd = append(cmd, signalCliSendRequest.Sticker)
|
||||||
}
|
}
|
||||||
|
|
||||||
if quoteTimestamp != nil {
|
if signalCliSendRequest.QuoteTimestamp != nil {
|
||||||
cmd = append(cmd, "--quote-timestamp")
|
cmd = append(cmd, "--quote-timestamp")
|
||||||
cmd = append(cmd, strconv.FormatInt(*quoteTimestamp, 10))
|
cmd = append(cmd, strconv.FormatInt(*signalCliSendRequest.QuoteTimestamp, 10))
|
||||||
}
|
}
|
||||||
|
|
||||||
if quoteAuthor != nil {
|
if signalCliSendRequest.QuoteAuthor != nil {
|
||||||
cmd = append(cmd, "--quote-author")
|
cmd = append(cmd, "--quote-author")
|
||||||
cmd = append(cmd, *quoteAuthor)
|
cmd = append(cmd, *signalCliSendRequest.QuoteAuthor)
|
||||||
}
|
}
|
||||||
|
|
||||||
if quoteMessage != nil {
|
if signalCliSendRequest.QuoteMessage != nil {
|
||||||
cmd = append(cmd, "--quote-message")
|
cmd = append(cmd, "--quote-message")
|
||||||
cmd = append(cmd, *quoteMessage)
|
cmd = append(cmd, *signalCliSendRequest.QuoteMessage)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, mention := range quoteMentions {
|
for _, mention := range signalCliSendRequest.QuoteMentions {
|
||||||
cmd = append(cmd, "--quote-mention")
|
cmd = append(cmd, "--quote-mention")
|
||||||
cmd = append(cmd, mention.toString())
|
cmd = append(cmd, mention.toString())
|
||||||
}
|
}
|
||||||
|
|
||||||
if editTimestamp != nil {
|
if signalCliSendRequest.EditTimestamp != nil {
|
||||||
cmd = append(cmd, "--edit-timestamp")
|
cmd = append(cmd, "--edit-timestamp")
|
||||||
cmd = append(cmd, strconv.FormatInt(*editTimestamp, 10))
|
cmd = append(cmd, strconv.FormatInt(*signalCliSendRequest.EditTimestamp, 10))
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd = append(cmd, "--notify-self")
|
cmd = append(cmd, "--notify-self")
|
||||||
|
|
||||||
rawData, err := s.cliClient.Execute(true, cmd, message)
|
rawData, err := s.cliClient.Execute(true, cmd, signalCliSendRequest.Message)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cleanupAttachmentEntries(attachmentEntries)
|
cleanupAttachmentEntries(attachmentEntries)
|
||||||
if strings.Contains(err.Error(), signalCliV2GroupError) {
|
if strings.Contains(err.Error(), signalCliV2GroupError) {
|
||||||
@ -650,7 +662,10 @@ func (s *SignalClient) VerifyRegisteredNumber(number string, token string, pin s
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *SignalClient) SendV1(number string, message string, recipients []string, base64Attachments []string, isGroup bool) (*SendResponse, error) {
|
func (s *SignalClient) SendV1(number string, message string, recipients []string, base64Attachments []string, isGroup bool) (*SendResponse, error) {
|
||||||
timestamp, err := s.send(number, message, recipients, base64Attachments, isGroup, "", nil, nil, nil, nil, nil, nil, nil)
|
signalCliSendRequest := SignalCliSendRequest{Number: number, Message: message, Recipients: recipients, Base64Attachments: base64Attachments,
|
||||||
|
IsGroup: isGroup, Sticker: "", Mentions: nil, QuoteTimestamp: nil, QuoteAuthor: nil, QuoteMessage: nil,
|
||||||
|
QuoteMentions: nil, TextMode: nil, EditTimestamp: nil}
|
||||||
|
timestamp, err := s.send(signalCliSendRequest)
|
||||||
return timestamp, err
|
return timestamp, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -700,8 +715,11 @@ func (s *SignalClient) SendV2(number string, message string, recps []string, bas
|
|||||||
|
|
||||||
timestamps := []SendResponse{}
|
timestamps := []SendResponse{}
|
||||||
for _, group := range groups {
|
for _, group := range groups {
|
||||||
timestamp, err := s.send(number, message, []string{group}, base64Attachments, true, sticker,
|
signalCliSendRequest := SignalCliSendRequest{Number: number, Message: message, Recipients: []string{group}, Base64Attachments: base64Attachments,
|
||||||
mentions, quoteTimestamp, quoteAuthor, quoteMessage, quoteMentions, textMode, editTimestamp)
|
IsGroup: true, Sticker: sticker, Mentions: mentions, QuoteTimestamp: quoteTimestamp,
|
||||||
|
QuoteAuthor: quoteAuthor, QuoteMessage: quoteMessage, QuoteMentions: quoteMentions,
|
||||||
|
TextMode: textMode, EditTimestamp: editTimestamp}
|
||||||
|
timestamp, err := s.send(signalCliSendRequest)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -709,8 +727,11 @@ func (s *SignalClient) SendV2(number string, message string, recps []string, bas
|
|||||||
}
|
}
|
||||||
|
|
||||||
if len(recipients) > 0 {
|
if len(recipients) > 0 {
|
||||||
timestamp, err := s.send(number, message, recipients, base64Attachments, false, sticker, mentions,
|
signalCliSendRequest := SignalCliSendRequest{Number: number, Message: message, Recipients: recipients, Base64Attachments: base64Attachments,
|
||||||
quoteTimestamp, quoteAuthor, quoteMessage, quoteMentions, textMode, editTimestamp)
|
IsGroup: false, Sticker: sticker, Mentions: mentions, QuoteTimestamp: quoteTimestamp,
|
||||||
|
QuoteAuthor: quoteAuthor, QuoteMessage: quoteMessage, QuoteMentions: quoteMentions,
|
||||||
|
TextMode: textMode, EditTimestamp: editTimestamp}
|
||||||
|
timestamp, err := s.send(signalCliSendRequest)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user