Skip to content

Commit

Permalink
feat: list interative message
Browse files Browse the repository at this point in the history
Signed-off-by: sarthakjdev <[email protected]>
  • Loading branch information
sarthakjdev committed May 23, 2024
1 parent fc82396 commit 8f1d539
Show file tree
Hide file tree
Showing 16 changed files with 366 additions and 47 deletions.
76 changes: 56 additions & 20 deletions example-chat-bot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,44 +32,80 @@ func main() {
Text: "Hello, from wapi.go",
})

// if err != nil {
// fmt.Println("error creating text message", err)
// return
// }

// contactMessage, err := wapiComponents.NewContactMessage([]wapiComponents.Contact{
// *wapiComponents.NewContact(wapiComponents.ContactName{
// FormattedName: "Sarthak Jain",
// FirstName: "Sarthak",
// })})

// if err != nil {
// fmt.Println("error creating contact message", err)
// return
// }

// locationMessage, err := wapiComponents.NewLocationMessage(28.7041, 77.1025)

// if err != nil {
// fmt.Println("error creating location message", err)
// return
// }

// reactionMessage, err := wapiComponents.NewReactionMessage(wapiComponents.ReactionMessageParams{
// MessageId: "wamid.HBgMOTE5NjQzNTAwNTQ1FQIAERgSQzVGOTlFMzExQ0VCQTg0MUFCAA==",
// Emoji: "😍",
// })

// if err != nil {
// fmt.Println("error creating reaction message", err)
// return
// }

// send the message
whatsappClient.Message.Send(manager.SendMessageParams{Message: textMessage, PhoneNumber: "919643500545"})
// whatsappClient.Message.Send(manager.SendMessageParams{Message: contactMessage, PhoneNumber: "919643500545"})
// whatsappClient.Message.Send(manager.SendMessageParams{Message: locationMessage, PhoneNumber: "919643500545"})
// whatsappClient.Message.Send(manager.SendMessageParams{Message: reactionMessage, PhoneNumber: "919643500545"})

listMessage, err := wapiComponents.NewListMessage(wapiComponents.ListMessageParams{
ButtonText: "Button 1",
BodyText: "Body 1",
})

if err != nil {
fmt.Println("error creating text message", err)
fmt.Println("error creating list message", err)
return
}

contact := wapiComponents.NewContact(wapiComponents.ContactName{
FormattedName: "Sarthak Jain",
FirstName: "Sarthak",
})

contactMessage, err := wapiComponents.NewContactMessage([]wapiComponents.Contact{*contact})
listSectionRow, err := wapiComponents.NewListSectionRow("1", "Title 1", "Description 1")

if err != nil {
fmt.Println("error creating contact message", err)
fmt.Println("error creating list section row", err)
return
}

locationMessage, err := wapiComponents.NewLocationMessage(28.7041, 77.1025)
listSection, err := wapiComponents.NewListSection("Section1")

if err != nil {
fmt.Println("error creating location message", err)
fmt.Println("error creating list section row", err)
return
}

reactionMessage, err := wapiComponents.NewReactionMessage(wapiComponents.ReactionMessageParams{
MessageId: "wamid.HBgMOTE5NjQzNTAwNTQ1FQIAERgSQzVGOTlFMzExQ0VCQTg0MUFCAA==",
Emoji: "😍",
})
listSection.AddRow(listSectionRow)
listMessage.AddSection(listSection)
jsonData, err := listMessage.ToJson(wapiComponents.ApiCompatibleJsonConverterConfigs{SendToPhoneNumber: "919643500545"})

if err != nil {
fmt.Println("error creating reaction message", err)
fmt.Println("error converting message to json", err)
return
}

// send the message
whatsappClient.Message.Send(manager.SendMessageParams{Message: textMessage, PhoneNumber: "919643500545"})
whatsappClient.Message.Send(manager.SendMessageParams{Message: contactMessage, PhoneNumber: "919643500545"})
whatsappClient.Message.Send(manager.SendMessageParams{Message: locationMessage, PhoneNumber: "919643500545"})
whatsappClient.Message.Send(manager.SendMessageParams{Message: reactionMessage, PhoneNumber: "919643500545"})
fmt.Println(string(jsonData))

