sync: 0.7.0 from main (3f0d10f172101035f4e0f736695e1700ee1ec967)

This commit is contained in:
github-actions[bot] committed 2026-04-17 18:41:28 +00:00
1 parent 182a719053
commit fdf612350a
34 files changed
+2032 -430

No files matched your search

+437 -137
View File
@@ -8,6 +8,7 @@ import (
"errors"
"fmt"
"image"
"image/jpeg"
"image/png"
"io"
"mime/multipart"
@@ -43,6 +44,10 @@ type SendService interface {
SendContact(data *ContactStruct, instance *instance_model.Instance) (*MessageSendStruct, error)
SendButton(data *ButtonStruct, instance *instance_model.Instance) (*MessageSendStruct, error)
SendList(data *ListStruct, instance *instance_model.Instance) (*MessageSendStruct, error)
SendCarousel(data *CarouselStruct, instance *instance_model.Instance) (*MessageSendStruct, error)
SendStatusText(data *StatusTextStruct, instance *instance_model.Instance) (*MessageSendStruct, error)
SendStatusMediaUrl(data *StatusMediaStruct, instance *instance_model.Instance) (*MessageSendStruct, error)
SendStatusMediaFile(data *StatusMediaStruct, fileData []byte, instance *instance_model.Instance) (*MessageSendStruct, error)
}
type sendService struct {
@@ -243,6 +248,18 @@ type CarouselStruct struct {
Cards []CarouselCardStruct `json:"cards"`
}
type StatusTextStruct struct {
Text string `json:"text"`
Id string `json:"id"`
}
type StatusMediaStruct struct {
Type string `json:"type"`
Url string `json:"url"`
Caption string `json:"caption"`
Id string `json:"id"`
}
type MessageSendStruct struct {
Info types.MessageInfo
Message *waE2E.Message
@@ -1666,24 +1683,38 @@ func (s *sendService) SendButton(data *ButtonStruct, instance *instance_model.In
return t
}()
msg = &waE2E.Message{ViewOnceMessage: &waE2E.FutureProofMessage{
Message: &waE2E.Message{
InteractiveMessage: &waE2E.InteractiveMessage{
Body: &waE2E.InteractiveMessage_Body{
Text: &body,
},
Footer: &waE2E.InteractiveMessage_Footer{
Text: &data.Footer,
},
InteractiveMessage: &waE2E.InteractiveMessage_NativeFlowMessage_{
NativeFlowMessage: &waE2E.InteractiveMessage_NativeFlowMessage{
Buttons: buttons,
MessageParamsJSON: &messageParamsJSON,
},
},
interactiveMsg := &waE2E.InteractiveMessage{
Body: &waE2E.InteractiveMessage_Body{
Text: &body,
},
InteractiveMessage: &waE2E.InteractiveMessage_NativeFlowMessage_{
NativeFlowMessage: &waE2E.InteractiveMessage_NativeFlowMessage{
Buttons: buttons,
MessageParamsJSON: &messageParamsJSON,
MessageVersion: proto.Int32(1),
},
},
}}
ContextInfo: &waE2E.ContextInfo{},
}
// Footer conditional - only add if not empty (iOS compatibility)
if data.Footer != "" {
interactiveMsg.Footer = &waE2E.InteractiveMessage_Footer{
Text: &data.Footer,
}
}
// Header with title
if data.Title != "" {
interactiveMsg.Header = &waE2E.InteractiveMessage_Header{
Title: proto.String(data.Title),
HasMediaAttachment: proto.Bool(false),
}
}
msg = &waE2E.Message{
InteractiveMessage: interactiveMsg,
}
}
recipient, err := s.validateAndCheckUserExists(data.Number, data.FormatJid, &data.Quoted.MessageID, &data.Quoted.MessageID, instance)
@@ -1743,15 +1774,16 @@ func stringPointer(s string) *string {
func sectionsToString(data *ListStruct) (string, error) {
type row struct {
Header *string `json:"header,omitempty"`
Title string `json:"title"`
Description *string `json:"description,omitempty"`
ID string `json:"id"`
Header string `json:"header"`
Title string `json:"title"`
Description string `json:"description"`
ID string `json:"id"`
}
type listSection struct {
Title string `json:"title"`
Rows []row `json:"rows"`
Title string `json:"title"`
HighlightLabel string `json:"highlight_label"`
Rows []row `json:"rows"`
}
type list struct {
@@ -1762,26 +1794,49 @@ func sectionsToString(data *ListStruct) (string, error) {
sections := []listSection{}
for _, s := range data.Sections {
sectionTitle := s.Title
if sectionTitle == "" {
sectionTitle = " "
}
rows := []row{}
for _, r := range s.Rows {
rowTitle := r.Title
if rowTitle == "" {
rowTitle = " "
}
rowDesc := r.Description
if rowDesc == "" {
rowDesc = " "
}
rowId := r.RowId
if rowId == "" {
rowId = fmt.Sprintf("row_%d", len(rows))
}
rows = append(rows, row{
Title: r.Title,
Description: stringPointer(r.Description),
ID: r.RowId,
Header: rowTitle,
Title: rowTitle,
Description: rowDesc,
ID: rowId,
})
}
section := listSection{
Title: s.Title,
Rows: rows,
Title: sectionTitle,
HighlightLabel: "",
Rows: rows,
}
sections = append(sections, section)
}
buttonText := data.ButtonText
if buttonText == "" {
buttonText = "Ver Menu"
}
listData := list{
Title: data.ButtonText,
Title: buttonText,
Sections: sections,
}
@@ -1794,105 +1849,70 @@ func sectionsToString(data *ListStruct) (string, error) {
}
func (s *sendService) SendList(data *ListStruct, instance *instance_model.Instance) (*MessageSendStruct, error) {
client, err := s.ensureClientConnected(instance.Id)
if err != nil {
return nil, err
// Legacy ListMessage format - works on iOS, Android and Web
// Matching PAPI Node.js default (non-modern) path exactly
buttonText := data.ButtonText
if buttonText == "" {
buttonText = "Ver Menu"
}
messageId := client.GenerateMessageID()
templateId := strconv.FormatInt(time.Now().UnixNano()/1000000, 10)
messageParamsJSON := `{"from":"api","templateId":` + templateId + `}`
buttons := []*waE2E.InteractiveMessage_NativeFlowMessage_NativeFlowButton{}
sectionString, err := sectionsToString(data)
if err != nil {
return nil, err
// Build sections in legacy ListMessage format
var sections []*waE2E.ListMessage_Section
for _, sec := range data.Sections {
sectionTitle := sec.Title
if sectionTitle == "" {
sectionTitle = " "
}
var rows []*waE2E.ListMessage_Row
for i, r := range sec.Rows {
rowTitle := r.Title
if rowTitle == "" {
rowTitle = " "
}
rowId := r.RowId
if rowId == "" {
rowId = fmt.Sprintf("row_%d_%d", i, len(rows))
}
rows = append(rows, &waE2E.ListMessage_Row{
Title: proto.String(rowTitle),
Description: proto.String(r.Description),
RowID: proto.String(rowId),
})
}
sections = append(sections, &waE2E.ListMessage_Section{
Title: proto.String(sectionTitle),
Rows: rows,
})
}
buttons = append(buttons, &waE2E.InteractiveMessage_NativeFlowMessage_NativeFlowButton{
Name: proto.String("single_select"),
ButtonParamsJSON: proto.String(sectionString),
listType := waE2E.ListMessage_SINGLE_SELECT
listMessage := &waE2E.ListMessage{
Title: proto.String(data.Title),
Description: proto.String(data.Description),
ButtonText: proto.String(buttonText),
FooterText: proto.String(data.FooterText),
ListType: &listType,
Sections: sections,
}
// Send as plain ListMessage (NO ViewOnceMessage wrapper) - matching PAPI Node.js
msg := &waE2E.Message{
ListMessage: listMessage,
}
message, err := s.SendMessage(instance, msg, "ListMessage", &SendDataStruct{
Number: data.Number,
Delay: data.Delay,
})
body := func() string {
t := "*" + data.Title + "*"
if data.Description != "" {
t += "\n\n" + data.Description + "\n"
}
return t
}()
msg := &waE2E.Message{ViewOnceMessage: &waE2E.FutureProofMessage{
Message: &waE2E.Message{
InteractiveMessage: &waE2E.InteractiveMessage{
Body: &waE2E.InteractiveMessage_Body{
Text: &body,
},
Footer: &waE2E.InteractiveMessage_Footer{
Text: &data.FooterText,
},
InteractiveMessage: &waE2E.InteractiveMessage_NativeFlowMessage_{
NativeFlowMessage: &waE2E.InteractiveMessage_NativeFlowMessage{
Buttons: buttons,
MessageParamsJSON: &messageParamsJSON,
},
},
},
},
}}
recipient, err := s.validateAndCheckUserExists(data.Number, data.FormatJid, &data.Quoted.MessageID, &data.Quoted.MessageID, instance)
if err != nil {
s.loggerWrapper.GetLogger(instance.Id).LogError("[%s] Error validating message fields or user check: %v", instance.Id, err)
s.loggerWrapper.GetLogger(instance.Id).LogError("[%s] Error sending list: %v", instance.Id, err)
return nil, err
}
if data.Delay > 0 {
err := client.SendChatPresence(context.Background(), recipient, types.ChatPresence("composing"), types.ChatPresenceMedia(""))
if err != nil {
return nil, err
}
time.Sleep(time.Duration(data.Delay) * time.Millisecond)
err = s.clientPointer[instance.Id].SendChatPresence(context.Background(), recipient, types.ChatPresence("paused"), types.ChatPresenceMedia(""))
if err != nil {
return nil, err
}
}
response, err := s.clientPointer[instance.Id].SendMessage(context.Background(), recipient, msg, whatsmeow.SendRequestExtra{ID: messageId})
if err != nil {
return nil, err
}
messageInfo := types.MessageInfo{
MessageSource: types.MessageSource{
Chat: recipient,
Sender: *s.clientPointer[instance.Id].Store.ID,
IsFromMe: true,
IsGroup: false,
},
ID: messageId,
Timestamp: time.Now(),
ServerID: response.ServerID,
Type: "ListMessage",
}
messageSent := &MessageSendStruct{
Info: messageInfo,
Message: msg,
MessageContextInfo: &waE2E.ContextInfo{
StanzaID: proto.String(data.Quoted.MessageID),
Participant: proto.String(data.Quoted.Participant),
QuotedMessage: &waE2E.Message{Conversation: proto.String("")},
},
}
return messageSent, nil
s.loggerWrapper.GetLogger(instance.Id).LogInfo("[%s] List sent to %s", instance.Id, data.Number)
return message, nil
}
func (s *sendService) SendMessage(instance *instance_model.Instance, msg *waE2E.Message, messageType string, data *SendDataStruct) (*MessageSendStruct, error) {
@@ -2010,6 +2030,22 @@ func (s *sendService) SendMessage(instance *instance_model.Instance, msg *waE2E.
Participant: proto.String(data.Quoted.Participant),
QuotedMessage: &waE2E.Message{Conversation: proto.String("")},
}
case "InteractiveMessage":
if msg.InteractiveMessage != nil {
msg.InteractiveMessage.ContextInfo = &waE2E.ContextInfo{
StanzaID: proto.String(data.Quoted.MessageID),
Participant: proto.String(data.Quoted.Participant),
QuotedMessage: &waE2E.Message{Conversation: proto.String("")},
}
}
case "ListMessage":
if msg.ListMessage != nil {
msg.ListMessage.ContextInfo = &waE2E.ContextInfo{
StanzaID: proto.String(data.Quoted.MessageID),
Participant: proto.String(data.Quoted.Participant),
QuotedMessage: &waE2E.Message{Conversation: proto.String("")},
}
}
default:
return nil, fmt.Errorf("invalid messageType: %s", messageType)
}
@@ -2044,6 +2080,10 @@ func (s *sendService) SendMessage(instance *instance_model.Instance, msg *waE2E.
msg.LocationMessage.ContextInfo = &waE2E.ContextInfo{}
case "ContactMessage":
msg.ContactMessage.ContextInfo = &waE2E.ContextInfo{}
case "InteractiveMessage":
// ContextInfo already set in SendCarousel/SendButton/SendList
case "ListMessage":
// ContextInfo already set in SendList
default:
return nil, fmt.Errorf("invalid messageType: %s", messageType)
}
@@ -2362,9 +2402,8 @@ func (s *sendService) SendCarousel(data *CarouselStruct, instance *instance_mode
if card.Header.ImageUrl != "" || card.Header.VideoUrl != "" {
header := interactiveCard.Header
// Add media to header if URL provided
if card.Header.ImageUrl != "" {
// Download and upload image
// Download image
resp, err := http.Get(card.Header.ImageUrl)
if err == nil {
defer resp.Body.Close()
@@ -2372,16 +2411,42 @@ func (s *sendService) SendCarousel(data *CarouselStruct, instance *instance_mode
if err == nil {
uploaded, err := client.Upload(context.Background(), fileData, whatsmeow.MediaImage)
if err == nil {
// Generate JPEG thumbnail for iOS compatibility
var jpegThumb []byte
img, _, decErr := image.Decode(bytes.NewReader(fileData))
if decErr == nil {
// Resize to 72px thumbnail
bounds := img.Bounds()
thumbWidth := 72
thumbHeight := int(float64(bounds.Dy()) * float64(thumbWidth) / float64(bounds.Dx()))
if thumbHeight < 1 {
thumbHeight = 1
}
thumbImg := image.NewRGBA(image.Rect(0, 0, thumbWidth, thumbHeight))
for y := 0; y < thumbHeight; y++ {
for x := 0; x < thumbWidth; x++ {
srcX := x * bounds.Dx() / thumbWidth
srcY := y * bounds.Dy() / thumbHeight
thumbImg.Set(x, y, img.At(srcX+bounds.Min.X, srcY+bounds.Min.Y))
}
}
var thumbBuf bytes.Buffer
if jpeg.Encode(&thumbBuf, thumbImg, &jpeg.Options{Quality: 50}) == nil {
jpegThumb = thumbBuf.Bytes()
}
}
header.HasMediaAttachment = proto.Bool(true)
header.Media = &waE2E.InteractiveMessage_Header_ImageMessage{
ImageMessage: &waE2E.ImageMessage{
URL: proto.String(uploaded.URL),
DirectPath: proto.String(uploaded.DirectPath),
MediaKey: uploaded.MediaKey,
Mimetype: proto.String("image/jpeg"),
FileEncSHA256: uploaded.FileEncSHA256,
FileSHA256: uploaded.FileSHA256,
FileLength: proto.Uint64(uint64(len(fileData))),
URL: proto.String(uploaded.URL),
DirectPath: proto.String(uploaded.DirectPath),
MediaKey: uploaded.MediaKey,
Mimetype: proto.String("image/jpeg"),
FileEncSHA256: uploaded.FileEncSHA256,
FileSHA256: uploaded.FileSHA256,
FileLength: proto.Uint64(uint64(len(fileData))),
JPEGThumbnail: jpegThumb,
},
}
}
@@ -2460,14 +2525,11 @@ func (s *sendService) SendCarousel(data *CarouselStruct, instance *instance_mode
}
}
// messageParamsJSON is REQUIRED for NativeFlowMessage with buttons
messageParamsJSON := fmt.Sprintf(`{"from":"api","templateId":"%d"}`, time.Now().UnixNano()/1000000)
// Cards in carousel: do NOT set MessageParamsJSON or MessageVersion
// (matching PAPI Node.js behavior for iOS compatibility)
interactiveCard.InteractiveMessage = &waE2E.InteractiveMessage_NativeFlowMessage_{
NativeFlowMessage: &waE2E.InteractiveMessage_NativeFlowMessage{
Buttons: buttons,
MessageParamsJSON: proto.String(messageParamsJSON),
MessageVersion: proto.Int32(1),
Buttons: buttons,
},
}
}
@@ -2475,14 +2537,12 @@ func (s *sendService) SendCarousel(data *CarouselStruct, instance *instance_mode
cards[i] = interactiveCard
}
// Build carousel message
carouselCardType := waE2E.InteractiveMessage_CarouselMessage_HSCROLL_CARDS
// Build carousel message (do NOT set CarouselCardType - matching PAPI Node.js for iOS)
interactiveMsg := &waE2E.InteractiveMessage{
InteractiveMessage: &waE2E.InteractiveMessage_CarouselMessage_{
CarouselMessage: &waE2E.InteractiveMessage_CarouselMessage{
Cards: cards,
MessageVersion: &messageVersion,
CarouselCardType: &carouselCardType,
Cards: cards,
MessageVersion: &messageVersion,
},
},
}
@@ -2541,6 +2601,246 @@ func (s *sendService) SendCarousel(data *CarouselStruct, instance *instance_mode
return message, nil
}
func (s *sendService) SendStatusText(data *StatusTextStruct, instance *instance_model.Instance) (*MessageSendStruct, error) {
client, err := s.ensureClientConnected(instance.Id)
if err != nil {
return nil, err
}
if data.Text == "" {
return nil, errors.New("text is required")
}
msg := &waE2E.Message{
ExtendedTextMessage: &waE2E.ExtendedTextMessage{
Text: &data.Text,
},
}
messageID := data.Id
if messageID == "" {
messageID = client.GenerateMessageID()
}
recipient := types.NewJID("status", "broadcast")
response, err := client.SendMessage(context.Background(), recipient, msg, whatsmeow.SendRequestExtra{ID: messageID})
if err != nil {
return nil, err
}
messageInfo := types.MessageInfo{
MessageSource: types.MessageSource{
Chat: recipient,
Sender: *client.Store.ID,
IsFromMe: true,
IsGroup: false,
},
ID: messageID,
Timestamp: time.Now(),
ServerID: response.ServerID,
Type: "StatusTextMessage",
}
messageSent := &MessageSendStruct{
Info: messageInfo,
Message: msg,
MessageContextInfo: &waE2E.ContextInfo{
StanzaID: proto.String(""),
Participant: proto.String(""),
QuotedMessage: &waE2E.Message{Conversation: proto.String("")},
},
}
s.sendStatusWebhook(messageSent, instance, "text")
s.loggerWrapper.GetLogger(instance.Id).LogInfo("[%s] Status text sent successfully", instance.Id)
return messageSent, nil
}
func (s *sendService) SendStatusMediaUrl(data *StatusMediaStruct, instance *instance_model.Instance) (*MessageSendStruct, error) {
client, err := s.ensureClientConnected(instance.Id)
if err != nil {
return nil, err
}
if data.Url == "" {
return nil, errors.New("url is required")
}
if data.Type != "image" && data.Type != "video" {
return nil, errors.New("type must be 'image' or 'video'")
}
req, err := http.NewRequest("GET", data.Url, nil)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", "Evolution-GO/1.0")
httpClient := &http.Client{}
resp, err := httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to download file from URL: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode > 299 {
return nil, fmt.Errorf("failed to download file: HTTP status %d", resp.StatusCode)
}
fileData, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return s.sendStatusMedia(client, data, fileData, instance)
}
func (s *sendService) SendStatusMediaFile(data *StatusMediaStruct, fileData []byte, instance *instance_model.Instance) (*MessageSendStruct, error) {
client, err := s.ensureClientConnected(instance.Id)
if err != nil {
return nil, err
}
if data.Type != "image" && data.Type != "video" {
return nil, errors.New("type must be 'image' or 'video'")
}
return s.sendStatusMedia(client, data, fileData, instance)
}
func (s *sendService) sendStatusMedia(client *whatsmeow.Client, data *StatusMediaStruct, fileData []byte, instance *instance_model.Instance) (*MessageSendStruct, error) {
mime, _ := mimetype.DetectReader(bytes.NewReader(fileData))
mimeType := mime.String()
var uploadType whatsmeow.MediaType
switch data.Type {
case "image":
if mimeType != "image/jpeg" && mimeType != "image/png" && mimeType != "image/webp" {
return nil, fmt.Errorf("invalid file format: '%s'. Only 'image/jpeg', 'image/png' and 'image/webp' are accepted", mimeType)
}
if mimeType == "image/webp" {
mimeType = "image/jpeg"
}
uploadType = whatsmeow.MediaImage
case "video":
if mimeType != "video/mp4" {
return nil, fmt.Errorf("invalid file format: '%s'. Only 'video/mp4' is accepted", mimeType)
}
uploadType = whatsmeow.MediaVideo
default:
return nil, errors.New("invalid media type")
}
uploaded, err := client.Upload(context.Background(), fileData, uploadType)
if err != nil {
return nil, err
}
s.loggerWrapper.GetLogger(instance.Id).LogInfo("[%s] Status media uploaded, size: %d", instance.Id, uploaded.FileLength)
var media *waE2E.Message
var mediaType string
switch data.Type {
case "image":
media = &waE2E.Message{ImageMessage: &waE2E.ImageMessage{
Caption: proto.String(data.Caption),
URL: proto.String(uploaded.URL),
DirectPath: proto.String(uploaded.DirectPath),
MediaKey: uploaded.MediaKey,
Mimetype: proto.String(mimeType),
FileEncSHA256: uploaded.FileEncSHA256,
FileSHA256: uploaded.FileSHA256,
FileLength: proto.Uint64(uint64(len(fileData))),
}}
mediaType = "ImageMessage"
case "video":
media = &waE2E.Message{VideoMessage: &waE2E.VideoMessage{
Caption: proto.String(data.Caption),
URL: proto.String(uploaded.URL),
DirectPath: proto.String(uploaded.DirectPath),
MediaKey: uploaded.MediaKey,
Mimetype: proto.String(mimeType),
FileEncSHA256: uploaded.FileEncSHA256,
FileSHA256: uploaded.FileSHA256,
FileLength: proto.Uint64(uint64(len(fileData))),
}}
mediaType = "VideoMessage"
}
messageID := data.Id
if messageID == "" {
messageID = client.GenerateMessageID()
}
recipient := types.NewJID("status", "broadcast")
response, err := client.SendMessage(context.Background(), recipient, media, whatsmeow.SendRequestExtra{ID: messageID})
if err != nil {
return nil, err
}
messageInfo := types.MessageInfo{
MessageSource: types.MessageSource{
Chat: recipient,
Sender: *client.Store.ID,
IsFromMe: true,
IsGroup: false,
},
ID: messageID,
Timestamp: time.Now(),
ServerID: response.ServerID,
Type: mediaType,
}
messageSent := &MessageSendStruct{
Info: messageInfo,
Message: media,
MessageContextInfo: &waE2E.ContextInfo{
StanzaID: proto.String(""),
Participant: proto.String(""),
QuotedMessage: &waE2E.Message{Conversation: proto.String("")},
},
}
s.sendStatusWebhook(messageSent, instance, "media")
return messageSent, nil
}
func (s *sendService) sendStatusWebhook(messageSent *MessageSendStruct, instance *instance_model.Instance, messageType string) {
postMap := make(map[string]interface{})
postMap["event"] = "SendStatus"
messageData := make(map[string]interface{})
messageData["Info"] = messageSent.Info
msgBytes, err := json.Marshal(messageSent.Message)
if err != nil {
s.loggerWrapper.GetLogger(instance.Id).LogError("[%s] Failed to marshal status message: %v", instance.Id, err)
return
}
var msgMap map[string]interface{}
if err := json.Unmarshal(msgBytes, &msgMap); err != nil {
s.loggerWrapper.GetLogger(instance.Id).LogError("[%s] Failed to unmarshal status message: %v", instance.Id, err)
return
}
messageData["Message"] = msgMap
messageData["MessageContextInfo"] = messageSent.MessageContextInfo
postMap["data"] = messageData
postMap["instanceToken"] = instance.Token
postMap["instanceId"] = instance.Id
postMap["instanceName"] = instance.Name
values, err := json.Marshal(postMap)
if err != nil {
s.loggerWrapper.GetLogger(instance.Id).LogError("[%s] Failed to marshal webhook payload: %v", instance.Id, err)
return
}
go s.whatsmeowService.CallWebhook(instance, "sendstatus", values)
if s.config.AmqpGlobalEnabled || s.config.NatsGlobalEnabled {
go s.whatsmeowService.SendToGlobalQueues("SendStatus", values, instance.Id)
}
s.loggerWrapper.GetLogger(instance.Id).LogInfo("[%s] Status %s sent successfully", instance.Id, messageType)
}
func NewSendService(
clientPointer map[string]*whatsmeow.Client,
whatsmeowService whatsmeow_service.WhatsmeowService,