Skip to content

Commit

Permalink
fix: add business account id from the incoming webhook to the event t…
Browse files Browse the repository at this point in the history
…rigger

Signed-off-by: sarthakjdev <jsarthak448@gmail.com>
  • Loading branch information
sarthakjdev committed Jan 6, 2025
1 parent 953ae2d commit c3374a1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
17 changes: 15 additions & 2 deletions internal/manager/webhook_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (wh *WebhookManager) PostRequestHandler(c echo.Context) error {
if err := json.Unmarshal(valueBytes, &messageValue); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid MessagesValue JSON: %v", err))
}
err = wh.handleMessagesSubscriptionEvents(messageValue.Messages, messageValue.Statuses, messageValue.Metadata.PhoneNumberId)
err = wh.handleMessagesSubscriptionEvents(messageValue.Messages, messageValue.Statuses, messageValue.Metadata.PhoneNumberId, entry.Id)
if err != nil {
fmt.Println("Error handling messages subscription events:", err)
c.String(500, "Internal server error")
Expand Down Expand Up @@ -294,7 +294,7 @@ func (wh *WebhookManager) ListenToEvents() {
}
}

func (wh *WebhookManager) handleMessagesSubscriptionEvents(messages []Message, statuses []Status, phoneNumberId string) error {
func (wh *WebhookManager) handleMessagesSubscriptionEvents(messages []Message, statuses []Status, phoneNumberId, businessAccountId string) error {
// consider the field here too, because we will be supporting more events
if len(statuses) > 0 {
for _, status := range statuses {
Expand Down Expand Up @@ -333,6 +333,7 @@ func (wh *WebhookManager) handleMessagesSubscriptionEvents(messages []Message, s
{
wh.EventManager.Publish(events.TextMessageEventType, events.NewTextMessageEvent(
events.NewBaseMessageEvent(
businessAccountId,
phoneNumberId,
message.Id,
message.Timestamp,
Expand All @@ -357,6 +358,7 @@ func (wh *WebhookManager) handleMessagesSubscriptionEvents(messages []Message, s

wh.EventManager.Publish(events.ImageMessageEventType, events.NewImageMessageEvent(
events.NewBaseMessageEvent(
businessAccountId,
phoneNumberId,
message.Id,
message.Timestamp,
Expand All @@ -382,6 +384,7 @@ func (wh *WebhookManager) handleMessagesSubscriptionEvents(messages []Message, s

wh.EventManager.Publish(events.AudioMessageEventType, events.NewAudioMessageEvent(
events.NewBaseMessageEvent(
businessAccountId,
phoneNumberId,
message.Id,
message.Timestamp,
Expand Down Expand Up @@ -409,6 +412,7 @@ func (wh *WebhookManager) handleMessagesSubscriptionEvents(messages []Message, s

wh.EventManager.Publish(events.VideoMessageEventType, events.NewVideoMessageEvent(
events.NewBaseMessageEvent(
businessAccountId,
phoneNumberId,
message.Id,
message.Timestamp,
Expand All @@ -435,6 +439,7 @@ func (wh *WebhookManager) handleMessagesSubscriptionEvents(messages []Message, s

wh.EventManager.Publish(events.DocumentMessageEventType, events.NewVideoMessageEvent(
events.NewBaseMessageEvent(
businessAccountId,
phoneNumberId,
message.Id,
message.Timestamp,
Expand All @@ -457,6 +462,7 @@ func (wh *WebhookManager) handleMessagesSubscriptionEvents(messages []Message, s

wh.EventManager.Publish(events.LocationMessageEventType, events.NewLocationMessageEvent(
events.NewBaseMessageEvent(
businessAccountId,
phoneNumberId,
message.Id,
message.Timestamp,
Expand All @@ -470,6 +476,7 @@ func (wh *WebhookManager) handleMessagesSubscriptionEvents(messages []Message, s
{
wh.EventManager.Publish(events.ContactMessageEventType, events.NewTextMessageEvent(
events.NewBaseMessageEvent(
businessAccountId,
phoneNumberId,
message.Id,
message.Timestamp,
Expand All @@ -494,6 +501,7 @@ func (wh *WebhookManager) handleMessagesSubscriptionEvents(messages []Message, s

wh.EventManager.Publish(events.StickerMessageEventType, events.NewStickerMessageEvent(
events.NewBaseMessageEvent(
businessAccountId,
phoneNumberId,
message.Id,
message.Timestamp,
Expand All @@ -509,6 +517,7 @@ func (wh *WebhookManager) handleMessagesSubscriptionEvents(messages []Message, s
{
wh.EventManager.Publish(events.QuickReplyMessageEventType, events.NewQuickReplyButtonInteractionEvent(
events.NewBaseMessageEvent(
businessAccountId,
phoneNumberId,
message.Id,
message.Timestamp,
Expand All @@ -524,6 +533,7 @@ func (wh *WebhookManager) handleMessagesSubscriptionEvents(messages []Message, s
if message.Interactive.Type == "list" {
wh.EventManager.Publish(events.ListInteractionMessageEventType, events.NewListInteractionEvent(
events.NewBaseMessageEvent(
businessAccountId,
phoneNumberId,
message.Id,
message.Timestamp,
Expand All @@ -537,6 +547,7 @@ func (wh *WebhookManager) handleMessagesSubscriptionEvents(messages []Message, s
} else {
wh.EventManager.Publish(events.ReplyButtonInteractionEventType, events.NewReplyButtonInteractionEvent(
events.NewBaseMessageEvent(
businessAccountId,
phoneNumberId,
message.Id,
message.Timestamp,
Expand All @@ -563,6 +574,7 @@ func (wh *WebhookManager) handleMessagesSubscriptionEvents(messages []Message, s

wh.EventManager.Publish(events.ReactionMessageEventType, events.NewReactionMessageEvent(
events.NewBaseMessageEvent(
businessAccountId,
phoneNumberId,
message.Id,
message.Timestamp,
Expand All @@ -576,6 +588,7 @@ func (wh *WebhookManager) handleMessagesSubscriptionEvents(messages []Message, s
{
wh.EventManager.Publish(events.OrderReceivedEventType, events.NewTextMessageEvent(
events.NewBaseMessageEvent(
businessAccountId,
phoneNumberId,
message.Id,
message.Timestamp,
Expand Down
15 changes: 8 additions & 7 deletions pkg/events/base_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,16 @@ type BaseBusinessAccountEventInterface interface {
}

type BaseMessageEvent struct {
requester request_client.RequestClient
MessageId string `json:"message_id"`
Context MessageContext `json:"context"`
Timestamp string `json:"timestamp"`
IsForwarded bool `json:"is_forwarded"`
PhoneNumber string `json:"phone_number"`
BusinessAccountId string `json:"business_account_id"`
requester request_client.RequestClient
MessageId string `json:"message_id"`
Context MessageContext `json:"context"`
Timestamp string `json:"timestamp"`
IsForwarded bool `json:"is_forwarded"`
PhoneNumber string `json:"phone_number"`
}

func NewBaseMessageEvent(phoneNumber string, messageId string, timestamp string, from string, isForwarded bool, requester request_client.RequestClient) BaseMessageEvent {
func NewBaseMessageEvent(businessAccountId, messageId, phoneNumber, timestamp, from string, isForwarded bool, requester request_client.RequestClient) BaseMessageEvent {
return BaseMessageEvent{
MessageId: messageId,
Context: MessageContext{
Expand Down

0 comments on commit c3374a1

Please sign in to comment.