whatsappClient.Message.Send(manager.SendMessageParams{Message: listMessage, PhoneNumber: "919643500545"})

}
2 changes: 1 addition & 1 deletion pkg/components/audio_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (audio *AudioMessage) ToJson(configs ApiCompatibleJsonConverterConfigs) ([]
}

jsonData := AudioMessageApiPayload{
BaseMessagePayload: NewBaseMessagePayload(configs.SendToPhoneNumber, AudioMessageType),
BaseMessagePayload: NewBaseMessagePayload(configs.SendToPhoneNumber, MessageTypeAudio),
Audio: *audio,
}

Expand Down
22 changes: 11 additions & 11 deletions pkg/components/base_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ package components
type MessageType string

const (
LocationMessageType MessageType = "location"
AudioMessageType MessageType = "audio"
VideoMessageType MessageType = "video"
DocumentMessageType MessageType = "document"
TextMessageType MessageType = "text"
ContactMessageType MessageType = "contacts"
InteractiveMessageType MessageType = "interactive"
TemplateMessageType MessageType = "template"
ReactionMessageType MessageType = "reaction"
StickerMessageType MessageType = "sticker"
ImageMessageType MessageType = "image"
MessageTypeLocation MessageType = "location"
MessageTypeAudio MessageType = "audio"
MessageTypeVideo MessageType = "video"
MessageTypeDocument MessageType = "document"
MessageTypeText MessageType = "text"
MessageTypeContact MessageType = "contacts"
MessageTypeInteractive MessageType = "interactive"
MessageTypeTemplate MessageType = "template"
MessageTypeReaction MessageType = "reaction"
MessageTypeSticker MessageType = "sticker"
MessageTypeImage MessageType = "image"
)

