diff --git a/message/message.go b/message/message.go index 3d42a92..c368cf3 100644 --- a/message/message.go +++ b/message/message.go @@ -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 { @@ -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, } } diff --git a/message/message_test.go b/message/message_test.go new file mode 100644 index 0000000..5c2584a --- /dev/null +++ b/message/message_test.go @@ -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: "", + typingIndicator: true, + expectedJSON: expectedMessageReadWithTypingJSON, + }, + { + messageID: "", + 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) + } + }) + } +} diff --git a/message/testdata/message_read_with_typing.json b/message/testdata/message_read_with_typing.json new file mode 100644 index 0000000..5f767ec --- /dev/null +++ b/message/testdata/message_read_with_typing.json @@ -0,0 +1,8 @@ +{ + "messaging_product": "whatsapp", + "status": "read", + "message_id": "", + "typing_indicator": { + "type": "text" + } +} diff --git a/message/testdata/message_read_without_typing.json b/message/testdata/message_read_without_typing.json new file mode 100644 index 0000000..a76828a --- /dev/null +++ b/message/testdata/message_read_without_typing.json @@ -0,0 +1,5 @@ +{ + "messaging_product": "whatsapp", + "status": "read", + "message_id": "" +}