sync: 0.7.0 from main (3f0d10f172101035f4e0f736695e1700ee1ec967)
This commit is contained in:
1 parent
182a719053
commit
fdf612350a
34 files changed
+2032
-430
No files matched your search
@@ -15,6 +15,7 @@ import (
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"golang.org/x/image/webp"
|
||||
@@ -141,6 +142,7 @@ type UserCollection struct {
|
||||
}
|
||||
|
||||
type ProxyConfig struct {
|
||||
Protocol string `json:"protocol,omitempty"`
|
||||
Host string `json:"host"`
|
||||
Password string `json:"password"`
|
||||
Port string `json:"port"`
|
||||
@@ -398,6 +400,7 @@ func (w whatsmeowService) StartClient(cd *ClientData) {
|
||||
return
|
||||
}
|
||||
|
||||
proxyProtocol := proxyConfig.Protocol
|
||||
proxyHost := proxyConfig.Host
|
||||
proxyPort := proxyConfig.Port
|
||||
proxyUsername := proxyConfig.Username
|
||||
@@ -411,6 +414,10 @@ func (w whatsmeowService) StartClient(cd *ClientData) {
|
||||
proxyPort = w.config.ProxyPort
|
||||
}
|
||||
|
||||
if proxyConfig.Protocol == "" {
|
||||
proxyProtocol = w.config.ProxyProtocol
|
||||
}
|
||||
|
||||
if proxyConfig.Username == "" {
|
||||
proxyUsername = w.config.ProxyUsername
|
||||
}
|
||||
@@ -419,12 +426,16 @@ func (w whatsmeowService) StartClient(cd *ClientData) {
|
||||
proxyPassword = w.config.ProxyPassword
|
||||
}
|
||||
|
||||
proxy, err := utils.CreateSocks5Proxy(proxyHost, proxyPort, proxyUsername, proxyPassword)
|
||||
proxyAddress, err := utils.BuildProxyAddress(proxyProtocol, proxyHost, proxyPort, proxyUsername, proxyPassword)
|
||||
if err != nil {
|
||||
w.loggerWrapper.GetLogger(cd.Instance.Id).LogWarn("[%s] Proxy error, continuing without proxy: %v", cd.Instance.Id, err)
|
||||
} else {
|
||||
client.SetSOCKSProxy(proxy)
|
||||
w.loggerWrapper.GetLogger(cd.Instance.Id).LogInfo("[%s] Proxy enabled", cd.Instance.Id)
|
||||
err = client.SetProxyAddress(proxyAddress)
|
||||
if err != nil {
|
||||
w.loggerWrapper.GetLogger(cd.Instance.Id).LogWarn("[%s] Proxy error, continuing without proxy: %v", cd.Instance.Id, err)
|
||||
} else {
|
||||
w.loggerWrapper.GetLogger(cd.Instance.Id).LogInfo("[%s] Proxy enabled (%s)", cd.Instance.Id, utils.NormalizeProxyProtocol(proxyProtocol, proxyPort))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1337,20 +1348,21 @@ func (mycli *MyClient) myEventHandler(rawEvt interface{}) {
|
||||
|
||||
if err == nil {
|
||||
webpReader := bytes.NewReader(data)
|
||||
img, err := webp.Decode(webpReader)
|
||||
if err != nil {
|
||||
mycli.loggerWrapper.GetLogger(mycli.userID).LogError("[%s] Failed to decode webp image: %v", mycli.userID, err)
|
||||
return
|
||||
img, decErr := webp.Decode(webpReader)
|
||||
if decErr != nil {
|
||||
mycli.loggerWrapper.GetLogger(mycli.userID).LogWarn("[%s] Failed to decode webp sticker, keeping raw webp: %v", mycli.userID, decErr)
|
||||
extension = ".webp"
|
||||
mimeType = "image/webp"
|
||||
} else {
|
||||
var pngBuffer bytes.Buffer
|
||||
if encErr := png.Encode(&pngBuffer, img); encErr != nil {
|
||||
mycli.loggerWrapper.GetLogger(mycli.userID).LogWarn("[%s] Failed to encode png from sticker, keeping raw webp: %v", mycli.userID, encErr)
|
||||
extension = ".webp"
|
||||
mimeType = "image/webp"
|
||||
} else {
|
||||
data = pngBuffer.Bytes()
|
||||
}
|
||||
}
|
||||
|
||||
var pngBuffer bytes.Buffer
|
||||
err = png.Encode(&pngBuffer, img)
|
||||
if err != nil {
|
||||
mycli.loggerWrapper.GetLogger(mycli.userID).LogError("[%s] Failed to encode png image: %v", mycli.userID, err)
|
||||
return
|
||||
}
|
||||
|
||||
data = pngBuffer.Bytes()
|
||||
}
|
||||
// Handle associated child media messages
|
||||
} else if associatedImg != nil {
|
||||
@@ -1379,21 +1391,24 @@ func (mycli *MyClient) myEventHandler(rawEvt interface{}) {
|
||||
mimeType = "image/png"
|
||||
mycli.loggerWrapper.GetLogger(mycli.userID).LogInfo("[%s] Processing associated child sticker message", mycli.userID)
|
||||
|
||||
webpReader := bytes.NewReader(data)
|
||||
img, err := webp.Decode(webpReader)
|
||||
if err != nil {
|
||||
mycli.loggerWrapper.GetLogger(mycli.userID).LogError("[%s] Failed to decode webp image: %v", mycli.userID, err)
|
||||
return
|
||||
if err == nil {
|
||||
webpReader := bytes.NewReader(data)
|
||||
img, decErr := webp.Decode(webpReader)
|
||||
if decErr != nil {
|
||||
mycli.loggerWrapper.GetLogger(mycli.userID).LogWarn("[%s] Failed to decode webp sticker, keeping raw webp: %v", mycli.userID, decErr)
|
||||
extension = ".webp"
|
||||
mimeType = "image/webp"
|
||||
} else {
|
||||
var pngBuffer bytes.Buffer
|
||||
if encErr := png.Encode(&pngBuffer, img); encErr != nil {
|
||||
mycli.loggerWrapper.GetLogger(mycli.userID).LogWarn("[%s] Failed to encode png from associated sticker, keeping raw webp: %v", mycli.userID, encErr)
|
||||
extension = ".webp"
|
||||
mimeType = "image/webp"
|
||||
} else {
|
||||
data = pngBuffer.Bytes()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var pngBuffer bytes.Buffer
|
||||
err = png.Encode(&pngBuffer, img)
|
||||
if err != nil {
|
||||
mycli.loggerWrapper.GetLogger(mycli.userID).LogError("[%s] Failed to encode png image: %v", mycli.userID, err)
|
||||
return
|
||||
}
|
||||
|
||||
data = pngBuffer.Bytes()
|
||||
}
|
||||
|
||||
downloadDuration := time.Since(downloadStart)
|
||||
@@ -1499,6 +1514,97 @@ func (mycli *MyClient) myEventHandler(rawEvt interface{}) {
|
||||
|
||||
postMap["data"] = dataMap
|
||||
|
||||
// ===== BUTTON CLICK EVENT DETECTION =====
|
||||
// Detecta cliques em botões e emite evento separado "ButtonClick"
|
||||
// Suporta 3 formatos: ButtonsResponseMessage, InteractiveResponseMessage (NativeFlow), TemplateButtonReplyMessage
|
||||
var buttonClickData map[string]interface{}
|
||||
|
||||
if resp := evt.Message.GetButtonsResponseMessage(); resp != nil {
|
||||
// Legacy buttons response
|
||||
buttonClickData = map[string]interface{}{
|
||||
"buttonId": resp.GetSelectedButtonID(),
|
||||
"buttonText": resp.GetSelectedDisplayText(),
|
||||
"type": "buttons_response",
|
||||
}
|
||||
mycli.loggerWrapper.GetLogger(mycli.userID).LogInfo("[%s] Button click detected (legacy): buttonId=%s, buttonText=%s", mycli.userID, resp.GetSelectedButtonID(), resp.GetSelectedDisplayText())
|
||||
} else if resp := evt.Message.GetInteractiveResponseMessage(); resp != nil {
|
||||
// NativeFlow interactive response (quick_reply, cta_url, cta_call, cta_copy)
|
||||
if nf := resp.GetNativeFlowResponseMessage(); nf != nil {
|
||||
buttonId := ""
|
||||
buttonText := ""
|
||||
// Parse paramsJSON to extract id and display_text
|
||||
if nf.GetParamsJSON() != "" {
|
||||
var params map[string]interface{}
|
||||
if err := json.Unmarshal([]byte(nf.GetParamsJSON()), ¶ms); err == nil {
|
||||
if id, ok := params["id"].(string); ok {
|
||||
buttonId = id
|
||||
}
|
||||
if dt, ok := params["display_text"].(string); ok {
|
||||
buttonText = dt
|
||||
}
|
||||
}
|
||||
}
|
||||
buttonClickData = map[string]interface{}{
|
||||
"buttonId": buttonId,
|
||||
"buttonText": buttonText,
|
||||
"type": "native_flow_response",
|
||||
"name": nf.GetName(),
|
||||
"paramsJSON": nf.GetParamsJSON(),
|
||||
}
|
||||
mycli.loggerWrapper.GetLogger(mycli.userID).LogInfo("[%s] Button click detected (native_flow): name=%s, buttonId=%s, buttonText=%s", mycli.userID, nf.GetName(), buttonId, buttonText)
|
||||
}
|
||||
} else if resp := evt.Message.GetTemplateButtonReplyMessage(); resp != nil {
|
||||
// Template button reply
|
||||
buttonClickData = map[string]interface{}{
|
||||
"buttonId": resp.GetSelectedID(),
|
||||
"buttonText": resp.GetSelectedDisplayText(),
|
||||
"type": "template_button_reply",
|
||||
}
|
||||
mycli.loggerWrapper.GetLogger(mycli.userID).LogInfo("[%s] Button click detected (template): buttonId=%s, buttonText=%s", mycli.userID, resp.GetSelectedID(), resp.GetSelectedDisplayText())
|
||||
} else if resp := evt.Message.GetListResponseMessage(); resp != nil {
|
||||
// List response (single select)
|
||||
buttonClickData = map[string]interface{}{
|
||||
"buttonId": resp.GetSingleSelectReply().GetSelectedRowID(),
|
||||
"buttonText": resp.GetTitle(),
|
||||
"type": "list_response",
|
||||
"description": resp.GetDescription(),
|
||||
}
|
||||
mycli.loggerWrapper.GetLogger(mycli.userID).LogInfo("[%s] List selection detected: rowId=%s, title=%s", mycli.userID, resp.GetSingleSelectReply().GetSelectedRowID(), resp.GetTitle())
|
||||
}
|
||||
|
||||
// Se detectou clique em botão, emite evento separado "ButtonClick"
|
||||
if buttonClickData != nil {
|
||||
buttonClickMap := map[string]interface{}{
|
||||
"event": "ButtonClick",
|
||||
"data": map[string]interface{}{
|
||||
"buttonId": buttonClickData["buttonId"],
|
||||
"buttonText": buttonClickData["buttonText"],
|
||||
"type": buttonClickData["type"],
|
||||
"phone": dataMap["Sender"],
|
||||
"jid": dataMap["Sender"],
|
||||
"pushName": dataMap["PushName"],
|
||||
"messageId": dataMap["ID"],
|
||||
"chat": dataMap["Chat"],
|
||||
"fromMe": dataMap["FromMe"],
|
||||
"timestamp": evt.Info.Timestamp.Unix(),
|
||||
"extraData": buttonClickData,
|
||||
},
|
||||
"instanceToken": mycli.token,
|
||||
"instanceId": mycli.userID,
|
||||
"instanceName": mycli.Instance.Name,
|
||||
}
|
||||
|
||||
buttonClickJSON, err := json.Marshal(buttonClickMap)
|
||||
if err == nil {
|
||||
buttonClickQueue := strings.ToLower(fmt.Sprintf("%s.buttonclick", userID))
|
||||
go mycli.service.CallWebhook(mycli.Instance, buttonClickQueue, buttonClickJSON)
|
||||
if mycli.config.AmqpGlobalEnabled || mycli.config.NatsGlobalEnabled {
|
||||
go mycli.service.SendToGlobalQueues("ButtonClick", buttonClickJSON, mycli.userID)
|
||||
}
|
||||
mycli.loggerWrapper.GetLogger(mycli.userID).LogInfo("[%s] ===== BUTTON CLICK EVENT DISPATCHED ===== Type: %s, ButtonId: %s", mycli.userID, buttonClickData["type"], buttonClickData["buttonId"])
|
||||
}
|
||||
}
|
||||
|
||||
mycli.loggerWrapper.GetLogger(mycli.userID).LogInfo("[%s] ===== MESSAGE PROCESSING COMPLETED ===== ID: %s, From: %s, Type: %s, Webhook: %v", mycli.userID, evt.Info.ID, evt.Info.Chat.String(), evt.Info.Type, doWebhook)
|
||||
case *events.Receipt:
|
||||
doWebhook = true
|
||||
@@ -1914,16 +2020,57 @@ func (w *whatsmeowService) CallWebhook(instance *instance_model.Instance, queueN
|
||||
if contains(subscriptions, "MESSAGE") {
|
||||
w.loggerWrapper.GetLogger(instance.Id).LogInfo("[%s] Event received of type %s", instance.Id, eventType)
|
||||
w.sendToQueueOrWebhook(instance, queueName, jsonData)
|
||||
} else {
|
||||
// Forward to GROUP/NEWSLETTER subscribers even without MESSAGE subscription
|
||||
if dataMap, ok := data["data"].(map[string]interface{}); ok {
|
||||
if infoMap, ok := dataMap["Info"].(map[string]interface{}); ok {
|
||||
if chat, ok := infoMap["Chat"].(string); ok {
|
||||
if strings.HasSuffix(chat, "@g.us") && contains(subscriptions, "GROUP") {
|
||||
w.loggerWrapper.GetLogger(instance.Id).LogInfo("[%s] Event received of type %s (Group)", instance.Id, eventType)
|
||||
w.sendToQueueOrWebhook(instance, queueName, jsonData)
|
||||
} else if strings.HasSuffix(chat, "@newsletter") && contains(subscriptions, "NEWSLETTER") {
|
||||
w.loggerWrapper.GetLogger(instance.Id).LogInfo("[%s] Event received of type %s (Newsletter)", instance.Id, eventType)
|
||||
w.sendToQueueOrWebhook(instance, queueName, jsonData)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
case "SendMessage":
|
||||
if contains(subscriptions, "SEND_MESSAGE") {
|
||||
w.loggerWrapper.GetLogger(instance.Id).LogInfo("[%s] Event received of type %s", instance.Id, eventType)
|
||||
w.sendToQueueOrWebhook(instance, queueName, jsonData)
|
||||
} else {
|
||||
if dataMap, ok := data["data"].(map[string]interface{}); ok {
|
||||
if infoMap, ok := dataMap["Info"].(map[string]interface{}); ok {
|
||||
if chat, ok := infoMap["Chat"].(string); ok {
|
||||
if strings.HasSuffix(chat, "@g.us") && contains(subscriptions, "GROUP") {
|
||||
w.loggerWrapper.GetLogger(instance.Id).LogInfo("[%s] Event received of type %s (Group)", instance.Id, eventType)
|
||||
w.sendToQueueOrWebhook(instance, queueName, jsonData)
|
||||
} else if strings.HasSuffix(chat, "@newsletter") && contains(subscriptions, "NEWSLETTER") {
|
||||
w.loggerWrapper.GetLogger(instance.Id).LogInfo("[%s] Event received of type %s (Newsletter)", instance.Id, eventType)
|
||||
w.sendToQueueOrWebhook(instance, queueName, jsonData)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
case "Receipt":
|
||||
if contains(subscriptions, "READ_RECEIPT") {
|
||||
w.loggerWrapper.GetLogger(instance.Id).LogInfo("[%s] Event received of type %s", instance.Id, eventType)
|
||||
w.sendToQueueOrWebhook(instance, queueName, jsonData)
|
||||
} else {
|
||||
if dataMap, ok := data["data"].(map[string]interface{}); ok {
|
||||
if chat, ok := dataMap["Chat"].(string); ok {
|
||||
if strings.HasSuffix(chat, "@g.us") && contains(subscriptions, "GROUP") {
|
||||
w.loggerWrapper.GetLogger(instance.Id).LogInfo("[%s] Event received of type %s (Group)", instance.Id, eventType)
|
||||
w.sendToQueueOrWebhook(instance, queueName, jsonData)
|
||||
} else if strings.HasSuffix(chat, "@newsletter") && contains(subscriptions, "NEWSLETTER") {
|
||||
w.loggerWrapper.GetLogger(instance.Id).LogInfo("[%s] Event received of type %s (Newsletter)", instance.Id, eventType)
|
||||
w.sendToQueueOrWebhook(instance, queueName, jsonData)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
case "Presence":
|
||||
if contains(subscriptions, "PRESENCE") {
|
||||
@@ -1975,6 +2122,11 @@ func (w *whatsmeowService) CallWebhook(instance *instance_model.Instance, queueN
|
||||
w.loggerWrapper.GetLogger(instance.Id).LogInfo("[%s] Event received of type %s", instance.Id, eventType)
|
||||
w.sendToQueueOrWebhook(instance, queueName, jsonData)
|
||||
}
|
||||
case "ButtonClick":
|
||||
if contains(subscriptions, "BUTTON_CLICK") || contains(subscriptions, "MESSAGE") {
|
||||
w.loggerWrapper.GetLogger(instance.Id).LogInfo("[%s] Event received of type %s", instance.Id, eventType)
|
||||
w.sendToQueueOrWebhook(instance, queueName, jsonData)
|
||||
}
|
||||
|
||||
default:
|
||||
return
|
||||
@@ -2035,7 +2187,21 @@ func (w whatsmeowService) StartInstance(instanceId string) error {
|
||||
}
|
||||
|
||||
if instance.Proxy == "" && w.config.ProxyHost != "" && w.config.ProxyPort != "" && w.config.ProxyUsername != "" && w.config.ProxyPassword != "" {
|
||||
instance.Proxy = fmt.Sprintf(`{"host": "%s", "port": "%s", "username": "%s", "password": "%s"}`, w.config.ProxyHost, w.config.ProxyPort, w.config.ProxyUsername, w.config.ProxyPassword)
|
||||
proxyConfig := ProxyConfig{
|
||||
Protocol: utils.NormalizeProxyProtocol(w.config.ProxyProtocol, w.config.ProxyPort),
|
||||
Host: w.config.ProxyHost,
|
||||
Port: w.config.ProxyPort,
|
||||
Username: w.config.ProxyUsername,
|
||||
Password: w.config.ProxyPassword,
|
||||
}
|
||||
|
||||
proxyJSON, err := json.Marshal(proxyConfig)
|
||||
if err != nil {
|
||||
w.loggerWrapper.GetLogger(instanceId).LogError("[%s] Failed to marshal proxy config: %v", instanceId, err)
|
||||
return err
|
||||
}
|
||||
|
||||
instance.Proxy = string(proxyJSON)
|
||||
|
||||
err = w.instanceRepository.UpdateProxy(instance.Id, instance.Proxy)
|
||||
if err != nil {
|
||||
@@ -2293,7 +2459,21 @@ func (w *whatsmeowService) SendToGlobalQueues(eventType string, payload []byte,
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
cachedWebVersion *clientVersion
|
||||
cachedWebVersionAt time.Time
|
||||
cachedWebVersionMu sync.Mutex
|
||||
webVersionCacheTTL = 1 * time.Hour
|
||||
)
|
||||
|
||||
func fetchWhatsAppWebVersion() (*clientVersion, error) {
|
||||
cachedWebVersionMu.Lock()
|
||||
defer cachedWebVersionMu.Unlock()
|
||||
|
||||
if cachedWebVersion != nil && time.Since(cachedWebVersionAt) < webVersionCacheTTL {
|
||||
return cachedWebVersion, nil
|
||||
}
|
||||
|
||||
resp, err := http.Get("https://web.whatsapp.com/sw.js")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to fetch WhatsApp Web version: %v", err)
|
||||
@@ -2327,11 +2507,13 @@ func fetchWhatsAppWebVersion() (*clientVersion, error) {
|
||||
|
||||
// Log qual padrão funcionou
|
||||
if clientRevision > 0 {
|
||||
return &clientVersion{
|
||||
cachedWebVersion = &clientVersion{
|
||||
Major: 2,
|
||||
Minor: 3000,
|
||||
Patch: clientRevision,
|
||||
}, nil
|
||||
}
|
||||
cachedWebVersionAt = time.Now()
|
||||
return cachedWebVersion, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user