Skip to content

Commit

Permalink
feat: add quick reply button component
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 12dd5fe commit 570a1ab
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 3 deletions.
12 changes: 12 additions & 0 deletions example-chat-bot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,16 @@ func main() {

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

buttonMessage, err := wapiComponents.NewQuickReplyButtonMessage("Body 1")

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

buttonMessage.AddButton("1", "Button 1")
buttonMessage.AddButton("2", "Button 2")

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

}
65 changes: 62 additions & 3 deletions pkg/components/quick_reply_button_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,76 @@ import (
"github.com/sarthakjdev/wapi.go/utils"
)

type QuickReplyButtonMessage struct {
// quickReplyButtonMessageButtonReply represents the reply structure of a quick reply button.
type quickReplyButtonMessageButtonReply struct {
Title string `json:"title" validate:"required"` // Title of the quick reply button.
Id string `json:"id" validate:"required"` // ID of the quick reply button.
}

// quickReplyButtonMessageButton represents a quick reply button.
type quickReplyButtonMessageButton struct {
Type string `json:"type" validate:"required"` // Type of the quick reply button.
Reply quickReplyButtonMessageButtonReply `json:"reply" validate:"required"` // Reply structure of the quick reply button.
}

// NewQuickReplyButton creates a new quick reply button with the given ID and title.
func NewQuickReplyButton(id, title string) (*quickReplyButtonMessageButton, error) {
return &quickReplyButtonMessageButton{
Type: "reply",
Reply: quickReplyButtonMessageButtonReply{
Title: title,
Id: id,
},
}, nil
}

type QuickReplyButtonMessageParams struct {
// QuickReplyButtonMessageAction represents the action of a quick reply button message.
type QuickReplyButtonMessageAction struct {
Buttons []quickReplyButtonMessageButton `json:"buttons" validate:"required"` // List of quick reply buttons.
}

// QuickReplyButtonMessageBody represents the body of a quick reply button message.
type QuickReplyButtonMessageBody struct {
Text string `json:"text" validate:"required"` // Text of the quick reply button message.
}

// QuickReplyButtonMessage represents a quick reply button message.
type QuickReplyButtonMessage struct {
Type InteractiveMessageType `json:"type" validate:"required"` // Type of the quick reply button message.
Body QuickReplyButtonMessageBody `json:"body" validate:"required"` // Body of the quick reply button message.
Action QuickReplyButtonMessageAction `json:"action" validate:"required"` // Action of the quick reply button message.
}

// QuickReplyButtonMessageApiPayload represents the API payload for a quick reply button message.
type QuickReplyButtonMessageApiPayload struct {
BaseMessagePayload
Interactive QuickReplyButtonMessage `json:"interactive" validate:"required"`
Interactive QuickReplyButtonMessage `json:"interactive" validate:"required"` // Interactive part of the API payload.
}

// NewQuickReplyButtonMessage creates a new quick reply button message with the given body text.
func NewQuickReplyButtonMessage(bodyText string) (*QuickReplyButtonMessage, error) {
return &QuickReplyButtonMessage{
Type: InteractiveMessageTypeButton,
Body: QuickReplyButtonMessageBody{
Text: bodyText,
},
Action: QuickReplyButtonMessageAction{
Buttons: []quickReplyButtonMessageButton{},
},
}, nil
}

func (m *QuickReplyButtonMessage) AddButton(id, title string) error {
button, err := NewQuickReplyButton(id, title)
if err != nil {
return fmt.Errorf("error creating quick reply button: %v", err)
}
m.Action.Buttons = append(m.Action.Buttons, *button)

return nil
}

// ToJson converts the quick reply button message to JSON.
func (m *QuickReplyButtonMessage) ToJson(configs ApiCompatibleJsonConverterConfigs) ([]byte, error) {
if err := utils.GetValidator().Struct(configs); err != nil {
return nil, fmt.Errorf("error validating configs: %v", err)
Expand Down

0 comments on commit 570a1ab

Please sign in to comment.