add stickers support in API and client

This commit is contained in:
Martin Vasko
2023-04-05 14:37:27 +02:00
parent 5130282dc4
commit 19c2f85c03
2 changed files with 59 additions and 43 deletions

View File

@@ -6,6 +6,7 @@ import (
"errors" "errors"
"net/http" "net/http"
"strconv" "strconv"
"strings"
"time" "time"
"github.com/gabriel-vasile/mimetype" "github.com/gabriel-vasile/mimetype"
@@ -93,15 +94,16 @@ type SendMessageV1 struct {
} }
type SendMessageV2 struct { type SendMessageV2 struct {
Number string `json:"number"` Number string `json:"number"`
Recipients []string `json:"recipients"` Recipients []string `json:"recipients"`
Message string `json:"message"` Message string `json:"message"`
Base64Attachments []string `json:"base64_attachments" example:"<BASE64 ENCODED DATA>,data:<MIME-TYPE>;base64<comma><BASE64 ENCODED DATA>,data:<MIME-TYPE>;filename=<FILENAME>;base64<comma><BASE64 ENCODED DATA>"` Base64Attachments []string `json:"base64_attachments" example:"<BASE64 ENCODED DATA>,data:<MIME-TYPE>;base64<comma><BASE64 ENCODED DATA>,data:<MIME-TYPE>;filename=<FILENAME>;base64<comma><BASE64 ENCODED DATA>"`
Mentions []client.MessageMention `json:"mentions"` Sticker string `json:"sticker"`
QuoteTimestamp *int64 `json:"quote_timestamp"` Mentions []client.MessageMention `json:"mentions"`
QuoteAuthor *string `json:"quote_author"` QuoteTimestamp *int64 `json:"quote_timestamp"`
QuoteMessage *string `json:"quote_message"` QuoteAuthor *string `json:"quote_author"`
QuoteMentions []client.MessageMention `json:"quote_mentions"` QuoteMessage *string `json:"quote_message"`
QuoteMentions []client.MessageMention `json:"quote_mentions"`
} }
type TypingIndicatorRequest struct { type TypingIndicatorRequest struct {
@@ -359,7 +361,13 @@ func (a *Api) SendV2(c *gin.Context) {
return return
} }
timestamps, err := a.signalClient.SendV2(req.Number, req.Message, req.Recipients, req.Base64Attachments, if req.Sticker != "" && !strings.Contains(req.Sticker, ":") {
c.JSON(400, gin.H{"error": "Couldn't process request - please provide valid sticker delimiter"})
return
}
timestamps, err := a.signalClient.SendV2(
req.Number, req.Message, req.Recipients, req.Base64Attachments, req.Sticker,
req.Mentions, req.QuoteTimestamp, req.QuoteAuthor, req.QuoteMessage, req.QuoteMentions) req.Mentions, req.QuoteTimestamp, req.QuoteAuthor, req.QuoteMessage, req.QuoteMentions)
if err != nil { if err != nil {
c.JSON(400, Error{Msg: err.Error()}) c.JSON(400, Error{Msg: err.Error()})

View File

@@ -60,9 +60,9 @@ func (g GroupLinkState) String() string {
} }
type MessageMention struct { type MessageMention struct {
Start int64 `json:"start"` Start int64 `json:"start"`
Length int64 `json:"length"` Length int64 `json:"length"`
Author string `json:"author"` Author string `json:"author"`
} }
type GroupEntry struct { type GroupEntry struct {
@@ -122,11 +122,11 @@ type SendResponse struct {
} }
type About struct { type About struct {
SupportedApiVersions []string `json:"versions"` SupportedApiVersions []string `json:"versions"`
BuildNr int `json:"build"` BuildNr int `json:"build"`
Mode string `json:"mode"` Mode string `json:"mode"`
Version string `json:"version"` Version string `json:"version"`
Capabilities map[string][]string `json:"capabilities"` Capabilities map[string][]string `json:"capabilities"`
} }
type SearchResultEntry struct { type SearchResultEntry struct {
@@ -290,11 +290,11 @@ func (s *SignalClient) Init() error {
} }
func (s *MessageMention) toString() string { 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(number string, message string,
recipients []string, base64Attachments []string, isGroup bool, mentions []MessageMention, recipients []string, base64Attachments []string, isGroup bool, sticker string, mentions []MessageMention,
quoteTimestamp *int64, quoteAuthor *string, quoteMessage *string, quoteMentions []MessageMention) (*SendResponse, error) { quoteTimestamp *int64, quoteAuthor *string, quoteMessage *string, quoteMentions []MessageMention) (*SendResponse, error) {
var resp SendResponse var resp SendResponse
@@ -336,15 +336,16 @@ func (s *SignalClient) send(number string, message string,
} }
type Request struct { type Request struct {
Recipients []string `json:"recipient,omitempty"` Recipients []string `json:"recipient,omitempty"`
Message string `json:"message"` Message string `json:"message"`
GroupId string `json:"group-id,omitempty"` GroupId string `json:"group-id,omitempty"`
Attachments []string `json:"attachment,omitempty"` Attachments []string `json:"attachment,omitempty"`
Mentions []string `json:"mentions,omitempty"` Sticker string `json:"sticker,omitempty"`
QuoteTimestamp *int64 `json:"quote-timestamp,omitempty"` Mentions []string `json:"mentions,omitempty"`
QuoteAuthor *string `json:"quote-author,omitempty"` QuoteTimestamp *int64 `json:"quote-timestamp,omitempty"`
QuoteMessage *string `json:"quote-message,omitempty"` QuoteAuthor *string `json:"quote-author,omitempty"`
QuoteMentions []string `json:"quote-mentions,omitempty"` QuoteMessage *string `json:"quote-message,omitempty"`
QuoteMentions []string `json:"quote-mentions,omitempty"`
} }
request := Request{Message: message} request := Request{Message: message}
@@ -356,24 +357,26 @@ func (s *SignalClient) send(number string, message string,
for _, attachmentEntry := range attachmentEntries { for _, attachmentEntry := range attachmentEntries {
request.Attachments = append(request.Attachments, attachmentEntry.toDataForSignal()) request.Attachments = append(request.Attachments, attachmentEntry.toDataForSignal())
} }
request.Sticker = sticker
if mentions != nil { if mentions != nil {
request.Mentions = make([]string, len(mentions)) request.Mentions = make([]string, len(mentions))
for i, mention := range mentions { for i, mention := range mentions {
request.Mentions[i] = mention.toString() request.Mentions[i] = mention.toString()
} }
} else { } else {
request.Mentions = nil request.Mentions = nil
} }
request.QuoteTimestamp = quoteTimestamp request.QuoteTimestamp = quoteTimestamp
request.QuoteAuthor = quoteAuthor request.QuoteAuthor = quoteAuthor
request.QuoteMessage = quoteMessage request.QuoteMessage = quoteMessage
if quoteMentions != nil { if quoteMentions != nil {
request.QuoteMentions = make([]string, len(quoteMentions)) request.QuoteMentions = make([]string, len(quoteMentions))
for i, mention := range quoteMentions { for i, mention := range quoteMentions {
request.QuoteMentions[i] = mention.toString() request.QuoteMentions[i] = mention.toString()
} }
} else { } else {
request.QuoteMentions = nil request.QuoteMentions = nil
} }
rawData, err := jsonRpc2Client.getRaw("send", request) rawData, err := jsonRpc2Client.getRaw("send", request)
@@ -409,6 +412,11 @@ func (s *SignalClient) send(number string, message string,
cmd = append(cmd, mention.toString()) cmd = append(cmd, mention.toString())
} }
if sticker != "" {
cmd = append(cmd, "--sticker")
cmd = append(cmd, sticker)
}
if quoteTimestamp != nil { if quoteTimestamp != nil {
cmd = append(cmd, "--quote-timestamp") cmd = append(cmd, "--quote-timestamp")
cmd = append(cmd, strconv.FormatInt(*quoteTimestamp, 10)) cmd = append(cmd, strconv.FormatInt(*quoteTimestamp, 10))
@@ -519,7 +527,7 @@ 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) timestamp, err := s.send(number, message, recipients, base64Attachments, isGroup, "", nil, nil, nil, nil, nil)
return timestamp, err return timestamp, err
} }
@@ -538,7 +546,7 @@ func (s *SignalClient) getJsonRpc2Clients() []*JsonRpc2Client {
return jsonRpc2Clients return jsonRpc2Clients
} }
func (s *SignalClient) SendV2(number string, message string, recps []string, base64Attachments []string, mentions []MessageMention, func (s *SignalClient) SendV2(number string, message string, recps []string, base64Attachments []string, sticker string, mentions []MessageMention,
quoteTimestamp *int64, quoteAuthor *string, quoteMessage *string, quoteMentions []MessageMention) (*[]SendResponse, error) { quoteTimestamp *int64, quoteAuthor *string, quoteMessage *string, quoteMentions []MessageMention) (*[]SendResponse, error) {
if len(recps) == 0 { if len(recps) == 0 {
return nil, errors.New("Please provide at least one recipient") return nil, errors.New("Please provide at least one recipient")
@@ -569,7 +577,7 @@ 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, mentions, quoteTimestamp, quoteAuthor, quoteMessage, quoteMentions) timestamp, err := s.send(number, message, []string{group}, base64Attachments, true, sticker, mentions, quoteTimestamp, quoteAuthor, quoteMessage, quoteMentions)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -577,7 +585,7 @@ 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, mentions, quoteTimestamp, quoteAuthor, quoteMessage, quoteMentions) timestamp, err := s.send(number, message, recipients, base64Attachments, false, sticker, mentions, quoteTimestamp, quoteAuthor, quoteMessage, quoteMentions)
if err != nil { if err != nil {
return nil, err return nil, err
} }