Whatsapp Business support
This commit is contained in:
354
internal/whatsapp/businessapi/client.go
Normal file
354
internal/whatsapp/businessapi/client.go
Normal file
@@ -0,0 +1,354 @@
|
||||
package businessapi
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"git.warky.dev/wdevs/whatshooked/internal/config"
|
||||
"git.warky.dev/wdevs/whatshooked/internal/events"
|
||||
"git.warky.dev/wdevs/whatshooked/internal/logging"
|
||||
|
||||
"go.mau.fi/whatsmeow/types"
|
||||
)
|
||||
|
||||
// Client represents a WhatsApp Business API client
|
||||
type Client struct {
|
||||
id string
|
||||
phoneNumber string
|
||||
config config.BusinessAPIConfig
|
||||
httpClient *http.Client
|
||||
eventBus *events.EventBus
|
||||
mediaConfig config.MediaConfig
|
||||
connected bool
|
||||
mu sync.RWMutex
|
||||
}
|
||||
|
||||
// NewClient creates a new Business API client
|
||||
func NewClient(cfg config.WhatsAppConfig, eventBus *events.EventBus, mediaConfig config.MediaConfig) (*Client, error) {
|
||||
if cfg.Type != "business-api" {
|
||||
return nil, fmt.Errorf("invalid client type for business-api: %s", cfg.Type)
|
||||
}
|
||||
|
||||
if cfg.BusinessAPI == nil {
|
||||
return nil, fmt.Errorf("business_api configuration is required for business-api type")
|
||||
}
|
||||
|
||||
// Validate required fields
|
||||
if cfg.BusinessAPI.PhoneNumberID == "" {
|
||||
return nil, fmt.Errorf("phone_number_id is required")
|
||||
}
|
||||
if cfg.BusinessAPI.AccessToken == "" {
|
||||
return nil, fmt.Errorf("access_token is required")
|
||||
}
|
||||
|
||||
// Set default API version
|
||||
if cfg.BusinessAPI.APIVersion == "" {
|
||||
cfg.BusinessAPI.APIVersion = "v21.0"
|
||||
}
|
||||
|
||||
return &Client{
|
||||
id: cfg.ID,
|
||||
phoneNumber: cfg.PhoneNumber,
|
||||
config: *cfg.BusinessAPI,
|
||||
httpClient: &http.Client{
|
||||
Timeout: 30 * time.Second,
|
||||
},
|
||||
eventBus: eventBus,
|
||||
mediaConfig: mediaConfig,
|
||||
connected: false,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Connect validates the Business API credentials
|
||||
func (c *Client) Connect(ctx context.Context) error {
|
||||
// Validate credentials by making a test request to get phone number details
|
||||
url := fmt.Sprintf("https://graph.facebook.com/%s/%s",
|
||||
c.config.APIVersion,
|
||||
c.config.PhoneNumberID)
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create request: %w", err)
|
||||
}
|
||||
|
||||
req.Header.Set("Authorization", "Bearer "+c.config.AccessToken)
|
||||
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
c.eventBus.Publish(events.WhatsAppPairFailedEvent(ctx, c.id, err))
|
||||
return fmt.Errorf("failed to validate credentials: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
err := fmt.Errorf("API returned status %d: %s", resp.StatusCode, string(body))
|
||||
c.eventBus.Publish(events.WhatsAppPairFailedEvent(ctx, c.id, err))
|
||||
return err
|
||||
}
|
||||
|
||||
c.mu.Lock()
|
||||
c.connected = true
|
||||
c.mu.Unlock()
|
||||
|
||||
logging.Info("Business API client connected", "account_id", c.id, "phone", c.phoneNumber)
|
||||
c.eventBus.Publish(events.WhatsAppConnectedEvent(ctx, c.id, c.phoneNumber))
|
||||
return nil
|
||||
}
|
||||
|
||||
// Disconnect closes the Business API client
|
||||
func (c *Client) Disconnect() error {
|
||||
c.mu.Lock()
|
||||
c.connected = false
|
||||
c.mu.Unlock()
|
||||
|
||||
logging.Info("Business API client disconnected", "account_id", c.id)
|
||||
return nil
|
||||
}
|
||||
|
||||
// IsConnected returns whether the client is connected
|
||||
func (c *Client) IsConnected() bool {
|
||||
c.mu.RLock()
|
||||
defer c.mu.RUnlock()
|
||||
return c.connected
|
||||
}
|
||||
|
||||
// GetID returns the client ID
|
||||
func (c *Client) GetID() string {
|
||||
return c.id
|
||||
}
|
||||
|
||||
// GetPhoneNumber returns the phone number
|
||||
func (c *Client) GetPhoneNumber() string {
|
||||
return c.phoneNumber
|
||||
}
|
||||
|
||||
// GetType returns the client type
|
||||
func (c *Client) GetType() string {
|
||||
return "business-api"
|
||||
}
|
||||
|
||||
// SendTextMessage sends a text message via Business API
|
||||
func (c *Client) SendTextMessage(ctx context.Context, jid types.JID, text string) (string, error) {
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
|
||||
// Convert JID to phone number
|
||||
phoneNumber := jidToPhoneNumber(jid)
|
||||
|
||||
// Create request
|
||||
reqBody := SendMessageRequest{
|
||||
MessagingProduct: "whatsapp",
|
||||
To: phoneNumber,
|
||||
Type: "text",
|
||||
Text: &TextObject{
|
||||
Body: text,
|
||||
},
|
||||
}
|
||||
|
||||
messageID, err := c.sendMessage(ctx, reqBody)
|
||||
if err != nil {
|
||||
c.eventBus.Publish(events.MessageFailedEvent(ctx, c.id, phoneNumber, text, err))
|
||||
return "", err
|
||||
}
|
||||
|
||||
logging.Debug("Message sent via Business API", "account_id", c.id, "to", phoneNumber)
|
||||
c.eventBus.Publish(events.MessageSentEvent(ctx, c.id, messageID, phoneNumber, text))
|
||||
return messageID, nil
|
||||
}
|
||||
|
||||
// SendImage sends an image message via Business API
|
||||
func (c *Client) SendImage(ctx context.Context, jid types.JID, imageData []byte, mimeType string, caption string) (string, error) {
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
|
||||
phoneNumber := jidToPhoneNumber(jid)
|
||||
|
||||
// Upload media first
|
||||
mediaID, err := c.uploadMedia(ctx, imageData, mimeType)
|
||||
if err != nil {
|
||||
c.eventBus.Publish(events.MessageFailedEvent(ctx, c.id, phoneNumber, caption, err))
|
||||
return "", fmt.Errorf("failed to upload image: %w", err)
|
||||
}
|
||||
|
||||
// Send message with media ID
|
||||
reqBody := SendMessageRequest{
|
||||
MessagingProduct: "whatsapp",
|
||||
To: phoneNumber,
|
||||
Type: "image",
|
||||
Image: &MediaObject{
|
||||
ID: mediaID,
|
||||
Caption: caption,
|
||||
},
|
||||
}
|
||||
|
||||
messageID, err := c.sendMessage(ctx, reqBody)
|
||||
if err != nil {
|
||||
c.eventBus.Publish(events.MessageFailedEvent(ctx, c.id, phoneNumber, caption, err))
|
||||
return "", err
|
||||
}
|
||||
|
||||
logging.Debug("Image sent via Business API", "account_id", c.id, "to", phoneNumber)
|
||||
c.eventBus.Publish(events.MessageSentEvent(ctx, c.id, messageID, phoneNumber, caption))
|
||||
return messageID, nil
|
||||
}
|
||||
|
||||
// SendVideo sends a video message via Business API
|
||||
func (c *Client) SendVideo(ctx context.Context, jid types.JID, videoData []byte, mimeType string, caption string) (string, error) {
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
|
||||
phoneNumber := jidToPhoneNumber(jid)
|
||||
|
||||
// Upload media first
|
||||
mediaID, err := c.uploadMedia(ctx, videoData, mimeType)
|
||||
if err != nil {
|
||||
c.eventBus.Publish(events.MessageFailedEvent(ctx, c.id, phoneNumber, caption, err))
|
||||
return "", fmt.Errorf("failed to upload video: %w", err)
|
||||
}
|
||||
|
||||
// Send message with media ID
|
||||
reqBody := SendMessageRequest{
|
||||
MessagingProduct: "whatsapp",
|
||||
To: phoneNumber,
|
||||
Type: "video",
|
||||
Video: &MediaObject{
|
||||
ID: mediaID,
|
||||
Caption: caption,
|
||||
},
|
||||
}
|
||||
|
||||
messageID, err := c.sendMessage(ctx, reqBody)
|
||||
if err != nil {
|
||||
c.eventBus.Publish(events.MessageFailedEvent(ctx, c.id, phoneNumber, caption, err))
|
||||
return "", err
|
||||
}
|
||||
|
||||
logging.Debug("Video sent via Business API", "account_id", c.id, "to", phoneNumber)
|
||||
c.eventBus.Publish(events.MessageSentEvent(ctx, c.id, messageID, phoneNumber, caption))
|
||||
return messageID, nil
|
||||
}
|
||||
|
||||
// SendDocument sends a document message via Business API
|
||||
func (c *Client) SendDocument(ctx context.Context, jid types.JID, documentData []byte, mimeType string, filename string, caption string) (string, error) {
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
|
||||
phoneNumber := jidToPhoneNumber(jid)
|
||||
|
||||
// Upload media first
|
||||
mediaID, err := c.uploadMedia(ctx, documentData, mimeType)
|
||||
if err != nil {
|
||||
c.eventBus.Publish(events.MessageFailedEvent(ctx, c.id, phoneNumber, caption, err))
|
||||
return "", fmt.Errorf("failed to upload document: %w", err)
|
||||
}
|
||||
|
||||
// Send message with media ID
|
||||
reqBody := SendMessageRequest{
|
||||
MessagingProduct: "whatsapp",
|
||||
To: phoneNumber,
|
||||
Type: "document",
|
||||
Document: &DocumentObject{
|
||||
ID: mediaID,
|
||||
Caption: caption,
|
||||
Filename: filename,
|
||||
},
|
||||
}
|
||||
|
||||
messageID, err := c.sendMessage(ctx, reqBody)
|
||||
if err != nil {
|
||||
c.eventBus.Publish(events.MessageFailedEvent(ctx, c.id, phoneNumber, caption, err))
|
||||
return "", err
|
||||
}
|
||||
|
||||
logging.Debug("Document sent via Business API", "account_id", c.id, "to", phoneNumber, "filename", filename)
|
||||
c.eventBus.Publish(events.MessageSentEvent(ctx, c.id, messageID, phoneNumber, caption))
|
||||
return messageID, nil
|
||||
}
|
||||
|
||||
// sendMessage sends a message request to the Business API
|
||||
func (c *Client) sendMessage(ctx context.Context, reqBody SendMessageRequest) (string, error) {
|
||||
url := fmt.Sprintf("https://graph.facebook.com/%s/%s/messages",
|
||||
c.config.APIVersion,
|
||||
c.config.PhoneNumberID)
|
||||
|
||||
jsonData, err := json.Marshal(reqBody)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to marshal request: %w", err)
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to create request: %w", err)
|
||||
}
|
||||
|
||||
req.Header.Set("Authorization", "Bearer "+c.config.AccessToken)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to send request: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to read response: %w", err)
|
||||
}
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
var errResp ErrorResponse
|
||||
if err := json.Unmarshal(body, &errResp); err == nil {
|
||||
return "", fmt.Errorf("API error: %s (code: %d)", errResp.Error.Message, errResp.Error.Code)
|
||||
}
|
||||
return "", fmt.Errorf("API returned status %d: %s", resp.StatusCode, string(body))
|
||||
}
|
||||
|
||||
var sendResp SendMessageResponse
|
||||
if err := json.Unmarshal(body, &sendResp); err != nil {
|
||||
return "", fmt.Errorf("failed to parse response: %w", err)
|
||||
}
|
||||
|
||||
if len(sendResp.Messages) == 0 {
|
||||
return "", fmt.Errorf("no message ID in response")
|
||||
}
|
||||
|
||||
return sendResp.Messages[0].ID, nil
|
||||
}
|
||||
|
||||
// jidToPhoneNumber converts a WhatsApp JID to E.164 phone number format
|
||||
func jidToPhoneNumber(jid types.JID) string {
|
||||
// JID format is like "27123456789@s.whatsapp.net"
|
||||
// Extract the phone number part before @
|
||||
phone := jid.User
|
||||
|
||||
// Ensure it starts with + for E.164
|
||||
if !strings.HasPrefix(phone, "+") {
|
||||
phone = "+" + phone
|
||||
}
|
||||
|
||||
return phone
|
||||
}
|
||||
|
||||
// phoneNumberToJID converts an E.164 phone number to WhatsApp JID
|
||||
func phoneNumberToJID(phoneNumber string) types.JID {
|
||||
// Remove + if present
|
||||
phone := strings.TrimPrefix(phoneNumber, "+")
|
||||
|
||||
// Create JID
|
||||
return types.JID{
|
||||
User: phone,
|
||||
Server: types.DefaultUserServer, // "s.whatsapp.net"
|
||||
}
|
||||
}
|
||||
288
internal/whatsapp/businessapi/events.go
Normal file
288
internal/whatsapp/businessapi/events.go
Normal file
@@ -0,0 +1,288 @@
|
||||
package businessapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"git.warky.dev/wdevs/whatshooked/internal/events"
|
||||
"git.warky.dev/wdevs/whatshooked/internal/logging"
|
||||
)
|
||||
|
||||
// HandleWebhook processes incoming webhook events from WhatsApp Business API
|
||||
func (c *Client) HandleWebhook(r *http.Request) error {
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to read request body: %w", err)
|
||||
}
|
||||
|
||||
var payload WebhookPayload
|
||||
if err := json.Unmarshal(body, &payload); err != nil {
|
||||
return fmt.Errorf("failed to parse webhook payload: %w", err)
|
||||
}
|
||||
|
||||
// Process each entry
|
||||
for _, entry := range payload.Entry {
|
||||
for _, change := range entry.Changes {
|
||||
c.processChange(change)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// processChange processes a webhook change
|
||||
func (c *Client) processChange(change WebhookChange) {
|
||||
ctx := context.Background()
|
||||
|
||||
// Process messages
|
||||
for _, msg := range change.Value.Messages {
|
||||
c.processMessage(ctx, msg, change.Value.Contacts)
|
||||
}
|
||||
|
||||
// Process statuses
|
||||
for _, status := range change.Value.Statuses {
|
||||
c.processStatus(ctx, status)
|
||||
}
|
||||
}
|
||||
|
||||
// processMessage processes an incoming message
|
||||
func (c *Client) processMessage(ctx context.Context, msg WebhookMessage, contacts []WebhookContact) {
|
||||
// Get sender name from contacts
|
||||
senderName := ""
|
||||
for _, contact := range contacts {
|
||||
if contact.WaID == msg.From {
|
||||
senderName = contact.Profile.Name
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// Parse timestamp
|
||||
timestamp := c.parseTimestamp(msg.Timestamp)
|
||||
|
||||
var text string
|
||||
var messageType string
|
||||
var mimeType string
|
||||
var filename string
|
||||
var mediaBase64 string
|
||||
var mediaURL string
|
||||
|
||||
// Process based on message type
|
||||
switch msg.Type {
|
||||
case "text":
|
||||
if msg.Text != nil {
|
||||
text = msg.Text.Body
|
||||
}
|
||||
messageType = "text"
|
||||
|
||||
case "image":
|
||||
if msg.Image != nil {
|
||||
messageType = "image"
|
||||
mimeType = msg.Image.MimeType
|
||||
text = msg.Image.Caption
|
||||
|
||||
// Download and process media
|
||||
data, _, err := c.downloadMedia(ctx, msg.Image.ID)
|
||||
if err != nil {
|
||||
logging.Error("Failed to download image", "account_id", c.id, "media_id", msg.Image.ID, "error", err)
|
||||
} else {
|
||||
filename, mediaURL = c.processMediaData(msg.ID, data, mimeType, &mediaBase64)
|
||||
}
|
||||
}
|
||||
|
||||
case "video":
|
||||
if msg.Video != nil {
|
||||
messageType = "video"
|
||||
mimeType = msg.Video.MimeType
|
||||
text = msg.Video.Caption
|
||||
|
||||
// Download and process media
|
||||
data, _, err := c.downloadMedia(ctx, msg.Video.ID)
|
||||
if err != nil {
|
||||
logging.Error("Failed to download video", "account_id", c.id, "media_id", msg.Video.ID, "error", err)
|
||||
} else {
|
||||
filename, mediaURL = c.processMediaData(msg.ID, data, mimeType, &mediaBase64)
|
||||
}
|
||||
}
|
||||
|
||||
case "document":
|
||||
if msg.Document != nil {
|
||||
messageType = "document"
|
||||
mimeType = msg.Document.MimeType
|
||||
text = msg.Document.Caption
|
||||
filename = msg.Document.Filename
|
||||
|
||||
// Download and process media
|
||||
data, _, err := c.downloadMedia(ctx, msg.Document.ID)
|
||||
if err != nil {
|
||||
logging.Error("Failed to download document", "account_id", c.id, "media_id", msg.Document.ID, "error", err)
|
||||
} else {
|
||||
filename, mediaURL = c.processMediaData(msg.ID, data, mimeType, &mediaBase64)
|
||||
}
|
||||
}
|
||||
|
||||
default:
|
||||
logging.Warn("Unsupported message type", "account_id", c.id, "type", msg.Type)
|
||||
return
|
||||
}
|
||||
|
||||
// Publish message received event
|
||||
c.eventBus.Publish(events.MessageReceivedEvent(
|
||||
ctx,
|
||||
c.id,
|
||||
msg.ID,
|
||||
msg.From,
|
||||
msg.From, // For Business API, chat is same as sender for individual messages
|
||||
text,
|
||||
timestamp,
|
||||
false, // Business API doesn't indicate groups in this webhook
|
||||
"",
|
||||
senderName,
|
||||
messageType,
|
||||
mimeType,
|
||||
filename,
|
||||
mediaBase64,
|
||||
mediaURL,
|
||||
))
|
||||
|
||||
logging.Debug("Message received via Business API", "account_id", c.id, "from", msg.From, "type", messageType)
|
||||
}
|
||||
|
||||
// processStatus processes a message status update
|
||||
func (c *Client) processStatus(ctx context.Context, status WebhookStatus) {
|
||||
timestamp := c.parseTimestamp(status.Timestamp)
|
||||
|
||||
switch status.Status {
|
||||
case "sent":
|
||||
c.eventBus.Publish(events.MessageSentEvent(ctx, c.id, status.ID, status.RecipientID, ""))
|
||||
logging.Debug("Message sent status", "account_id", c.id, "message_id", status.ID)
|
||||
|
||||
case "delivered":
|
||||
c.eventBus.Publish(events.MessageDeliveredEvent(ctx, c.id, status.ID, status.RecipientID, timestamp))
|
||||
logging.Debug("Message delivered", "account_id", c.id, "message_id", status.ID)
|
||||
|
||||
case "read":
|
||||
c.eventBus.Publish(events.MessageReadEvent(ctx, c.id, status.ID, status.RecipientID, timestamp))
|
||||
logging.Debug("Message read", "account_id", c.id, "message_id", status.ID)
|
||||
|
||||
case "failed":
|
||||
errMsg := "unknown error"
|
||||
if len(status.Errors) > 0 {
|
||||
errMsg = fmt.Sprintf("%s (code: %d)", status.Errors[0].Title, status.Errors[0].Code)
|
||||
}
|
||||
c.eventBus.Publish(events.MessageFailedEvent(ctx, c.id, status.RecipientID, "", fmt.Errorf("%s", errMsg)))
|
||||
logging.Error("Message failed", "account_id", c.id, "message_id", status.ID, "error", errMsg)
|
||||
|
||||
default:
|
||||
logging.Debug("Unknown status type", "account_id", c.id, "status", status.Status)
|
||||
}
|
||||
}
|
||||
|
||||
// parseTimestamp parses a Unix timestamp string to time.Time
|
||||
func (c *Client) parseTimestamp(ts string) time.Time {
|
||||
unix, err := strconv.ParseInt(ts, 10, 64)
|
||||
if err != nil {
|
||||
logging.Warn("Failed to parse timestamp", "timestamp", ts, "error", err)
|
||||
return time.Now()
|
||||
}
|
||||
return time.Unix(unix, 0)
|
||||
}
|
||||
|
||||
// processMediaData processes media based on the configured mode
|
||||
func (c *Client) processMediaData(messageID string, data []byte, mimeType string, mediaBase64 *string) (string, string) {
|
||||
mode := c.mediaConfig.Mode
|
||||
var filename string
|
||||
var mediaURL string
|
||||
|
||||
// Generate filename
|
||||
ext := getExtensionFromMimeType(mimeType)
|
||||
hash := sha256.Sum256(data)
|
||||
hashStr := hex.EncodeToString(hash[:8])
|
||||
filename = fmt.Sprintf("%s_%s%s", messageID, hashStr, ext)
|
||||
|
||||
// Handle base64 mode
|
||||
if mode == "base64" || mode == "both" {
|
||||
*mediaBase64 = base64.StdEncoding.EncodeToString(data)
|
||||
}
|
||||
|
||||
// Handle link mode
|
||||
if mode == "link" || mode == "both" {
|
||||
// Save file to disk
|
||||
filePath, err := c.saveMediaFile(messageID, data, mimeType)
|
||||
if err != nil {
|
||||
logging.Error("Failed to save media file", "account_id", c.id, "message_id", messageID, "error", err)
|
||||
} else {
|
||||
filename = filepath.Base(filePath)
|
||||
mediaURL = c.generateMediaURL(messageID, filename)
|
||||
}
|
||||
}
|
||||
|
||||
return filename, mediaURL
|
||||
}
|
||||
|
||||
// saveMediaFile saves media data to disk
|
||||
func (c *Client) saveMediaFile(messageID string, data []byte, mimeType string) (string, error) {
|
||||
mediaDir := filepath.Join(c.mediaConfig.DataPath, c.id)
|
||||
if err := os.MkdirAll(mediaDir, 0755); err != nil {
|
||||
return "", fmt.Errorf("failed to create media directory: %w", err)
|
||||
}
|
||||
|
||||
hash := sha256.Sum256(data)
|
||||
hashStr := hex.EncodeToString(hash[:8])
|
||||
ext := getExtensionFromMimeType(mimeType)
|
||||
filename := fmt.Sprintf("%s_%s%s", messageID, hashStr, ext)
|
||||
|
||||
filePath := filepath.Join(mediaDir, filename)
|
||||
|
||||
if err := os.WriteFile(filePath, data, 0644); err != nil {
|
||||
return "", fmt.Errorf("failed to write media file: %w", err)
|
||||
}
|
||||
|
||||
return filePath, nil
|
||||
}
|
||||
|
||||
// generateMediaURL generates a URL for accessing stored media
|
||||
func (c *Client) generateMediaURL(messageID, filename string) string {
|
||||
baseURL := c.mediaConfig.BaseURL
|
||||
if baseURL == "" {
|
||||
baseURL = "http://localhost:8080"
|
||||
}
|
||||
return fmt.Sprintf("%s/api/media/%s/%s", baseURL, c.id, filename)
|
||||
}
|
||||
|
||||
// getExtensionFromMimeType returns the file extension for a given MIME type
|
||||
func getExtensionFromMimeType(mimeType string) string {
|
||||
extensions := map[string]string{
|
||||
"image/jpeg": ".jpg",
|
||||
"image/png": ".png",
|
||||
"image/gif": ".gif",
|
||||
"image/webp": ".webp",
|
||||
"video/mp4": ".mp4",
|
||||
"video/mpeg": ".mpeg",
|
||||
"video/webm": ".webm",
|
||||
"video/3gpp": ".3gp",
|
||||
"application/pdf": ".pdf",
|
||||
"application/msword": ".doc",
|
||||
"application/vnd.openxmlformats-officedocument.wordprocessingml.document": ".docx",
|
||||
"application/vnd.ms-excel": ".xls",
|
||||
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": ".xlsx",
|
||||
"text/plain": ".txt",
|
||||
"application/json": ".json",
|
||||
"audio/mpeg": ".mp3",
|
||||
"audio/ogg": ".ogg",
|
||||
}
|
||||
|
||||
if ext, ok := extensions[mimeType]; ok {
|
||||
return ext
|
||||
}
|
||||
return ""
|
||||
}
|
||||
138
internal/whatsapp/businessapi/media.go
Normal file
138
internal/whatsapp/businessapi/media.go
Normal file
@@ -0,0 +1,138 @@
|
||||
package businessapi
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// uploadMedia uploads media to the Business API and returns the media ID
|
||||
func (c *Client) uploadMedia(ctx context.Context, data []byte, mimeType string) (string, error) {
|
||||
url := fmt.Sprintf("https://graph.facebook.com/%s/%s/media",
|
||||
c.config.APIVersion,
|
||||
c.config.PhoneNumberID)
|
||||
|
||||
// Create multipart form data
|
||||
var requestBody bytes.Buffer
|
||||
writer := multipart.NewWriter(&requestBody)
|
||||
|
||||
// Add the file
|
||||
part, err := writer.CreateFormFile("file", "media")
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to create form file: %w", err)
|
||||
}
|
||||
|
||||
if _, err := part.Write(data); err != nil {
|
||||
return "", fmt.Errorf("failed to write file data: %w", err)
|
||||
}
|
||||
|
||||
// Add messaging_product field
|
||||
if err := writer.WriteField("messaging_product", "whatsapp"); err != nil {
|
||||
return "", fmt.Errorf("failed to write messaging_product field: %w", err)
|
||||
}
|
||||
|
||||
// Add type field (mime type)
|
||||
if err := writer.WriteField("type", mimeType); err != nil {
|
||||
return "", fmt.Errorf("failed to write type field: %w", err)
|
||||
}
|
||||
|
||||
if err := writer.Close(); err != nil {
|
||||
return "", fmt.Errorf("failed to close multipart writer: %w", err)
|
||||
}
|
||||
|
||||
// Create request
|
||||
req, err := http.NewRequestWithContext(ctx, "POST", url, &requestBody)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to create request: %w", err)
|
||||
}
|
||||
|
||||
req.Header.Set("Authorization", "Bearer "+c.config.AccessToken)
|
||||
req.Header.Set("Content-Type", writer.FormDataContentType())
|
||||
|
||||
// Send request
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to upload media: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to read response: %w", err)
|
||||
}
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
var errResp ErrorResponse
|
||||
if err := json.Unmarshal(body, &errResp); err == nil {
|
||||
return "", fmt.Errorf("upload error: %s (code: %d)", errResp.Error.Message, errResp.Error.Code)
|
||||
}
|
||||
return "", fmt.Errorf("upload failed with status %d: %s", resp.StatusCode, string(body))
|
||||
}
|
||||
|
||||
var uploadResp MediaUploadResponse
|
||||
if err := json.Unmarshal(body, &uploadResp); err != nil {
|
||||
return "", fmt.Errorf("failed to parse upload response: %w", err)
|
||||
}
|
||||
|
||||
return uploadResp.ID, nil
|
||||
}
|
||||
|
||||
// downloadMedia downloads media from the Business API using the media ID
|
||||
func (c *Client) downloadMedia(ctx context.Context, mediaID string) ([]byte, string, error) {
|
||||
// Step 1: Get the media URL
|
||||
url := fmt.Sprintf("https://graph.facebook.com/%s/%s",
|
||||
c.config.APIVersion,
|
||||
mediaID)
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
|
||||
if err != nil {
|
||||
return nil, "", fmt.Errorf("failed to create request: %w", err)
|
||||
}
|
||||
|
||||
req.Header.Set("Authorization", "Bearer "+c.config.AccessToken)
|
||||
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, "", fmt.Errorf("failed to get media URL: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
return nil, "", fmt.Errorf("failed to get media URL, status %d: %s", resp.StatusCode, string(body))
|
||||
}
|
||||
|
||||
var mediaResp MediaURLResponse
|
||||
if err := json.NewDecoder(resp.Body).Decode(&mediaResp); err != nil {
|
||||
return nil, "", fmt.Errorf("failed to parse media URL response: %w", err)
|
||||
}
|
||||
|
||||
// Step 2: Download from the CDN URL
|
||||
downloadReq, err := http.NewRequestWithContext(ctx, "GET", mediaResp.URL, nil)
|
||||
if err != nil {
|
||||
return nil, "", fmt.Errorf("failed to create download request: %w", err)
|
||||
}
|
||||
|
||||
downloadReq.Header.Set("Authorization", "Bearer "+c.config.AccessToken)
|
||||
|
||||
downloadResp, err := c.httpClient.Do(downloadReq)
|
||||
if err != nil {
|
||||
return nil, "", fmt.Errorf("failed to download media: %w", err)
|
||||
}
|
||||
defer downloadResp.Body.Close()
|
||||
|
||||
if downloadResp.StatusCode != http.StatusOK {
|
||||
return nil, "", fmt.Errorf("failed to download media, status %d", downloadResp.StatusCode)
|
||||
}
|
||||
|
||||
data, err := io.ReadAll(downloadResp.Body)
|
||||
if err != nil {
|
||||
return nil, "", fmt.Errorf("failed to read media data: %w", err)
|
||||
}
|
||||
|
||||
return data, mediaResp.MimeType, nil
|
||||
}
|
||||
193
internal/whatsapp/businessapi/types.go
Normal file
193
internal/whatsapp/businessapi/types.go
Normal file
@@ -0,0 +1,193 @@
|
||||
package businessapi
|
||||
|
||||
// SendMessageRequest represents a request to send a text message via Business API
|
||||
type SendMessageRequest struct {
|
||||
MessagingProduct string `json:"messaging_product"` // Always "whatsapp"
|
||||
RecipientType string `json:"recipient_type,omitempty"` // "individual"
|
||||
To string `json:"to"` // Phone number in E.164 format
|
||||
Type string `json:"type"` // "text", "image", "video", "document"
|
||||
Text *TextObject `json:"text,omitempty"`
|
||||
Image *MediaObject `json:"image,omitempty"`
|
||||
Video *MediaObject `json:"video,omitempty"`
|
||||
Document *DocumentObject `json:"document,omitempty"`
|
||||
}
|
||||
|
||||
// TextObject represents a text message
|
||||
type TextObject struct {
|
||||
Body string `json:"body"`
|
||||
}
|
||||
|
||||
// MediaObject represents media (image/video) message
|
||||
type MediaObject struct {
|
||||
ID string `json:"id,omitempty"` // Media ID (from upload)
|
||||
Link string `json:"link,omitempty"` // Or direct URL
|
||||
Caption string `json:"caption,omitempty"`
|
||||
}
|
||||
|
||||
// DocumentObject represents a document message
|
||||
type DocumentObject struct {
|
||||
ID string `json:"id,omitempty"` // Media ID (from upload)
|
||||
Link string `json:"link,omitempty"` // Or direct URL
|
||||
Caption string `json:"caption,omitempty"`
|
||||
Filename string `json:"filename,omitempty"`
|
||||
}
|
||||
|
||||
// SendMessageResponse represents the response from sending a message
|
||||
type SendMessageResponse struct {
|
||||
MessagingProduct string `json:"messaging_product"`
|
||||
Contacts []struct {
|
||||
Input string `json:"input"`
|
||||
WaID string `json:"wa_id"`
|
||||
} `json:"contacts"`
|
||||
Messages []struct {
|
||||
ID string `json:"id"`
|
||||
} `json:"messages"`
|
||||
}
|
||||
|
||||
// MediaUploadResponse represents the response from uploading media
|
||||
type MediaUploadResponse struct {
|
||||
ID string `json:"id"` // Media ID to use in messages
|
||||
}
|
||||
|
||||
// MediaURLResponse represents the response when getting media URL
|
||||
type MediaURLResponse struct {
|
||||
URL string `json:"url"` // CDN URL to download media
|
||||
MimeType string `json:"mime_type"`
|
||||
SHA256 string `json:"sha256"`
|
||||
FileSize int64 `json:"file_size"`
|
||||
ID string `json:"id"`
|
||||
MessagingProduct string `json:"messaging_product"`
|
||||
}
|
||||
|
||||
// ErrorResponse represents an error from the Business API
|
||||
type ErrorResponse struct {
|
||||
Error struct {
|
||||
Message string `json:"message"`
|
||||
Type string `json:"type"`
|
||||
Code int `json:"code"`
|
||||
ErrorSubcode int `json:"error_subcode,omitempty"`
|
||||
FBTraceID string `json:"fbtrace_id,omitempty"`
|
||||
} `json:"error"`
|
||||
}
|
||||
|
||||
// WebhookPayload represents the incoming webhook from WhatsApp Business API
|
||||
type WebhookPayload struct {
|
||||
Object string `json:"object"` // "whatsapp_business_account"
|
||||
Entry []WebhookEntry `json:"entry"`
|
||||
}
|
||||
|
||||
// WebhookEntry represents an entry in the webhook
|
||||
type WebhookEntry struct {
|
||||
ID string `json:"id"` // WhatsApp Business Account ID
|
||||
Changes []WebhookChange `json:"changes"`
|
||||
}
|
||||
|
||||
// WebhookChange represents a change notification
|
||||
type WebhookChange struct {
|
||||
Value WebhookValue `json:"value"`
|
||||
Field string `json:"field"` // "messages"
|
||||
}
|
||||
|
||||
// WebhookValue contains the actual webhook data
|
||||
type WebhookValue struct {
|
||||
MessagingProduct string `json:"messaging_product"`
|
||||
Metadata WebhookMetadata `json:"metadata"`
|
||||
Contacts []WebhookContact `json:"contacts,omitempty"`
|
||||
Messages []WebhookMessage `json:"messages,omitempty"`
|
||||
Statuses []WebhookStatus `json:"statuses,omitempty"`
|
||||
}
|
||||
|
||||
// WebhookMetadata contains metadata about the phone number
|
||||
type WebhookMetadata struct {
|
||||
DisplayPhoneNumber string `json:"display_phone_number"`
|
||||
PhoneNumberID string `json:"phone_number_id"`
|
||||
}
|
||||
|
||||
// WebhookContact represents a contact in the webhook
|
||||
type WebhookContact struct {
|
||||
Profile WebhookProfile `json:"profile"`
|
||||
WaID string `json:"wa_id"`
|
||||
}
|
||||
|
||||
// WebhookProfile contains profile information
|
||||
type WebhookProfile struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
// WebhookMessage represents a message in the webhook
|
||||
type WebhookMessage struct {
|
||||
From string `json:"from"` // Sender phone number
|
||||
ID string `json:"id"` // Message ID
|
||||
Timestamp string `json:"timestamp"` // Unix timestamp as string
|
||||
Type string `json:"type"` // "text", "image", "video", "document", etc.
|
||||
Text *WebhookText `json:"text,omitempty"`
|
||||
Image *WebhookMediaMessage `json:"image,omitempty"`
|
||||
Video *WebhookMediaMessage `json:"video,omitempty"`
|
||||
Document *WebhookDocumentMessage `json:"document,omitempty"`
|
||||
Context *WebhookContext `json:"context,omitempty"` // Reply context
|
||||
}
|
||||
|
||||
// WebhookText represents a text message
|
||||
type WebhookText struct {
|
||||
Body string `json:"body"`
|
||||
}
|
||||
|
||||
// WebhookMediaMessage represents a media message (image/video)
|
||||
type WebhookMediaMessage struct {
|
||||
ID string `json:"id"` // Media ID
|
||||
MimeType string `json:"mime_type"`
|
||||
SHA256 string `json:"sha256"`
|
||||
Caption string `json:"caption,omitempty"`
|
||||
}
|
||||
|
||||
// WebhookDocumentMessage represents a document message
|
||||
type WebhookDocumentMessage struct {
|
||||
ID string `json:"id"` // Media ID
|
||||
MimeType string `json:"mime_type"`
|
||||
SHA256 string `json:"sha256"`
|
||||
Filename string `json:"filename,omitempty"`
|
||||
Caption string `json:"caption,omitempty"`
|
||||
}
|
||||
|
||||
// WebhookContext represents reply context
|
||||
type WebhookContext struct {
|
||||
From string `json:"from"`
|
||||
ID string `json:"id"` // Message ID being replied to
|
||||
MessageID string `json:"message_id,omitempty"`
|
||||
}
|
||||
|
||||
// WebhookStatus represents a message status update
|
||||
type WebhookStatus struct {
|
||||
ID string `json:"id"` // Message ID
|
||||
Status string `json:"status"` // "sent", "delivered", "read", "failed"
|
||||
Timestamp string `json:"timestamp"` // Unix timestamp as string
|
||||
RecipientID string `json:"recipient_id"`
|
||||
Conversation *WebhookConversation `json:"conversation,omitempty"`
|
||||
Pricing *WebhookPricing `json:"pricing,omitempty"`
|
||||
Errors []WebhookError `json:"errors,omitempty"`
|
||||
}
|
||||
|
||||
// WebhookConversation contains conversation details
|
||||
type WebhookConversation struct {
|
||||
ID string `json:"id"`
|
||||
ExpirationTimestamp string `json:"expiration_timestamp,omitempty"`
|
||||
Origin WebhookOrigin `json:"origin"`
|
||||
}
|
||||
|
||||
// WebhookOrigin contains conversation origin
|
||||
type WebhookOrigin struct {
|
||||
Type string `json:"type"`
|
||||
}
|
||||
|
||||
// WebhookPricing contains pricing information
|
||||
type WebhookPricing struct {
|
||||
Billable bool `json:"billable"`
|
||||
PricingModel string `json:"pricing_model"`
|
||||
Category string `json:"category"`
|
||||
}
|
||||
|
||||
// WebhookError represents an error in status update
|
||||
type WebhookError struct {
|
||||
Code int `json:"code"`
|
||||
Title string `json:"title"`
|
||||
}
|
||||
Reference in New Issue
Block a user