From e4341fc58caf576da70ef6f085e355d985c7801c Mon Sep 17 00:00:00 2001 From: Dmitriy Sviridov Date: Thu, 22 Jul 2021 22:27:00 +0300 Subject: [PATCH] WhatsApp bot --- sendpulse/bots_fb_service.go | 2 +- sendpulse/bots_fb_service_test.go | 4 +- sendpulse/bots_service.go | 2 + sendpulse/bots_telegram_service.go | 2 +- sendpulse/bots_telegram_service_test.go | 4 +- sendpulse/bots_vk_service.go | 2 +- sendpulse/bots_vk_service_test.go | 4 +- sendpulse/bots_whatsapp_service.go | 933 ++++++++++++++++++++++++ sendpulse/bots_whatsapp_service_test.go | 805 ++++++++++++++++++++ 9 files changed, 1749 insertions(+), 9 deletions(-) create mode 100644 sendpulse/bots_whatsapp_service.go create mode 100644 sendpulse/bots_whatsapp_service_test.go diff --git a/sendpulse/bots_fb_service.go b/sendpulse/bots_fb_service.go index f7981cc..d84c909 100644 --- a/sendpulse/bots_fb_service.go +++ b/sendpulse/bots_fb_service.go @@ -155,7 +155,7 @@ type FbBotSendTextParams struct { Text string `json:"text"` } -func (service *BotsFbService) SendTextToContact(params FbBotSendTextParams) error { +func (service *BotsFbService) SendTextByContact(params FbBotSendTextParams) error { path := "/messenger/contacts/sendText" var respData struct { diff --git a/sendpulse/bots_fb_service_test.go b/sendpulse/bots_fb_service_test.go index f484d5c..581fef1 100644 --- a/sendpulse/bots_fb_service_test.go +++ b/sendpulse/bots_fb_service_test.go @@ -186,7 +186,7 @@ func (suite *SendpulseTestSuite) TestBotsFbService_GetContactsByVariable() { suite.Equal("1234", contacts[0].ID) } -func (suite *SendpulseTestSuite) TestBotsFbService_SendTextToContact() { +func (suite *SendpulseTestSuite) TestBotsFbService_SendTextByContact() { suite.mux.HandleFunc("/messenger/contacts/sendText", func(w http.ResponseWriter, r *http.Request) { suite.Equal(http.MethodPost, r.Method) @@ -195,7 +195,7 @@ func (suite *SendpulseTestSuite) TestBotsFbService_SendTextToContact() { }`) }) - err := suite.client.Bots.Fb.SendTextToContact(FbBotSendTextParams{ + err := suite.client.Bots.Fb.SendTextByContact(FbBotSendTextParams{ ContactID: "qwe12345", MessageType: "RESPONSE", MessageTag: "ACCOUNT_UPDATE", diff --git a/sendpulse/bots_service.go b/sendpulse/bots_service.go index 082be26..7add1b3 100644 --- a/sendpulse/bots_service.go +++ b/sendpulse/bots_service.go @@ -5,6 +5,7 @@ type BotsService struct { Fb *BotsFbService Vk *BotsVkService Telegram *BotsTelegramService + WhatsApp *BotsWhatsAppService } func newBotsService(cl *Client) *BotsService { @@ -13,5 +14,6 @@ func newBotsService(cl *Client) *BotsService { Fb: newBotsFbService(cl), Vk: newBotsVkService(cl), Telegram: newBotsTelegramService(cl), + WhatsApp: newBotsWhatsAppService(cl), } } diff --git a/sendpulse/bots_telegram_service.go b/sendpulse/bots_telegram_service.go index 2a52b69..72a239f 100644 --- a/sendpulse/bots_telegram_service.go +++ b/sendpulse/bots_telegram_service.go @@ -145,7 +145,7 @@ func (service *BotsTelegramService) GetContactsByVariable(params BotContactsByVa return respData.Data, err } -func (service *BotsTelegramService) SendTextToContact(contactID string, text string) error { +func (service *BotsTelegramService) SendTextByContact(contactID string, text string) error { path := "/telegram/contacts/sendText" type bodyFormat struct { diff --git a/sendpulse/bots_telegram_service_test.go b/sendpulse/bots_telegram_service_test.go index c105d98..7940789 100644 --- a/sendpulse/bots_telegram_service_test.go +++ b/sendpulse/bots_telegram_service_test.go @@ -189,7 +189,7 @@ func (suite *SendpulseTestSuite) TestBotsTelegramService_GetContactsByVariable() suite.Equal("1234", contacts[0].ID) } -func (suite *SendpulseTestSuite) TestBotsTelegramService_SendTextToContact() { +func (suite *SendpulseTestSuite) TestBotsTelegramService_SendTextByContact() { suite.mux.HandleFunc("/telegram/contacts/sendText", func(w http.ResponseWriter, r *http.Request) { suite.Equal(http.MethodPost, r.Method) @@ -198,7 +198,7 @@ func (suite *SendpulseTestSuite) TestBotsTelegramService_SendTextToContact() { }`) }) - err := suite.client.Bots.Telegram.SendTextToContact("qwe12345", "hello") + err := suite.client.Bots.Telegram.SendTextByContact("qwe12345", "hello") suite.NoError(err) } diff --git a/sendpulse/bots_vk_service.go b/sendpulse/bots_vk_service.go index eb2e57d..afc79d5 100644 --- a/sendpulse/bots_vk_service.go +++ b/sendpulse/bots_vk_service.go @@ -136,7 +136,7 @@ func (service *BotsVkService) GetContactsByVariable(params BotContactsByVariable return respData.Data, err } -func (service *BotsVkService) SendTextToContact(contactID string, text string) error { +func (service *BotsVkService) SendTextByContact(contactID string, text string) error { path := "/vk/contacts/sendText" type bodyFormat struct { diff --git a/sendpulse/bots_vk_service_test.go b/sendpulse/bots_vk_service_test.go index 192733a..c510091 100644 --- a/sendpulse/bots_vk_service_test.go +++ b/sendpulse/bots_vk_service_test.go @@ -177,7 +177,7 @@ func (suite *SendpulseTestSuite) TestBotsVkService_GetContactsByVariable() { suite.Equal("1234", contacts[0].ID) } -func (suite *SendpulseTestSuite) TestBotsVkService_SendTextToContact() { +func (suite *SendpulseTestSuite) TestBotsVkService_SendTextByContact() { suite.mux.HandleFunc("/vk/contacts/sendText", func(w http.ResponseWriter, r *http.Request) { suite.Equal(http.MethodPost, r.Method) @@ -186,7 +186,7 @@ func (suite *SendpulseTestSuite) TestBotsVkService_SendTextToContact() { }`) }) - err := suite.client.Bots.Vk.SendTextToContact("qwe12345", "hello") + err := suite.client.Bots.Vk.SendTextByContact("qwe12345", "hello") suite.NoError(err) } diff --git a/sendpulse/bots_whatsapp_service.go b/sendpulse/bots_whatsapp_service.go new file mode 100644 index 0000000..9a81c1f --- /dev/null +++ b/sendpulse/bots_whatsapp_service.go @@ -0,0 +1,933 @@ +package sendpulse + +import ( + "fmt" + "net/http" + "net/url" + "time" +) + +type BotsWhatsAppService struct { + client *Client +} + +func newBotsWhatsAppService(cl *Client) *BotsWhatsAppService { + return &BotsWhatsAppService{client: cl} +} + +type WhatsAppAccount struct { + Plan struct { + Branding bool `json:"branding"` + MaxBots int `json:"max_bots"` + MaxContacts int `json:"max_contacts"` + MaxMessages int `json:"max_messages"` + MaxTags int `json:"max_tags"` + MaxVariables int `json:"max_variables"` + Code string `json:"code"` + IsExceeded bool `json:"is_exceeded"` + IsExpired bool `json:"is_expired"` + ExpiredAt time.Time `json:"expired_at"` + } `json:"plan"` + Statistics struct { + Messages int `json:"messages"` + Bots int `json:"bots"` + Contacts int `json:"contacts"` + Variables int `json:"variables"` + } `json:"statistics"` +} + +func (service *BotsWhatsAppService) GetAccount() (*WhatsAppAccount, error) { + path := "/whatsapp/account" + + var respData struct { + Success bool `json:"success"` + Data *WhatsAppAccount `json:"data"` + } + _, err := service.client.newRequest(http.MethodGet, path, nil, &respData, true) + return respData.Data, err +} + +type WhatsAppBot struct { + ID string `json:"id"` + ChannelData struct { + Name string `json:"name"` + Phone string `json:"phone"` + } `json:"channel_data"` + Inbox struct { + Total int `json:"total"` + Unread int `json:"unread"` + } `json:"inbox"` + Status int `json:"status"` + CreatedAt time.Time `json:"created_at"` +} + +func (service *BotsWhatsAppService) GetBots() ([]*WhatsAppBot, error) { + path := "/whatsapp/bots" + + var respData struct { + Success bool `json:"success"` + Data []*WhatsAppBot `json:"data"` + } + _, err := service.client.newRequest(http.MethodGet, path, nil, &respData, true) + return respData.Data, err +} + +type WhatsAppBotContact struct { + ID string `json:"id"` + BotID string `json:"bot_id"` + Status int `json:"status"` + ChannelData struct { + UserName string `json:"username"` + FirstName string `json:"first_name"` + LastName string `json:"last_name"` + Name string `json:"name"` + LanguageCode string `json:"language_code"` + } `json:"channel_data"` + Tags []string `json:"tags"` + Variables map[string]interface{} `json:"variables"` + IsChatOpened bool `json:"is_chat_opened"` + LastActivityAt time.Time `json:"last_activity_at"` + AutomationPausedUntil time.Time `json:"automation_paused_until"` + CreatedAt time.Time `json:"created_at"` +} + +type WhatsAppMessage struct { + Type string `json:"type"` + Text *struct { + Body string `json:"body"` + } `json:"text,omitempty"` + Image *struct { + Link string `json:"link"` + Caption string `json:"caption"` + } `json:"image,omitempty"` + Document *struct { + Link string `json:"link"` + Caption string `json:"caption"` + } `json:"document,omitempty"` +} + +func (service *BotsWhatsAppService) CreateContact(botID, phone, name string) (*WhatsAppBotContact, error) { + path := "/whatsapp/contacts" + + type bodyFormat struct { + Phone string `json:"phone"` + Name string `json:"name"` + BotID string `json:"bot_id"` + } + body := bodyFormat{ + Phone: phone, + Name: name, + BotID: botID, + } + + var respData struct { + Success bool `json:"success"` + Data *WhatsAppBotContact `json:"data"` + } + _, err := service.client.newRequest(http.MethodPost, path, body, &respData, true) + return respData.Data, err +} + +func (service *BotsWhatsAppService) GetContact(contactID string) (*WhatsAppBotContact, error) { + path := fmt.Sprintf("/whatsapp/contacts/get?id=%s", contactID) + + var respData struct { + Success bool `json:"success"` + Data *WhatsAppBotContact `json:"data"` + } + _, err := service.client.newRequest(http.MethodGet, path, nil, &respData, true) + return respData.Data, err +} + +func (service *BotsWhatsAppService) GetContactsByPhone(phone, botID string) ([]*WhatsAppBotContact, error) { + path := fmt.Sprintf("/whatsapp/contacts/getByPhone?tag=%s&bot_id=%s", phone, botID) + + var respData struct { + Success bool `json:"success"` + Data []*WhatsAppBotContact `json:"data"` + } + _, err := service.client.newRequest(http.MethodGet, path, nil, &respData, true) + return respData.Data, err +} + +func (service *BotsWhatsAppService) GetContactsByTag(tag, botID string) ([]*WhatsAppBotContact, error) { + path := fmt.Sprintf("/whatsapp/contacts/getByTag?tag=%s&bot_id=%s", tag, botID) + + var respData struct { + Success bool `json:"success"` + Data []*WhatsAppBotContact `json:"data"` + } + _, err := service.client.newRequest(http.MethodGet, path, nil, &respData, true) + return respData.Data, err +} + +func (service *BotsWhatsAppService) GetContactsByVariable(params BotContactsByVariableParams) ([]*WhatsAppBotContact, error) { + urlParams := url.Values{} + urlParams.Add("variable_value", params.VariableValue) + if params.VariableID != "" { + urlParams.Add("variable_id", params.VariableID) + } + if params.VariableName != "" { + urlParams.Add("variable_name", params.VariableName) + } + if params.BotID != "" { + urlParams.Add("bot_id", params.BotID) + } + path := "/whatsapp/contacts/getByVariable?" + urlParams.Encode() + + var respData struct { + Success bool `json:"success"` + Data []*WhatsAppBotContact `json:"data"` + } + _, err := service.client.newRequest(http.MethodGet, path, nil, &respData, true) + return respData.Data, err +} + +func (service *BotsWhatsAppService) SendByContact(contactID string, message *WhatsAppMessage) error { + path := "/whatsapp/contacts/send" + + type bodyFormat struct { + ContactID string `json:"contact_id"` + Message *WhatsAppMessage `json:"message"` + } + body := bodyFormat{ + ContactID: contactID, + Message: message, + } + + var respData struct { + Success bool `json:"success"` + } + _, err := service.client.newRequest(http.MethodPost, path, body, &respData, true) + return err +} + +func (service *BotsWhatsAppService) SendByPhone(botID, phone string, message *WhatsAppMessage) error { + path := "/whatsapp/contacts/sendByPhone" + + type bodyFormat struct { + BotID string `json:"bot_id"` + Phone string `json:"phone"` + Message *WhatsAppMessage `json:"message"` + } + body := bodyFormat{ + BotID: botID, + Phone: phone, + Message: message, + } + + var respData struct { + Success bool `json:"success"` + } + _, err := service.client.newRequest(http.MethodPost, path, body, &respData, true) + return err +} + +func (service *BotsWhatsAppService) SendTemplate(contactID, templateName, languageCode string) error { + path := "/whatsapp/contacts/sendTemplate" + + type bodyFormat struct { + ContactID string `json:"contact_id"` + Template struct { + Name string `json:"name"` + Language struct { + Code string `json:"code"` + } `json:"language"` + } `json:"template"` + } + body := bodyFormat{ + ContactID: contactID, + Template: struct { + Name string `json:"name"` + Language struct { + Code string `json:"code"` + } `json:"language"` + }{ + Name: templateName, + Language: struct { + Code string `json:"code"` + }{ + Code: languageCode, + }, + }, + } + + var respData struct { + Success bool `json:"success"` + } + _, err := service.client.newRequest(http.MethodPost, path, body, &respData, true) + return err +} + +func (service *BotsWhatsAppService) SendTemplateWithVariables(contactID, templateName, languageCode string, variables []string) error { + path := "/whatsapp/contacts/sendTemplate" + + type bodyComponentVariableFormat struct { + Type string `json:"type"` + Text string `json:"text"` + } + + type bodyComponentFormat struct { + Type string `json:"type"` + Parameters []bodyComponentVariableFormat `json:"parameters"` + } + + type bodyFormat struct { + ContactID string `json:"contact_id"` + Template struct { + Name string `json:"name"` + Language struct { + Code string `json:"code"` + } `json:"language"` + } `json:"template"` + Components []bodyComponentFormat `json:"components"` + } + bodyVariables := make([]bodyComponentVariableFormat, len(variables)) + for i, v := range variables { + bodyVariables[i] = bodyComponentVariableFormat{ + Type: "text", + Text: v, + } + } + + components := make([]bodyComponentFormat, 1) + components[0] = bodyComponentFormat{ + Type: "body", + Parameters: bodyVariables, + } + + body := bodyFormat{ + ContactID: contactID, + Template: struct { + Name string `json:"name"` + Language struct { + Code string `json:"code"` + } `json:"language"` + }{ + Name: templateName, + Language: struct { + Code string `json:"code"` + }{ + Code: languageCode, + }, + }, + Components: components, + } + + var respData struct { + Success bool `json:"success"` + } + _, err := service.client.newRequest(http.MethodPost, path, body, &respData, true) + return err +} + +func (service *BotsWhatsAppService) SendTemplateWithImage(contactID, templateName, languageCode, imageLink string) error { + path := "/whatsapp/contacts/sendTemplate" + + type bodyComponentImageFormat struct { + Type string `json:"type"` + Image struct { + Link string `json:"link"` + } `json:"image"` + } + + type bodyComponentFormat struct { + Type string `json:"type"` + Parameters []bodyComponentImageFormat `json:"parameters"` + } + + type bodyFormat struct { + ContactID string `json:"contact_id"` + Template struct { + Name string `json:"name"` + Language struct { + Code string `json:"code"` + } `json:"language"` + } `json:"template"` + Components []bodyComponentFormat `json:"components"` + } + bodyImages := make([]bodyComponentImageFormat, 1) + bodyImages[0] = bodyComponentImageFormat{ + Type: "image", + Image: struct { + Link string `json:"link"` + }{ + Link: imageLink, + }, + } + + components := make([]bodyComponentFormat, 1) + components[0] = bodyComponentFormat{ + Type: "header", + Parameters: bodyImages, + } + + body := bodyFormat{ + ContactID: contactID, + Template: struct { + Name string `json:"name"` + Language struct { + Code string `json:"code"` + } `json:"language"` + }{ + Name: templateName, + Language: struct { + Code string `json:"code"` + }{ + Code: languageCode, + }, + }, + Components: components, + } + + var respData struct { + Success bool `json:"success"` + } + _, err := service.client.newRequest(http.MethodPost, path, body, &respData, true) + return err +} + +func (service *BotsWhatsAppService) SendTemplateByPhone(botID, phone, templateName, languageCode string) error { + path := "/whatsapp/contacts/sendTemplateByPhone" + + type bodyFormat struct { + BotID string `json:"bot_id"` + Phone string `json:"phone"` + Template struct { + Name string `json:"name"` + Language struct { + Code string `json:"code"` + } `json:"language"` + } `json:"template"` + } + body := bodyFormat{ + BotID: botID, + Phone: phone, + Template: struct { + Name string `json:"name"` + Language struct { + Code string `json:"code"` + } `json:"language"` + }{ + Name: templateName, + Language: struct { + Code string `json:"code"` + }{ + Code: languageCode, + }, + }, + } + + var respData struct { + Success bool `json:"success"` + } + _, err := service.client.newRequest(http.MethodPost, path, body, &respData, true) + return err +} + +func (service *BotsWhatsAppService) SendTemplateByPhoneWithVariables(botID, phone, templateName, languageCode string, variables []string) error { + path := "/whatsapp/contacts/sendTemplateByPhone" + + type bodyComponentVariableFormat struct { + Type string `json:"type"` + Text string `json:"text"` + } + + type bodyComponentFormat struct { + Type string `json:"type"` + Parameters []bodyComponentVariableFormat `json:"parameters"` + } + + type bodyFormat struct { + BotID string `json:"bot_id"` + Phone string `json:"phone"` + Template struct { + Name string `json:"name"` + Language struct { + Code string `json:"code"` + } `json:"language"` + } `json:"template"` + Components []bodyComponentFormat `json:"components"` + } + bodyVariables := make([]bodyComponentVariableFormat, len(variables)) + for i, v := range variables { + bodyVariables[i] = bodyComponentVariableFormat{ + Type: "text", + Text: v, + } + } + + components := make([]bodyComponentFormat, 1) + components[0] = bodyComponentFormat{ + Type: "body", + Parameters: bodyVariables, + } + + body := bodyFormat{ + BotID: botID, + Phone: phone, + Template: struct { + Name string `json:"name"` + Language struct { + Code string `json:"code"` + } `json:"language"` + }{ + Name: templateName, + Language: struct { + Code string `json:"code"` + }{ + Code: languageCode, + }, + }, + Components: components, + } + + var respData struct { + Success bool `json:"success"` + } + _, err := service.client.newRequest(http.MethodPost, path, body, &respData, true) + return err +} + +func (service *BotsWhatsAppService) SendTemplateByPhoneWithImage(botID, phone, templateName, languageCode, imageLink string) error { + path := "/whatsapp/contacts/sendTemplateByPhone" + + type bodyComponentImageFormat struct { + Type string `json:"type"` + Image struct { + Link string `json:"link"` + } `json:"image"` + } + + type bodyComponentFormat struct { + Type string `json:"type"` + Parameters []bodyComponentImageFormat `json:"parameters"` + } + + type bodyFormat struct { + BotID string `json:"bot_id"` + Phone string `json:"phone"` + Template struct { + Name string `json:"name"` + Language struct { + Code string `json:"code"` + } `json:"language"` + } `json:"template"` + Components []bodyComponentFormat `json:"components"` + } + bodyImages := make([]bodyComponentImageFormat, 1) + bodyImages[0] = bodyComponentImageFormat{ + Type: "image", + Image: struct { + Link string `json:"link"` + }{ + Link: imageLink, + }, + } + + components := make([]bodyComponentFormat, 1) + components[0] = bodyComponentFormat{ + Type: "header", + Parameters: bodyImages, + } + + body := bodyFormat{ + BotID: botID, + Phone: phone, + Template: struct { + Name string `json:"name"` + Language struct { + Code string `json:"code"` + } `json:"language"` + }{ + Name: templateName, + Language: struct { + Code string `json:"code"` + }{ + Code: languageCode, + }, + }, + Components: components, + } + + var respData struct { + Success bool `json:"success"` + } + _, err := service.client.newRequest(http.MethodPost, path, body, &respData, true) + return err +} + +func (service *BotsWhatsAppService) SetVariableToContact(contactID string, variableID string, variableName string, variableValue interface{}) error { + path := "/whatsapp/contacts/setVariable" + + type bodyFormat struct { + ContactID string `json:"contact_id"` + VariableID string `json:"variable_id,omitempty"` + VariableName string `json:"variable_name,omitempty"` + VariableValue interface{} `json:"variable_value"` + } + body := bodyFormat{ + ContactID: contactID, + VariableID: variableID, + VariableName: variableName, + VariableValue: variableValue, + } + + var respData struct { + Success bool `json:"success"` + } + _, err := service.client.newRequest(http.MethodPost, path, body, &respData, true) + return err +} + +func (service *BotsWhatsAppService) SetTagsToContact(contactID string, tags []string) error { + path := "/whatsapp/contacts/setTag" + + type bodyFormat struct { + ContactID string `json:"contact_id"` + Tags []string `json:"tags"` + } + body := bodyFormat{ + ContactID: contactID, + Tags: tags, + } + + var respData struct { + Success bool `json:"success"` + } + _, err := service.client.newRequest(http.MethodPost, path, body, &respData, true) + return err +} + +func (service *BotsWhatsAppService) DeleteTagFromContact(contactID string, tag string) error { + path := "/whatsapp/contacts/deleteTag" + + type bodyFormat struct { + ContactID string `json:"contact_id"` + Tag string `json:"tag"` + } + body := bodyFormat{ + ContactID: contactID, + Tag: tag, + } + + var respData struct { + Success bool `json:"success"` + } + _, err := service.client.newRequest(http.MethodPost, path, body, &respData, true) + return err +} + +func (service *BotsWhatsAppService) DisableContact(contactID string) error { + path := "/whatsapp/contacts/disable" + + type bodyFormat struct { + ContactID string `json:"contact_id"` + } + body := bodyFormat{ + ContactID: contactID, + } + + var respData struct { + Success bool `json:"success"` + } + _, err := service.client.newRequest(http.MethodPost, path, body, &respData, true) + return err +} + +func (service *BotsWhatsAppService) EnableContact(contactID string) error { + path := "/whatsapp/contacts/enable" + + type bodyFormat struct { + ContactID string `json:"contact_id"` + } + body := bodyFormat{ + ContactID: contactID, + } + + var respData struct { + Success bool `json:"success"` + } + _, err := service.client.newRequest(http.MethodPost, path, body, &respData, true) + return err +} + +func (service *BotsWhatsAppService) DeleteContact(contactID string) error { + path := "/whatsapp/contacts/delete" + + type bodyFormat struct { + ContactID string `json:"contact_id"` + } + body := bodyFormat{ + ContactID: contactID, + } + + var respData struct { + Success bool `json:"success"` + } + _, err := service.client.newRequest(http.MethodPost, path, body, &respData, true) + return err +} + +func (service *BotsWhatsAppService) GetPauseAutomation(contactID string) (int, error) { + path := fmt.Sprintf("/whatsapp/contacts/getPauseAutomation?contact_id=%s", contactID) + + var respData struct { + Success bool `json:"success"` + Data struct { + Minutes int `json:"minutes"` + } `json:"data"` + } + _, err := service.client.newRequest(http.MethodGet, path, nil, &respData, true) + return respData.Data.Minutes, err +} + +func (service *BotsWhatsAppService) SetPauseAutomation(contactID string, minutes int) error { + path := "/whatsapp/contacts/setPauseAutomation" + type bodyFormat struct { + ContactID string `json:"contact_id"` + Minutes int `json:"minutes"` + } + body := bodyFormat{ + ContactID: contactID, + Minutes: minutes, + } + + var respData struct { + Success bool `json:"success"` + } + _, err := service.client.newRequest(http.MethodPost, path, body, &respData, true) + return err +} + +func (service *BotsWhatsAppService) DeletePauseAutomation(contactID string) error { + path := "/whatsapp/contacts/deletePauseAutomation" + type bodyFormat struct { + ContactID string `json:"contact_id"` + } + body := bodyFormat{ + ContactID: contactID, + } + + var respData struct { + Success bool `json:"success"` + } + _, err := service.client.newRequest(http.MethodPost, path, body, &respData, true) + return err +} + +func (service *BotsWhatsAppService) GetBotVariables(botID string) ([]*BotVariable, error) { + path := fmt.Sprintf("/whatsapp/variables?bot_id=%s", botID) + + var respData struct { + Success bool `json:"success"` + Data []*BotVariable `json:"data"` + } + _, err := service.client.newRequest(http.MethodGet, path, nil, &respData, true) + return respData.Data, err +} + +func (service *BotsWhatsAppService) GetFlows(botID string) ([]*BotFlow, error) { + path := fmt.Sprintf("/whatsapp/flows?bot_id=%s", botID) + + var respData struct { + Success bool `json:"success"` + Data []*BotFlow `json:"data"` + } + _, err := service.client.newRequest(http.MethodGet, path, nil, &respData, true) + return respData.Data, err +} + +func (service *BotsWhatsAppService) RunFlow(contactID, flowID string, externalData map[string]interface{}) error { + path := "/whatsapp/flows/run" + + type bodyFormat struct { + ContactID string `json:"contact_id"` + FlowID string `json:"flow_id"` + ExternalData map[string]interface{} `json:"external_data,omitempty"` + } + body := bodyFormat{ + ContactID: contactID, + FlowID: flowID, + ExternalData: externalData, + } + + var respData struct { + Success bool `json:"success"` + } + _, err := service.client.newRequest(http.MethodPost, path, body, &respData, true) + return err +} + +func (service *BotsWhatsAppService) RunFlowByTrigger(contactID, triggerKeyword string, externalData map[string]interface{}) error { + path := "/whatsapp/flows/runByTrigger" + + type bodyFormat struct { + ContactID string `json:"contact_id"` + TriggerKeyword string `json:"trigger_keyword"` + ExternalData map[string]interface{} `json:"external_data,omitempty"` + } + body := bodyFormat{ + ContactID: contactID, + TriggerKeyword: triggerKeyword, + ExternalData: externalData, + } + + var respData struct { + Success bool `json:"success"` + } + _, err := service.client.newRequest(http.MethodPost, path, body, &respData, true) + return err +} + +func (service *BotsWhatsAppService) GetBotTriggers(botID string) ([]*BotTrigger, error) { + path := fmt.Sprintf("/whatsapp/triggers?bot_id=%s", botID) + + var respData struct { + Success bool `json:"success"` + Data []*BotTrigger `json:"data"` + } + _, err := service.client.newRequest(http.MethodGet, path, nil, &respData, true) + return respData.Data, err +} + +type WhatsAppBotMessage struct { + ID string `json:"id"` + ContactID string `json:"contact_id"` + BotID string `json:"bot_id"` + CampaignID string `json:"campaign_id"` + Data map[string]interface{} `json:"data"` + Direction int `json:"direction"` + Status int `json:"status"` + CreatedAt time.Time `json:"created_at"` +} + +type WhatsAppBotChat struct { + Contact *WhatsAppBotContact `json:"contact"` + InboxLastMessage *FbBotMessage `json:"inbox_last_message"` + InboxUnread int `json:"inbox_unread"` +} + +func (service *BotsWhatsAppService) GetBotChats(botID string) ([]*WhatsAppBotChat, error) { + path := fmt.Sprintf("/whatsapp/chats?bot_id=%s", botID) + + var respData struct { + Success bool `json:"success"` + Data []*WhatsAppBotChat `json:"data"` + } + _, err := service.client.newRequest(http.MethodGet, path, nil, &respData, true) + return respData.Data, err +} + +func (service *BotsWhatsAppService) GetContactMessages(contactID string) ([]*WhatsAppBotMessage, error) { + path := fmt.Sprintf("/whatsapp/chats/messages?contact_id=%s", contactID) + + var respData struct { + Success bool `json:"success"` + Data []*WhatsAppBotMessage `json:"data"` + } + _, err := service.client.newRequest(http.MethodGet, path, nil, &respData, true) + return respData.Data, err +} + +type WhatsAppBotSendCampaignParams struct { + Title string `json:"title"` + BotID string `json:"bot_id"` + SendAt time.Time `json:"send_at"` + Messages []WhatsAppMessage `json:"messages"` +} + +func (service *BotsWhatsAppService) SendCampaign(params WhatsAppBotSendCampaignParams) error { + path := "/whatsapp/campaigns/send" + + var respData struct { + Success bool `json:"success"` + } + _, err := service.client.newRequest(http.MethodPost, path, params, &respData, true) + return err +} + +type WhatsAppBotSendCampaignByTemplateParams struct { + Title string + BotID string + SendAt time.Time + Messages []WhatsAppMessage + TemplateName string + LanguageCode string +} + +func (service *BotsWhatsAppService) SendCampaignByTemplate(params WhatsAppBotSendCampaignByTemplateParams) error { + path := "/whatsapp/campaigns/sendTemplate" + type bodyFormat struct { + Title string `json:"title"` + BotID string `json:"bot_id"` + SendAt DateTimeType `json:"send_at"` + Template struct { + Name string `json:"name"` + Language struct { + Code string `json:"code"` + } `json:"language"` + Components struct { + Type string `json:"type"` + Parameters []WhatsAppMessage `json:"parameters"` + } `json:"components"` + } + } + body := bodyFormat{ + Title: params.Title, + BotID: params.BotID, + Template: struct { + Name string `json:"name"` + Language struct { + Code string `json:"code"` + } `json:"language"` + Components struct { + Type string `json:"type"` + Parameters []WhatsAppMessage `json:"parameters"` + } `json:"components"` + }{ + Name: params.TemplateName, + Language: struct { + Code string `json:"code"` + }{ + Code: params.LanguageCode, + }, + Components: struct { + Type string `json:"type"` + Parameters []WhatsAppMessage `json:"parameters"` + }{ + Type: "header", + Parameters: params.Messages, + }, + }, + } + var respData struct { + Success bool `json:"success"` + } + _, err := service.client.newRequest(http.MethodPost, path, body, &respData, true) + return err +} + +type WhatsAppTemplate struct { + ID string `json:"id"` + BotID string `json:"bot_id"` + Namespace string `json:"namespace"` + Category string `json:"category"` + Components []WhatsAppMessage `json:"components"` + Language string `json:"language"` + Name string `json:"name"` + RejectedReason string `json:"rejected_reason"` + Status string `json:"status"` + CreatedAt time.Time `json:"created_at"` +} + +func (service *BotsWhatsAppService) GetTemplates() ([]*WhatsAppTemplate, error) { + path := "/whatsapp/templates" + + var respData struct { + Success bool `json:"success"` + Data []*WhatsAppTemplate `json:"data"` + } + _, err := service.client.newRequest(http.MethodPost, path, nil, &respData, true) + return respData.Data, err +} diff --git a/sendpulse/bots_whatsapp_service_test.go b/sendpulse/bots_whatsapp_service_test.go new file mode 100644 index 0000000..9bc5c42 --- /dev/null +++ b/sendpulse/bots_whatsapp_service_test.go @@ -0,0 +1,805 @@ +package sendpulse + +import ( + "fmt" + "github.com/bxcodec/faker/v3" + "net/http" + "time" +) + +func (suite *SendpulseTestSuite) TestBotsWhatsAppService_GetAccount() { + suite.mux.HandleFunc("/whatsapp/account", func(w http.ResponseWriter, r *http.Request) { + suite.Equal(http.MethodGet, r.Method) + + fmt.Fprintf(w, `{ + "success": true, + "data": { + "plan": { + "code": "Plan code", + "max_bots": 0, + "max_contacts": -1, + "max_messages": 0, + "max_tags": 0, + "max_variables": 0, + "branding": true, + "is_exceeded": true, + "is_expired": true, + "expired_at": "2020-12-12T00:00:00+03:00" + }, + "statistics": { + "messages": 0, + "bots": 0, + "contacts": 0, + "variables": 0 + } + } + }`) + }) + + account, err := suite.client.Bots.WhatsApp.GetAccount() + suite.NoError(err) + suite.Equal("Plan code", account.Plan.Code) +} + +func (suite *SendpulseTestSuite) TestBotsWhatsAppService_GetBots() { + suite.mux.HandleFunc("/whatsapp/bots", func(w http.ResponseWriter, r *http.Request) { + suite.Equal(http.MethodGet, r.Method) + + fmt.Fprintf(w, `{ + "success": true, + "data": [{ + "id": "12345", + "channel_data": { + "name": "Alex", + "phone": "89221112233" + }, + "inbox": { + "total": 100, + "unread": 20 + }, + "status": 3, + "created_at": "2020-12-12T00:00:00+03:00" + }] + }`) + }) + + bots, err := suite.client.Bots.WhatsApp.GetBots() + suite.NoError(err) + suite.Equal("12345", bots[0].ID) +} + +func (suite *SendpulseTestSuite) TestBotsWhatsAppService_CreateContact() { + suite.mux.HandleFunc("/whatsapp/contacts", func(w http.ResponseWriter, r *http.Request) { + suite.Equal(http.MethodPost, r.Method) + + fmt.Fprintf(w, `{ + "success": true, + "data": { + "id": "12345", + "bot_id": "6789", + "status": 1, + "channel_data": { + "username": "alex1981", + "first_name": "Alex", + "last_name": "Pavlov", + "name": "Aleksey", + "language_code": "ru" + }, + "tags": [ + "tag1", + "tag2" + ], + "variables": {}, + "is_chat_opened": true, + "last_activity_at": "2020-12-12T00:00:00+03:00", + "automation_paused_until": "2020-12-12T00:00:00+03:00", + "created_at": "2020-12-12T00:00:00+03:00" + } + }`) + }) + + contact, err := suite.client.Bots.WhatsApp.CreateContact("6789", "89221112233", "Aleksey") + suite.NoError(err) + suite.Equal("12345", contact.ID) +} + +func (suite *SendpulseTestSuite) TestBotsWhatsAppService_GetContact() { + suite.mux.HandleFunc("/whatsapp/contacts/get", func(w http.ResponseWriter, r *http.Request) { + suite.Equal(http.MethodGet, r.Method) + + fmt.Fprintf(w, `{ + "success": true, + "data": { + "id": "12345", + "bot_id": "6789", + "status": 1, + "channel_data": { + "username": "alex1981", + "first_name": "Alex", + "last_name": "Pavlov", + "name": "Aleksey", + "language_code": "ru" + }, + "tags": [ + "tag1", + "tag2" + ], + "variables": {}, + "is_chat_opened": true, + "last_activity_at": "2020-12-12T00:00:00+03:00", + "automation_paused_until": "2020-12-12T00:00:00+03:00", + "created_at": "2020-12-12T00:00:00+03:00" + } + }`) + }) + + contact, err := suite.client.Bots.WhatsApp.GetContact("1") + suite.NoError(err) + suite.Equal("12345", contact.ID) +} + +func (suite *SendpulseTestSuite) TestBotsWhatsAppService_GetContactsByPhone() { + suite.mux.HandleFunc("/whatsapp/contacts/getByPhone", func(w http.ResponseWriter, r *http.Request) { + suite.Equal(http.MethodGet, r.Method) + + fmt.Fprintf(w, `{ + "success": true, + "data": [{ + "id": "12345", + "bot_id": "6789", + "status": 1, + "channel_data": { + "username": "alex1981", + "first_name": "Alex", + "last_name": "Pavlov", + "name": "Aleksey", + "language_code": "ru" + }, + "tags": [ + "tag1", + "tag2" + ], + "variables": {}, + "is_chat_opened": true, + "last_activity_at": "2020-12-12T00:00:00+03:00", + "automation_paused_until": "2020-12-12T00:00:00+03:00", + "created_at": "2020-12-12T00:00:00+03:00" + }] + }`) + }) + + contacts, err := suite.client.Bots.WhatsApp.GetContactsByPhone("89991112233", "6789") + suite.NoError(err) + suite.Equal("12345", contacts[0].ID) +} + +func (suite *SendpulseTestSuite) TestBotsWhatsAppService_GetContactsByTag() { + suite.mux.HandleFunc("/whatsapp/contacts/getByTag", func(w http.ResponseWriter, r *http.Request) { + suite.Equal(http.MethodGet, r.Method) + + fmt.Fprintf(w, `{ + "success": true, + "data": [{ + "id": "12345", + "bot_id": "6789", + "status": 1, + "channel_data": { + "username": "alex1981", + "first_name": "Alex", + "last_name": "Pavlov", + "name": "Aleksey", + "language_code": "ru" + }, + "tags": [ + "tag1", + "tag2" + ], + "variables": {}, + "is_chat_opened": true, + "last_activity_at": "2020-12-12T00:00:00+03:00", + "automation_paused_until": "2020-12-12T00:00:00+03:00", + "created_at": "2020-12-12T00:00:00+03:00" + }] + }`) + }) + + contacts, err := suite.client.Bots.WhatsApp.GetContactsByTag("tag1", "6789") + suite.NoError(err) + suite.Equal("12345", contacts[0].ID) +} + +func (suite *SendpulseTestSuite) TestBotsWhatsAppService_GetContactsByVariable() { + suite.mux.HandleFunc("/whatsapp/contacts/getByVariable", func(w http.ResponseWriter, r *http.Request) { + suite.Equal(http.MethodGet, r.Method) + + fmt.Fprintf(w, `{ + "success": true, + "data": [{ + "id": "12345", + "bot_id": "6789", + "status": 1, + "channel_data": { + "username": "alex1981", + "first_name": "Alex", + "last_name": "Pavlov", + "name": "Aleksey", + "language_code": "ru" + }, + "tags": [ + "tag1", + "tag2" + ], + "variables": {}, + "is_chat_opened": true, + "last_activity_at": "2020-12-12T00:00:00+03:00", + "automation_paused_until": "2020-12-12T00:00:00+03:00", + "created_at": "2020-12-12T00:00:00+03:00" + }] + }`) + }) + + contacts, err := suite.client.Bots.WhatsApp.GetContactsByVariable(BotContactsByVariableParams{ + VariableName: "name", + VariableValue: "Aleksey", + VariableID: "12345", + BotID: "6789", + }) + suite.NoError(err) + suite.Equal("12345", contacts[0].ID) +} + +func (suite *SendpulseTestSuite) TestBotsWhatsAppService_SendByContact() { + suite.mux.HandleFunc("/whatsapp/contacts/send", func(w http.ResponseWriter, r *http.Request) { + suite.Equal(http.MethodPost, r.Method) + + fmt.Fprintf(w, `{ + "success": true + }`) + }) + + msg := WhatsAppMessage{ + Type: "image", + Image: &struct { + Link string `json:"link"` + Caption string `json:"caption"` + }{ + Link: faker.URL(), + Caption: faker.Word(), + }, + } + err := suite.client.Bots.WhatsApp.SendByContact("12345", &msg) + suite.NoError(err) +} + +func (suite *SendpulseTestSuite) TestBotsWhatsAppService_SendByPhone() { + suite.mux.HandleFunc("/whatsapp/contacts/sendByPhone", func(w http.ResponseWriter, r *http.Request) { + suite.Equal(http.MethodPost, r.Method) + + fmt.Fprintf(w, `{ + "success": true + }`) + }) + + msg := WhatsAppMessage{ + Type: "image", + Image: &struct { + Link string `json:"link"` + Caption string `json:"caption"` + }{ + Link: faker.URL(), + Caption: faker.Word(), + }, + } + err := suite.client.Bots.WhatsApp.SendByPhone("12345", "89221112345", &msg) + suite.NoError(err) +} + +func (suite *SendpulseTestSuite) TestBotsWhatsAppService_SendTemplate() { + suite.mux.HandleFunc("/whatsapp/contacts/sendTemplate", func(w http.ResponseWriter, r *http.Request) { + suite.Equal(http.MethodPost, r.Method) + + fmt.Fprintf(w, `{ + "success": true + }`) + }) + + err := suite.client.Bots.WhatsApp.SendTemplate("12345", "first_template", "ru") + suite.NoError(err) +} + +func (suite *SendpulseTestSuite) TestBotsWhatsAppService_SendTemplateWithVariables() { + suite.mux.HandleFunc("/whatsapp/contacts/sendTemplate", func(w http.ResponseWriter, r *http.Request) { + suite.Equal(http.MethodPost, r.Method) + + fmt.Fprintf(w, `{ + "success": true + }`) + }) + + err := suite.client.Bots.WhatsApp.SendTemplateWithVariables("12345", "first_template", "ru", []string{ + "текст переменной", + "{{last_name}}", + }) + suite.NoError(err) +} + +func (suite *SendpulseTestSuite) TestBotsWhatsAppService_SendTemplateWithImage() { + suite.mux.HandleFunc("/whatsapp/contacts/sendTemplate", func(w http.ResponseWriter, r *http.Request) { + suite.Equal(http.MethodPost, r.Method) + + fmt.Fprintf(w, `{ + "success": true + }`) + }) + + err := suite.client.Bots.WhatsApp.SendTemplateWithImage("12345", "first_template", "ru", faker.Word()) + suite.NoError(err) +} + +func (suite *SendpulseTestSuite) TestBotsWhatsAppService_SendTemplateByPhone() { + suite.mux.HandleFunc("/whatsapp/contacts/sendTemplateByPhone", func(w http.ResponseWriter, r *http.Request) { + suite.Equal(http.MethodPost, r.Method) + + fmt.Fprintf(w, `{ + "success": true + }`) + }) + + err := suite.client.Bots.WhatsApp.SendTemplateByPhone("12345", "89991112233", "first_template", "ru") + suite.NoError(err) +} + +func (suite *SendpulseTestSuite) TestBotsWhatsAppService_SendTemplateByPhoneWithVariables() { + suite.mux.HandleFunc("/whatsapp/contacts/sendTemplateByPhone", func(w http.ResponseWriter, r *http.Request) { + suite.Equal(http.MethodPost, r.Method) + + fmt.Fprintf(w, `{ + "success": true + }`) + }) + + err := suite.client.Bots.WhatsApp.SendTemplateByPhoneWithVariables("12345", + "89991112233", + "first_template", + "ru", + []string{ + "текст переменной", + "{{last_name}}", + }) + suite.NoError(err) +} + +func (suite *SendpulseTestSuite) TestBotsWhatsAppService_SendTemplateByPhoneWithImage() { + suite.mux.HandleFunc("/whatsapp/contacts/sendTemplateByPhone", func(w http.ResponseWriter, r *http.Request) { + suite.Equal(http.MethodPost, r.Method) + + fmt.Fprintf(w, `{ + "success": true + }`) + }) + + err := suite.client.Bots.WhatsApp.SendTemplateByPhoneWithImage("12345", + "89991112233", + "first_template", + "ru", + faker.URL()) + suite.NoError(err) +} + +func (suite *SendpulseTestSuite) TestBotsWhatsAppService_SetVariableToContact() { + suite.mux.HandleFunc("/whatsapp/contacts/setVariable", func(w http.ResponseWriter, r *http.Request) { + suite.Equal(http.MethodPost, r.Method) + + fmt.Fprintf(w, `{ + "success": true + }`) + }) + + err := suite.client.Bots.WhatsApp.SetVariableToContact("contactId", "variableId", "variableName", 123) + suite.NoError(err) +} + +func (suite *SendpulseTestSuite) TestBotsWhatsAppService_SetTagsToContact() { + suite.mux.HandleFunc("/whatsapp/contacts/setTag", func(w http.ResponseWriter, r *http.Request) { + suite.Equal(http.MethodPost, r.Method) + + fmt.Fprintf(w, `{ + "success": true + }`) + }) + + err := suite.client.Bots.WhatsApp.SetTagsToContact("contactId", []string{"tag1", "tag2"}) + suite.NoError(err) +} + +func (suite *SendpulseTestSuite) TestBotsWhatsAppService_DeleteTagFromContact() { + suite.mux.HandleFunc("/whatsapp/contacts/deleteTag", func(w http.ResponseWriter, r *http.Request) { + suite.Equal(http.MethodPost, r.Method) + + fmt.Fprintf(w, `{ + "success": true + }`) + }) + + err := suite.client.Bots.WhatsApp.DeleteTagFromContact("contactId", "tag1") + suite.NoError(err) +} + +func (suite *SendpulseTestSuite) TestBotsWhatsAppService_DisableContact() { + suite.mux.HandleFunc("/whatsapp/contacts/disable", func(w http.ResponseWriter, r *http.Request) { + suite.Equal(http.MethodPost, r.Method) + + fmt.Fprintf(w, `{ + "success": true + }`) + }) + + err := suite.client.Bots.WhatsApp.DisableContact("contactId") + suite.NoError(err) +} + +func (suite *SendpulseTestSuite) TestBotsWhatsAppService_EnableContact() { + suite.mux.HandleFunc("/whatsapp/contacts/enable", func(w http.ResponseWriter, r *http.Request) { + suite.Equal(http.MethodPost, r.Method) + + fmt.Fprintf(w, `{ + "success": true + }`) + }) + + err := suite.client.Bots.WhatsApp.EnableContact("contactId") + suite.NoError(err) +} + +func (suite *SendpulseTestSuite) TestBotsWhatsAppService_DeleteContact() { + suite.mux.HandleFunc("/whatsapp/contacts/delete", func(w http.ResponseWriter, r *http.Request) { + suite.Equal(http.MethodPost, r.Method) + + fmt.Fprintf(w, `{ + "success": true + }`) + }) + + err := suite.client.Bots.WhatsApp.DeleteContact("contactId") + suite.NoError(err) +} + +func (suite *SendpulseTestSuite) TestBotsWhatsAppService_GetPauseAutomation() { + suite.mux.HandleFunc("/whatsapp/contacts/getPauseAutomation", func(w http.ResponseWriter, r *http.Request) { + suite.Equal(http.MethodGet, r.Method) + + fmt.Fprintf(w, `{ + "success": true, + "data": { + "minutes": 123 + } + }`) + }) + + p, err := suite.client.Bots.WhatsApp.GetPauseAutomation("contactId") + suite.NoError(err) + suite.Equal(123, p) +} + +func (suite *SendpulseTestSuite) TestBotsWhatsAppService_SetPauseAutomation() { + suite.mux.HandleFunc("/whatsapp/contacts/setPauseAutomation", func(w http.ResponseWriter, r *http.Request) { + suite.Equal(http.MethodPost, r.Method) + + fmt.Fprintf(w, `{ + "success": true + }`) + }) + + err := suite.client.Bots.WhatsApp.SetPauseAutomation("contactId", 60) + suite.NoError(err) +} + +func (suite *SendpulseTestSuite) TestBotsWhatsAppService_DeletePauseAutomation() { + suite.mux.HandleFunc("/whatsapp/contacts/deletePauseAutomation", func(w http.ResponseWriter, r *http.Request) { + suite.Equal(http.MethodPost, r.Method) + + fmt.Fprintf(w, `{ + "success": true + }`) + }) + + err := suite.client.Bots.WhatsApp.DeletePauseAutomation("contactId") + suite.NoError(err) +} + +func (suite *SendpulseTestSuite) TestBotsWhatsAppService_GetBotVariables() { + suite.mux.HandleFunc("/whatsapp/variables", func(w http.ResponseWriter, r *http.Request) { + suite.Equal(http.MethodGet, r.Method) + + fmt.Fprintf(w, `{ + "success": true, + "data": [{ + "id": "qwerty", + "bot_id": "qwerty", + "name": "Alex", + "description": "Alex qwerty", + "type": 1, + "value_type": 1, + "status": 1, + "created_at": "2020-12-12T00:00:00+03:00" + }] + }`) + }) + + variables, err := suite.client.Bots.WhatsApp.GetBotVariables("contactId") + suite.NoError(err) + suite.Equal("qwerty", variables[0].ID) +} + +func (suite *SendpulseTestSuite) TestBotsWhatsAppService_GetFlows() { + suite.mux.HandleFunc("/whatsapp/flows", func(w http.ResponseWriter, r *http.Request) { + suite.Equal(http.MethodGet, r.Method) + + fmt.Fprintf(w, `{ + "success": true, + "data": [{ + "id": "qwe123", + "bot_id": "qwe12345", + "name": "Alex", + "status": 1, + "triggers": [ + { + "id": "string", + "name": "string" + } + ], + "created_at": "2020-12-12T00:00:00+03:00" + }] + }`) + }) + + flows, err := suite.client.Bots.WhatsApp.GetFlows("contactId") + suite.NoError(err) + suite.Equal("qwe123", flows[0].ID) +} + +func (suite *SendpulseTestSuite) TestBotsWhatsAppService_RunFlow() { + suite.mux.HandleFunc("/whatsapp/flows/run", func(w http.ResponseWriter, r *http.Request) { + suite.Equal(http.MethodPost, r.Method) + + fmt.Fprintf(w, `{ + "success": true + }`) + }) + + err := suite.client.Bots.WhatsApp.RunFlow("contactId", "flowId", map[string]interface{}{ + "tracking_number": "1234-0987-5678-9012", + }) + suite.NoError(err) +} + +func (suite *SendpulseTestSuite) TestBotsWhatsAppService_RunFlowByTrigger() { + suite.mux.HandleFunc("/whatsapp/flows/runByTrigger", func(w http.ResponseWriter, r *http.Request) { + suite.Equal(http.MethodPost, r.Method) + + fmt.Fprintf(w, `{ + "success": true + }`) + }) + + err := suite.client.Bots.WhatsApp.RunFlowByTrigger("contactId", "keyword", map[string]interface{}{ + "tracking_number": "1234-0987-5678-9012", + }) + suite.NoError(err) +} + +func (suite *SendpulseTestSuite) TestBotsWhatsAppService_GetBotTriggers() { + suite.mux.HandleFunc("/whatsapp/triggers", func(w http.ResponseWriter, r *http.Request) { + suite.Equal(http.MethodGet, r.Method) + + fmt.Fprintf(w, `{ + "success": true, + "data": [{ + "id": "qwe1234", + "bot_id": "bot1", + "flow_id": "flow1", + "name": "string", + "type": 1, + "status": 1, + "keywords": [ + "bot1" + ], + "execution": { + "interval": 0, + "units": 1 + }, + "created_at": "2020-12-12T00:00:00+03:00" + }] + }`) + }) + + triggers, err := suite.client.Bots.WhatsApp.GetBotTriggers("bot") + suite.NoError(err) + suite.Equal("qwe1234", triggers[0].ID) +} + +func (suite *SendpulseTestSuite) TestBotsWhatsAppService_GetBotChats() { + suite.mux.HandleFunc("/whatsapp/chats", func(w http.ResponseWriter, r *http.Request) { + suite.Equal(http.MethodGet, r.Method) + + fmt.Fprintf(w, `{ + "success": true, + "data": [{ + "contact": { + "id": "string", + "bot_id": "string", + "status": 1, + "channel_data": { + "id": "string", + "name": "string", + "first_name": "string", + "last_name": "string", + "profile_pic": "string", + "locale": "string", + "gender": "string" + }, + "tags": [ + "string" + ], + "variables": {}, + "is_chat_opened": true, + "last_activity_at": "2020-12-12T00:00:00+03:00", + "automation_paused_until": "2020-12-12T00:00:00+03:00", + "unsubscribed_at": "2020-12-12T00:00:00+03:00", + "created_at": "2020-12-12T00:00:00+03:00" + }, + "inbox_last_message": { + "id": "string", + "contact_id": "string", + "bot_id": "string", + "campaign_id": "string", + "data": { + "text": "hello" + }, + "direction": 1, + "status": 1, + "delivered_at": "2020-12-12T00:00:00+03:00", + "opened_at": "2020-12-12T00:00:00+03:00", + "redirected_at": "2020-12-12T00:00:00+03:00", + "created_at": "2020-12-12T00:00:00+03:00" + }, + "inbox_unread": 0 + }] + }`) + }) + + chats, err := suite.client.Bots.WhatsApp.GetBotChats("bot") + suite.NoError(err) + suite.Equal("string", chats[0].InboxLastMessage.CampaignID) +} + +func (suite *SendpulseTestSuite) TestBotsWhatsAppService_GetContactMessages() { + suite.mux.HandleFunc("/whatsapp/chats/messages", func(w http.ResponseWriter, r *http.Request) { + suite.Equal(http.MethodGet, r.Method) + + fmt.Fprintf(w, `{ + "success": true, + "data": [{ + "id": "string", + "contact_id": "string", + "bot_id": "string", + "campaign_id": "string", + "data": { + "text": "hello" + }, + "direction": 1, + "status": 1, + "delivered_at": "2020-12-12T00:00:00+03:00", + "opened_at": "2020-12-12T00:00:00+03:00", + "redirected_at": "2020-12-12T00:00:00+03:00", + "created_at": "2020-12-12T00:00:00+03:00" + }] + }`) + }) + + messages, err := suite.client.Bots.WhatsApp.GetContactMessages("bot") + suite.NoError(err) + suite.Equal("string", messages[0].ID) +} + +func (suite *SendpulseTestSuite) TestBotsWhatsAppService_SendCampaign() { + suite.mux.HandleFunc("/whatsapp/campaigns/send", func(w http.ResponseWriter, r *http.Request) { + suite.Equal(http.MethodPost, r.Method) + + fmt.Fprintf(w, `{ + "success": true + }`) + }) + + messages := make([]WhatsAppMessage, 1) + messages[0] = WhatsAppMessage{ + Type: "image", + Image: &struct { + Link string `json:"link"` + Caption string `json:"caption"` + }{ + Link: faker.URL(), + Caption: faker.Word(), + }, + } + + err := suite.client.Bots.WhatsApp.SendCampaign(WhatsAppBotSendCampaignParams{ + Title: "Title", + BotID: "qwe123", + SendAt: time.Now(), + Messages: messages, + }) + suite.NoError(err) +} + +func (suite *SendpulseTestSuite) TestBotsWhatsAppService_SendCampaignByTemplate() { + suite.mux.HandleFunc("/whatsapp/campaigns/sendTemplate", func(w http.ResponseWriter, r *http.Request) { + suite.Equal(http.MethodPost, r.Method) + + fmt.Fprintf(w, `{ + "success": true + }`) + }) + + messages := make([]WhatsAppMessage, 1) + messages[0] = WhatsAppMessage{ + Type: "image", + Image: &struct { + Link string `json:"link"` + Caption string `json:"caption"` + }{ + Link: faker.URL(), + Caption: faker.Word(), + }, + } + + err := suite.client.Bots.WhatsApp.SendCampaignByTemplate(WhatsAppBotSendCampaignByTemplateParams{ + TemplateName: "first_template", + LanguageCode: "en", + Title: "Title", + BotID: "qwe123", + SendAt: time.Now(), + Messages: messages, + }) + suite.NoError(err) +} + +func (suite *SendpulseTestSuite) TestBotsWhatsAppService_GetTemplates() { + suite.mux.HandleFunc("/whatsapp/templates", func(w http.ResponseWriter, r *http.Request) { + suite.Equal(http.MethodPost, r.Method) + + fmt.Fprintf(w, `{ + "success": true, + "data": [{ + "id": "123", + "bot_id": "456", + "namespace": "tpls", + "category": "category", + "components": [ + {} + ], + "language": "ru", + "name": "first_template", + "rejected_reason": "", + "status": "APPROVED", + "created_at": "2020-12-12T00:00:00+03:00" + }] + }`) + }) + + messages := make([]WhatsAppMessage, 1) + messages[0] = WhatsAppMessage{ + Type: "image", + Image: &struct { + Link string `json:"link"` + Caption string `json:"caption"` + }{ + Link: faker.URL(), + Caption: faker.Word(), + }, + } + + templates, err := suite.client.Bots.WhatsApp.GetTemplates() + suite.NoError(err) + suite.Equal("456", templates[0].BotID) +}