How to start receiving and responding to messages#
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.
How to start receiving and responding to messages#
To start receiving notifications, you need to call the bot.StartReceivingNotifications() method on the bot. But before that you need to add a handler; this can be done in two ways. You can do this directly in the main
function as in the base
example.
Link to example: base.go.
package base
import (
"github.com/green-api/whatsapp_chatbot_golang"
)
func main() {
bot := whatsapp_chatbot_golang.NewBot("INSTANCE_ID", "TOKEN")
bot.IncomingMessageHandler(func(message *whatsapp_chatbot_golang.Notification) {
if message.Filter(map[string][]string{"text": {"test"}}) {
message.AnswerWithText("Well done! You have write \"test\".")
} else {
message.AnswerWithText("Write \"test\"!")
}
})
bot.StartReceivingNotifications()
}
If you have complex nested scripts, it is better to use scenes as in the baseScene
example. Using scenes is easy - just put the bot logic into a separate structure that implements the Scene
interface, and add it to the bot using the bot.SetStartScene(StartScene{})
method. The starting scene can call the next one using the message.ActivateNextScene(NextScene{})
method, then the next webhook will go into the new scene, this will allow you to divide the bot into separate parts and make the code more readable and editable:
Link to example: baseScene.go.
package base
import (
"github.com/green-api/whatsapp_chatbot_golang"
)
func main() {
bot := whatsapp_chatbot_golang.NewBot("INSTANCE_ID", "TOKEN")
bot.SetStartScene(StartScene{})
bot.StartReceivingNotifications()
}
type StartScene struct {
}
func (s StartScene) Start(bot *whatsapp_chatbot_golang.Bot) {
bot.IncomingMessageHandler(func(message *whatsapp_chatbot_golang.Notification) {
if message.Filter(map[string][]string{"text": {"test"}}) {
message.AnswerWithText("Well done! You have write \"test\".")
message.AnswerWithText("Now write \"second scene\"")
message.ActivateNextScene(SecondScene{})
} else {
message.AnswerWithText("Write \"test\"!")
}
})
}
type SecondScene struct {
}
func (s SecondScene) Start(bot *whatsapp_chatbot_golang.Bot) {
bot.IncomingMessageHandler(func(message *whatsapp_chatbot_golang.Notification) {
if message.Filter(map[string][]string{"text": {"second scene"}}) {
message.AnswerWithText("Well done! You have write \"second scene\".")
message.ActivateNextScene(StartScene{})
} else {
message.AnswerWithText("This is second scene write \"second scene\"!")
}
})
}
If you need that when creating a new state, it already has some default values, you need to change the InitData
field of the StateManager
structure. In the standard implementation of MapStateManager
this is done like this:
package main
import (
"github.com/green-api/whatsapp_chatbot_golang"
"github.com/green-api/whatsapp_chatbot_golang/examples/full"
)
func main() {
bot := whatsapp_chatbot_golang.NewBot("INSTANCE_ID", "TOKEN")
bot.StateManager = whatsapp_chatbot_golang.NewMapStateManager(
map[string]interface{}{
"defaultField1": "defaultValue1",
"defaultField2": "defaultValue2",
"defaultField3": "defaultValue3",
})
bot.SetStartScene(full.StartScene{})
bot.StartReceivingNotifications()
}
Please note that errors may occur while executing queries so that your program does not break due to them, you need to handle errors. All library errors are sent to the ErrorChannel
channel, you can handle them for example in this way:
package main
import (
"fmt"
"github.com/green-api/whatsapp_chatbot_golang"
"github.com/green-api/whatsapp_chatbot_golang/examples/full"
)
func main() {
bot := whatsapp_chatbot_golang.NewBot("INSTANCE_ID", "TOKEN")
bot.SetStartScene(full.StartScene{})
//All errors will simply be output to the console
go func() {
select {
case err := <-bot.ErrorChannel:
if err != nil {
fmt.Println(err)
}
}
}()
bot.StartReceivingNotifications()
}
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 |