Lint fixes and testing workflow actions
This commit is contained in:
@@ -340,15 +340,3 @@ func jidToPhoneNumber(jid types.JID) string {
|
||||
|
||||
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"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,8 +32,8 @@ func (c *Client) HandleWebhook(r *http.Request) error {
|
||||
|
||||
// Process each entry
|
||||
for _, entry := range payload.Entry {
|
||||
for _, change := range entry.Changes {
|
||||
c.processChange(change)
|
||||
for i := range entry.Changes {
|
||||
c.processChange(entry.Changes[i])
|
||||
}
|
||||
}
|
||||
|
||||
@@ -198,10 +198,8 @@ func (c *Client) parseTimestamp(ts string) time.Time {
|
||||
}
|
||||
|
||||
// processMediaData processes media based on the configured mode
|
||||
func (c *Client) processMediaData(messageID string, data []byte, mimeType string, mediaBase64 *string) (string, string) {
|
||||
func (c *Client) processMediaData(messageID string, data []byte, mimeType string, mediaBase64 *string) (filename string, mediaURL string) {
|
||||
mode := c.mediaConfig.Mode
|
||||
var filename string
|
||||
var mediaURL string
|
||||
|
||||
// Generate filename
|
||||
ext := getExtensionFromMimeType(mimeType)
|
||||
@@ -262,23 +260,23 @@ func (c *Client) generateMediaURL(messageID, filename string) string {
|
||||
// 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",
|
||||
"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.ms-excel": ".xls",
|
||||
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": ".xlsx",
|
||||
"text/plain": ".txt",
|
||||
"text/plain": ".txt",
|
||||
"application/json": ".json",
|
||||
"audio/mpeg": ".mp3",
|
||||
"audio/ogg": ".ogg",
|
||||
"audio/mpeg": ".mp3",
|
||||
"audio/ogg": ".ogg",
|
||||
}
|
||||
|
||||
if ext, ok := extensions[mimeType]; ok {
|
||||
|
||||
@@ -82,7 +82,7 @@ func (c *Client) uploadMedia(ctx context.Context, data []byte, mimeType string)
|
||||
}
|
||||
|
||||
// downloadMedia downloads media from the Business API using the media ID
|
||||
func (c *Client) downloadMedia(ctx context.Context, mediaID string) ([]byte, string, error) {
|
||||
func (c *Client) downloadMedia(ctx context.Context, mediaID string) (data []byte, mimeType string, err error) {
|
||||
// Step 1: Get the media URL
|
||||
url := fmt.Sprintf("https://graph.facebook.com/%s/%s",
|
||||
c.config.APIVersion,
|
||||
@@ -129,10 +129,11 @@ func (c *Client) downloadMedia(ctx context.Context, mediaID string) ([]byte, str
|
||||
return nil, "", fmt.Errorf("failed to download media, status %d", downloadResp.StatusCode)
|
||||
}
|
||||
|
||||
data, err := io.ReadAll(downloadResp.Body)
|
||||
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
|
||||
mimeType = mediaResp.MimeType
|
||||
return
|
||||
}
|
||||
|
||||
@@ -2,13 +2,13 @@ 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"`
|
||||
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"`
|
||||
}
|
||||
|
||||
@@ -19,14 +19,14 @@ type TextObject struct {
|
||||
|
||||
// MediaObject represents media (image/video) message
|
||||
type MediaObject struct {
|
||||
ID string `json:"id,omitempty"` // Media ID (from upload)
|
||||
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)
|
||||
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"`
|
||||
@@ -51,11 +51,11 @@ type MediaUploadResponse struct {
|
||||
|
||||
// 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"`
|
||||
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"`
|
||||
}
|
||||
|
||||
@@ -90,11 +90,11 @@ type WebhookChange struct {
|
||||
|
||||
// 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"`
|
||||
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
|
||||
@@ -116,15 +116,15 @@ type WebhookProfile struct {
|
||||
|
||||
// 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"`
|
||||
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
|
||||
Context *WebhookContext `json:"context,omitempty"` // Reply context
|
||||
}
|
||||
|
||||
// WebhookText represents a text message
|
||||
@@ -158,20 +158,20 @@ type WebhookContext struct {
|
||||
|
||||
// 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"`
|
||||
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"`
|
||||
ID string `json:"id"`
|
||||
ExpirationTimestamp string `json:"expiration_timestamp,omitempty"`
|
||||
Origin WebhookOrigin `json:"origin"`
|
||||
}
|
||||
|
||||
// WebhookOrigin contains conversation origin
|
||||
|
||||
@@ -395,7 +395,7 @@ func (c *Client) handleEvent(evt interface{}) {
|
||||
|
||||
// Extract message content based on type
|
||||
var text string
|
||||
var messageType string = "text"
|
||||
var messageType = "text"
|
||||
var mimeType string
|
||||
var filename string
|
||||
var mediaBase64 string
|
||||
@@ -516,12 +516,13 @@ func (c *Client) handleEvent(evt interface{}) {
|
||||
|
||||
case *waEvents.Receipt:
|
||||
// Handle delivery and read receipts
|
||||
if v.Type == types.ReceiptTypeDelivered {
|
||||
switch v.Type {
|
||||
case types.ReceiptTypeDelivered:
|
||||
for _, messageID := range v.MessageIDs {
|
||||
logging.Debug("Message delivered", "account_id", c.id, "message_id", messageID, "from", v.Sender.String())
|
||||
c.eventBus.Publish(events.MessageDeliveredEvent(ctx, c.id, messageID, v.Sender.String(), v.Timestamp))
|
||||
}
|
||||
} else if v.Type == types.ReceiptTypeRead {
|
||||
case types.ReceiptTypeRead:
|
||||
for _, messageID := range v.MessageIDs {
|
||||
logging.Debug("Message read", "account_id", c.id, "message_id", messageID, "from", v.Sender.String())
|
||||
c.eventBus.Publish(events.MessageReadEvent(ctx, c.id, messageID, v.Sender.String(), v.Timestamp))
|
||||
@@ -561,10 +562,8 @@ func (c *Client) startKeepAlive() {
|
||||
}
|
||||
|
||||
// processMediaData processes media based on the configured mode
|
||||
func (c *Client) processMediaData(messageID string, data []byte, mimeType string, mediaBase64 *string) (string, string) {
|
||||
func (c *Client) processMediaData(messageID string, data []byte, mimeType string, mediaBase64 *string) (filename string, mediaURL string) {
|
||||
mode := c.mediaConfig.Mode
|
||||
var filename string
|
||||
var mediaURL string
|
||||
|
||||
// Generate filename
|
||||
ext := getExtensionFromMimeType(mimeType)
|
||||
|
||||
Reference in New Issue
Block a user