Skip to content

Commit a13afd4

Browse files
committed
feat: add support for message reading & sending typing indicator
Signed-off-by: sarthakjdev <jsarthak448@gmail.com>
1 parent 749184e commit a13afd4

1 file changed

Lines changed: 63 additions & 0 deletions

File tree

manager/message_manager.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ type MessageSendError struct {
5050
FbtraceID string `json:"fbtrace_id"`
5151
}
5252

53+
// StatusResponse represents the API response for status updates (read receipts).
54+
type StatusResponse struct {
55+
Success bool `json:"success"`
56+
Error *MessageSendError `json:"error,omitempty"`
57+
}
58+
5359
// Reply sends a reply message using the provided BaseMessage and returns a structured response.
5460
// If the API response contains an error, it returns that error.
5561
func (mm *MessageManager) Reply(message components.BaseMessage, phoneNumber string, replyTo string) (*MessageSendResponse, error) {
@@ -117,3 +123,60 @@ func (mm *MessageManager) Send(message components.BaseMessage, phoneNumber strin
117123

118124
return &sendResponse, nil
119125
}
126+
127+
// ReadMessage marks a message as read.
128+
// messageId: The ID of the message to mark as read
129+
// showTyping: Whether to show typing indicator (will auto-dismiss after 25 seconds or when you respond)
130+
func (mm *MessageManager) readMessage(messageId string, showTyping bool) error {
131+
// Create the request body for marking message as read
132+
requestBody := map[string]interface{}{
133+
"messaging_product": "whatsapp",
134+
"status": "read",
135+
"message_id": messageId,
136+
}
137+
138+
// Add typing indicator if requested
139+
if showTyping {
140+
requestBody["typing_indicator"] = map[string]string{
141+
"type": "text",
142+
}
143+
}
144+
145+
body, err := json.Marshal(requestBody)
146+
if err != nil {
147+
return fmt.Errorf("error marshalling read request body: %v", err)
148+
}
149+
150+
// Build the API request
151+
apiRequest := mm.requester.NewApiRequest(strings.Join([]string{mm.PhoneNumberId, "messages"}, "/"), http.MethodPost)
152+
apiRequest.SetBody(string(body))
153+
responseStr, err := apiRequest.Execute()
154+
if err != nil {
155+
return fmt.Errorf("error executing read message request: %v", err)
156+
}
157+
158+
// Parse the response to check for errors
159+
var statusResponse StatusResponse
160+
err = json.Unmarshal([]byte(responseStr), &statusResponse)
161+
if err != nil {
162+
return fmt.Errorf("error unmarshalling read response: %v", err)
163+
}
164+
165+
if statusResponse.Error != nil {
166+
return fmt.Errorf("error marking message as read: %s", statusResponse.Error.Message)
167+
}
168+
169+
return nil
170+
}
171+
172+
// ReadMessageWithTyping marks a message as read and shows typing indicator.
173+
// This is a convenience method for ReadMessage(messageId, true).
174+
func (mm *MessageManager) ReadMessageWithTyping(messageId string) error {
175+
return mm.readMessage(messageId, true)
176+
}
177+
178+
// ReadMessageOnly marks a message as read without showing typing indicator.
179+
// This is a convenience method for ReadMessage(messageId, false).
180+
func (mm *MessageManager) ReadMessageOnly(messageId string) error {
181+
return mm.readMessage(messageId, false)
182+
}

0 commit comments

Comments
 (0)