Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 25 additions & 12 deletions message/message.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
package message

type Envelope struct {
MessagingProduct string `json:"messaging_product"`
RecipientType string `json:"recipient_type,omitempty"`
To string `json:"to,omitempty"`
Type string `json:"type,omitempty"`
Text *Text `json:"text,omitempty"`
Image *Image `json:"image,omitempty"`
Interactive *Interactive `json:"interactive,omitempty"`
Document *Document `json:"document,omitempty"`
Sticker *Sticker `json:"sticker,omitempty"`
Status string `json:"status,omitempty"`
MessageID string `json:"message_id,omitempty"`
MessagingProduct string `json:"messaging_product"`
RecipientType string `json:"recipient_type,omitempty"`
To string `json:"to,omitempty"`
Type string `json:"type,omitempty"`
Text *Text `json:"text,omitempty"`
Image *Image `json:"image,omitempty"`
Interactive *Interactive `json:"interactive,omitempty"`
Document *Document `json:"document,omitempty"`
Sticker *Sticker `json:"sticker,omitempty"`
Status string `json:"status,omitempty"`
TypingIndicator *TypingIndicator `json:"typing_indicator,omitempty"`
MessageID string `json:"message_id,omitempty"`
}

type TypingIndicator struct {
Type string `json:"type"`
}

type Sticker struct {
Expand Down Expand Up @@ -163,11 +168,19 @@ func NewInteractiveFlow(to string, opts NewFlowOpts) Envelope {
}
}

func NewMessageRead(messageId string) Envelope {
func NewMessageRead(messageId string, typingIndicator bool) Envelope {
var indicator *TypingIndicator
if typingIndicator {
indicator = &TypingIndicator{
Type: "text",
}
}

return Envelope{
MessagingProduct: "whatsapp",
Status: "read",
MessageID: messageId,
TypingIndicator: indicator,
}
}

Expand Down
58 changes: 58 additions & 0 deletions message/message_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package message

import (
_ "embed"
"encoding/json"
"fmt"
"reflect"
"testing"
)

//go:embed testdata/message_read_with_typing.json
var expectedMessageReadWithTypingJSON string

//go:embed testdata/message_read_without_typing.json
var expectedMessageReadWithoutTypingJSON string

func TestNewMessageRead(t *testing.T) {
tests := []struct {
messageID string
typingIndicator bool
expectedJSON string
}{
{
messageID: "<WHATSAPP_MESSAGE_ID>",
typingIndicator: true,
expectedJSON: expectedMessageReadWithTypingJSON,
},
{
messageID: "<WHATSAPP_MESSAGE_ID>",
typingIndicator: false,
expectedJSON: expectedMessageReadWithoutTypingJSON,
},
}

for _, tt := range tests {
t.Run(fmt.Sprintf("%vTypingIndicator", tt.typingIndicator), func(t *testing.T) {
msg := NewMessageRead(tt.messageID, tt.typingIndicator)
actualJSON, err := json.Marshal(msg)
if err != nil {
t.Fatalf("failed to marshal message: %v", err)
}

var expected, actual map[string]interface{}

if err := json.Unmarshal([]byte(tt.expectedJSON), &expected); err != nil {
t.Fatalf("failed to unmarshal expected JSON: %v", err)
}

if err := json.Unmarshal(actualJSON, &actual); err != nil {
t.Fatalf("failed to unmarshal actual JSON: %v", err)
}

if !reflect.DeepEqual(actual, expected) {
t.Errorf("got %v, want %v", actual, expected)
}
})
}
}
8 changes: 8 additions & 0 deletions message/testdata/message_read_with_typing.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"messaging_product": "whatsapp",
"status": "read",
"message_id": "<WHATSAPP_MESSAGE_ID>",
"typing_indicator": {
"type": "text"
}
}
5 changes: 5 additions & 0 deletions message/testdata/message_read_without_typing.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"messaging_product": "whatsapp",
"status": "read",
"message_id": "<WHATSAPP_MESSAGE_ID>"
}
Loading