Bot example#
Installation#
Before you begin, you need to install the library and initiate the bot; this process is described in detail here: How to import the library and initiate your bot.
Bot example#
As an example, a bot was created that demonstrates sending methods of the Notification structure.
The bot is started with the command - /start After launching, you need to select a method from the menu, and the bot will execute it.
Link to example: full,go.
The start scene waits for the /start command, after which it sends the menu and activates the next PickMethodScene. PickMethodScene waits for the user's response and executes the selected method. If the user selected SendFileByUrl(), then the bot will launch the InputLinkScene scene, in which it will ask for a link to the file and send the file if the link is valid.
package full
import (
"github.com/green-api/whatsapp_chatbot_golang"
)
type StartScene struct {
}
func (s StartScene) Start(bot *whatsapp_chatbot_golang.Bot) {
bot.IncomingMessageHandler(func(notification *whatsapp_chatbot_golang.Notification) {
if notification.Filter(map[string][]string{"text": {"/start"}}) {
notification.AnswerWithText(`Hi! This bot uses various API methods.
Please select a method:
1. SendMessage()
2. SendFileByUrl()
3. SendPoll()
4. SendContact()
5. SendLocation()
Send the item number in one digit.`)
notification.ActivateNextScene(PickMethodScene{})
} else {
notification.AnswerWithText("Please enter the /start command.")
}
})
}
type PickMethodScene struct {
}
func (s PickMethodScene) Start(bot *whatsapp_chatbot_golang.Bot) {
bot.IncomingMessageHandler(func(message *whatsapp_chatbot_golang.Notification) {
if message.Filter(map[string][]string{"text": {"1"}}) {
message.AnswerWithText("Hello world!")
}
if message.Filter(map[string][]string{"text": {"2"}}) {
message.AnswerWithText("Give me a link for a file, for example: https://th.bing.com/th/id/OIG.gq_uOPPdJc81e_v0XAei")
message.ActivateNextScene(InputLinkScene{})
}
if message.Filter(map[string][]string{"text": {"3"}}) {
message.AnswerWithPoll("Please choose a color:", false, []map[string]interface{}{
{
"optionName": "Red",
},
{
"optionName": "Green",
},
{
"optionName": "Blue",
},
})
}
if message.Filter(map[string][]string{"text": {"4"}}) {
message.AnswerWithContact(map[string]interface{}{
"phoneContact": 79001234567,
"firstName": "Artem",
"middleName": "Petrovich",
"lastName": "Evpatoria",
"company": "Bicycle",
})
}
if message.Filter(map[string][]string{"text": {"5"}}) {
message.AnswerWithLocation("House", "Cdad. de La Paz 2969, Buenos Aires", -34.5553558, -58.4642510)
}
if !message.Filter(map[string][]string{"text_regex": {"\\d+"}}) {
message.AnswerWithText("Answer must contain only numbers!")
}
})
}
type InputLinkScene struct {
}
func (s InputLinkScene) Start(bot *whatsapp_chatbot_golang.Bot) {
bot.IncomingMessageHandler(func(message *whatsapp_chatbot_golang.Notification) {
if message.Filter(map[string][]string{"regex": {"^https://[^\\s]+$"}}) {
text, _ := message.Text()
message.AnswerWithUrlFile(text, "testFile", "This is your file!")
message.ActivateNextScene(PickMethodScene{})
} else {
message.AnswerWithText("The link must not contain spaces and must begin with https://")
}
})
}
List of examples#
| Description | Link to example |
|---|---|
| How to initialize a handler | base.go |
| How to initialize a scene | baseScene.go |
| Scene "Echo" | echo.go |
| How to receive other types of notifications | event.go |
| How to filter incoming messages | filter.go |
| How to work with bot state | state.go |
| Example of a ready-made chat bot | full.go |