modified: .env
modified: bot.db modified: db/db.go modified: handlers/webhook.go modified: services/whatsapp.go
This commit is contained in:
@@ -1 +1,43 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
// SendWhatsAppMessage sends a text reply to a user
|
||||
func SendWhatsAppMessage(toPhone string, messageBody string) error {
|
||||
token := os.Getenv("WHATSAPP_TOKEN") // "EAA..."
|
||||
phoneID := os.Getenv("WHATSAPP_PHONE_ID") // "100..."
|
||||
version := "v17.0"
|
||||
url := fmt.Sprintf("https://graph.facebook.com/%s/%s/messages", version, phoneID)
|
||||
|
||||
payload := map[string]interface{}{
|
||||
"messaging_product": "whatsapp",
|
||||
"to": toPhone,
|
||||
"type": "text",
|
||||
"text": map[string]string{
|
||||
"body": messageBody,
|
||||
},
|
||||
}
|
||||
|
||||
jsonData, _ := json.Marshal(payload)
|
||||
req, _ := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode >= 400 {
|
||||
return fmt.Errorf("whatsapp api error: status %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user