feat: webhook option added for incoming messages
This commit is contained in:
-6
@@ -1,6 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="DataSourcePerFileMappings">
|
||||
<file url="file://$APPLICATION_CONFIG_DIR$/consoles/db/4f6200ec-1eae-4549-9d93-2a328888b8d7/console.sql" value="4f6200ec-1eae-4549-9d93-2a328888b8d7" />
|
||||
</component>
|
||||
</project>
|
||||
Generated
-6
@@ -1,6 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="SqlDialectMappings">
|
||||
<file url="file://$PROJECT_DIR$/main.go" dialect="GenericSQL" />
|
||||
</component>
|
||||
</project>
|
||||
@@ -14,9 +14,10 @@ type dbConfig struct {
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
DB dbConfig
|
||||
JWTSecret []byte
|
||||
APIKey string
|
||||
DB dbConfig
|
||||
JWTSecret []byte
|
||||
APIKey string
|
||||
WebhookUrl string
|
||||
}
|
||||
|
||||
func LoadConfig() (*Config, error) {
|
||||
@@ -45,6 +46,7 @@ func LoadConfig() (*Config, error) {
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("missing API_KEY")
|
||||
}
|
||||
webhookUrl := os.Getenv("WEBHOOK_URL")
|
||||
|
||||
isPostgres := os.Getenv("IS_POSTGRES") == "true"
|
||||
|
||||
@@ -58,5 +60,6 @@ func LoadConfig() (*Config, error) {
|
||||
},
|
||||
JWTSecret: []byte(jwtSecret),
|
||||
APIKey: apiKey,
|
||||
WebhookUrl: webhookUrl,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ services:
|
||||
POSTGRES_PORT: "5432"
|
||||
API_KEY: "c3VwZXItbG9uZy1yYW5kb20tc3RyaW5nLW1pbmltdW0tb2YtNjQtY2hhcmFjdGVycy15b3UtbmVlZC10by1wYXN0ZS1oZXJl"
|
||||
JWT_SECRET: "YW5vdGhlci1zdXBlci1sb25nLXJhbmRvbS1zdHJpbmctbWluaW11bS1vZi02NC1jaGFyYWN0ZXJzLXlvdS1uZWVkLXRvLXBhc3RlLWhlcmU="
|
||||
WEBHOOK_URL: "http://192.168.178.52:5678/webhook-test/whatsapp"
|
||||
ports:
|
||||
- "8080:8080"
|
||||
volumes:
|
||||
|
||||
@@ -555,6 +555,54 @@ func extractMediaInfo(msg *waE2E.Message) (mediaType string, filename string, ur
|
||||
|
||||
// Handle regular incoming messages with media support
|
||||
func handleMessage(client *whatsmeow.Client, messageStore *MessageStore, msg *events.Message, logger waLog.Logger) {
|
||||
go func() {
|
||||
cfg, err := config.LoadConfig()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
log.Println("Recovered in webhook goroutine:", r)
|
||||
}
|
||||
}()
|
||||
if cfg.WebhookUrl == "" {
|
||||
return
|
||||
}
|
||||
|
||||
content := extractTextContent(msg.Message)
|
||||
if content == "" {
|
||||
return
|
||||
}
|
||||
|
||||
payload := map[string]interface{}{
|
||||
"chat_jid": msg.Info.Chat.String(),
|
||||
"sender": msg.Info.Sender.User,
|
||||
"content": extractTextContent(msg.Message),
|
||||
"is_from_me": msg.Info.IsFromMe,
|
||||
"timestamp": msg.Info.Timestamp.String(),
|
||||
"push_name": msg.Info.PushName,
|
||||
"is_group": strings.Contains(msg.Info.Chat.String(), "@g.us"),
|
||||
"message_id": msg.Info.ID,
|
||||
}
|
||||
|
||||
jsonData, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
log.Println("Webhook marshal error:", err)
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := http.Post(
|
||||
cfg.WebhookUrl,
|
||||
"application/json",
|
||||
bytes.NewBuffer(jsonData),
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
log.Println("Webhook POST error:", err)
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
}()
|
||||
chatJID := msg.Info.Chat.String()
|
||||
sender := msg.Info.Sender.User
|
||||
|
||||
|
||||
Reference in New Issue
Block a user