type ApiCompatibleJsonConverterConfigs struct {
Expand Down
2 changes: 1 addition & 1 deletion pkg/components/contact_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func (m *ContactMessage) ToJson(configs ApiCompatibleJsonConverterConfigs) ([]by
return nil, fmt.Errorf("error validating configs: %v", err)
}
jsonData := ContactMessageApiPayload{
BaseMessagePayload: NewBaseMessagePayload(configs.SendToPhoneNumber, ContactMessageType),
BaseMessagePayload: NewBaseMessagePayload(configs.SendToPhoneNumber, MessageTypeContact),
Contacts: m.Contacts,
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/components/document_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (dm *DocumentMessage) ToJson(configs ApiCompatibleJsonConverterConfigs) ([]
}

jsonData := DocumentMessageApiPayload{
BaseMessagePayload: NewBaseMessagePayload(configs.SendToPhoneNumber, DocumentMessageType),
BaseMessagePayload: NewBaseMessagePayload(configs.SendToPhoneNumber, MessageTypeDocument),
Document: *dm,
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/components/image_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (image *ImageMessage) ToJson(configs ApiCompatibleJsonConverterConfigs) ([]
}

jsonData := ImageMessageApiPayload{
BaseMessagePayload: NewBaseMessagePayload(configs.SendToPhoneNumber, ImageMessageType),
BaseMessagePayload: NewBaseMessagePayload(configs.SendToPhoneNumber, MessageTypeImage),
Image: *image,
}

Expand Down
10 changes: 8 additions & 2 deletions pkg/components/interactive_message.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
package components

type BaseInteractiveMessage struct {
}
type InteractiveMessageType string

const (
InteractiveMessageTypeButton InteractiveMessageType = "button"
InteractiveMessageTypeProduct InteractiveMessageType = "product"
InteractiveMessageTypeProductList InteractiveMessageType = "product_list"
InteractiveMessageTypeList InteractiveMessageType = "list"
)
128 changes: 128 additions & 0 deletions pkg/components/list_message.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package components

import (
"encoding/json"
"fmt"

"github.com/sarthakjdev/wapi.go/utils"
)

type listSection struct {
Title string `json:"title" validate:"required"`
Rows []listSectionRow `json:"rows" validate:"required"`
}

func NewListSection(title string) (*listSection, error) {
return &listSection{
Title: title,
}, nil
}

type listSectionRow struct {
Id string `json:"id" validate:"required"`
Description string `json:"description" validate:"required"`
Title string `json:"title" validate:"required"`
}

func (section *listSection) AddRow(row *listSectionRow) {
section.Rows = append(section.Rows, *row)
}

func (section *listSection) SetTitle(title string) {
section.Title = title
}

func NewListSectionRow(id, title, description string) (*listSectionRow, error) {
return &listSectionRow{
Id: id,
Description: description,
Title: title,
}, nil
}

func (row *listSectionRow) SetTitle(title string) {
row.Title = title
}

func (row *listSectionRow) SetDescription(description string) {
row.Description = description
}

func (row *listSectionRow) SetId(id string) {
row.Id = id
}

type listMessageAction struct {
ButtonText string `json:"button" validate:"required"`
Sections []listSection `json:"sections" validate:"required"`
}

type ListMessageBody struct {
Text string `json:"text" validate:"required"`
}

type listMessage struct {
Type InteractiveMessageType `json:"type" validate:"required"`
Action listMessageAction `json:"action" validate:"required"`
Body ListMessageBody `json:"body,omitempty"`
}

type ListMessageParams struct {
ButtonText string `json:"-" validate:"required"`
BodyText string `json:"-" validate:"required`
}

func NewListMessage(params ListMessageParams) (*listMessage, error) {
if err := utils.GetValidator().Struct(params); err != nil {
return nil, fmt.Errorf("error validating configs: %v", err)
}

return &listMessage{
Type: InteractiveMessageTypeList,
Body: ListMessageBody{
Text: params.BodyText,
},
Action: listMessageAction{
ButtonText: params.ButtonText,
},
}, nil
}

type ListMessageApiPayload struct {
BaseMessagePayload
Interactive listMessage `json:"interactive" validate:"required"`
}

func (m *listMessage) AddSection(section *listSection) {
m.Action.Sections = append(m.Action.Sections, *section)
}

func (m *listMessage) SetBodyText(section *listSection) {
m.Body.Text = section.Title
}

func (m *listMessage) ToJson(configs ApiCompatibleJsonConverterConfigs) ([]byte, error) {
if err := utils.GetValidator().Struct(configs); err != nil {
return nil, fmt.Errorf("error validating configs: %v", err)
}

jsonData := ListMessageApiPayload{
BaseMessagePayload: NewBaseMessagePayload(configs.SendToPhoneNumber, MessageTypeInteractive),
Interactive: *m,
}

if configs.ReplyToMessageId != "" {
jsonData.Context = &Context{
MessageId: configs.ReplyToMessageId,
}
}

jsonToReturn, err := json.Marshal(jsonData)

if err != nil {
return nil, fmt.Errorf("error marshalling json: %v", err)
}

return jsonToReturn, nil

}
2 changes: 1 addition & 1 deletion pkg/components/location_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (location *LocationMessage) ToJson(configs ApiCompatibleJsonConverterConfig
}

jsonData := LocationMessageApiPayload{
BaseMessagePayload: NewBaseMessagePayload(configs.SendToPhoneNumber, LocationMessageType),
BaseMessagePayload: NewBaseMessagePayload(configs.SendToPhoneNumber, MessageTypeLocation),
Location: *location,
}

Expand Down
53 changes: 53 additions & 0 deletions pkg/components/product_list_message.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package components

import (
"encoding/json"
"fmt"

"github.com/sarthakjdev/wapi.go/utils"
)

type ProductListMessage struct {
}

type ProductListMessageParams struct {
}

type ProductListMessageApiPayload struct {
BaseMessagePayload
Interactive ProductListMessage `json:"interactive" validate:"required"`
}

func NewProductListMessage(params ProductListMessageParams) (*ProductListMessage, error) {
if err := utils.GetValidator().Struct(params); err != nil {
return nil, fmt.Errorf("error validating configs: %v", err)
}

return &ProductListMessage{}, nil
}

func (m *ProductListMessage) ToJson(configs ApiCompatibleJsonConverterConfigs) ([]byte, error) {
if err := utils.GetValidator().Struct(configs); err != nil {
return nil, fmt.Errorf("error validating configs: %v", err)
}

jsonData := ProductListMessageApiPayload{
BaseMessagePayload: NewBaseMessagePayload(configs.SendToPhoneNumber, MessageTypeInteractive),
Interactive: *m,
}

if configs.ReplyToMessageId != "" {
jsonData.Context = &Context{
MessageId: configs.ReplyToMessageId,
}
}

jsonToReturn, err := json.Marshal(jsonData)

if err != nil {
return nil, fmt.Errorf("error marshalling json: %v", err)
}

return jsonToReturn, nil

}
Loading

0 comments on commit 8f1d539

Please sign in to comment.