Build a WhatsApp chatbot using Go. This package makes it easy to use NLP (Natural Language Processing), navigation, menus and sessions in a WhatsApp chatbot.
You can easily plugin your WhatsApp Business API either directly from Facebook or third-party platforms like Twilio,Clickatel,etc.
WhatsApp.Demo.MP4
This package provides an implementation for Twilio. The code snippets uses Twilio's WhatsApp.
You can listen for different types of messages.
twilio := twilio.NewListener(r)
listener := listen.NewListener(r, twilio)
text := listener.GetText()
twilio := twilio.NewListener(r)
listener := listen.NewListener(r, twilio)
f, location := listener.Location()
twilio := twilio.NewListener(r)
listener := listen.NewListener(r, twilio)
f, attachments := listener.Attachments()
client := twilioGo.NewRestClientWithParams(twilioGo.ClientParams{
Username: "",
Password: "",
})
twilioConnector := twilio.NewConnector("", client.Api)
sender := sender.NewService(twilioConnector)
if err := sender.SendText(text, phoneNumber); err != nil {
log.Fatal(err)
}
Natural language processing (NLP) is the ability of a computer program to understand human language as it is spoken and written -- referred to as natural language. This package provides an implementation for DialogFlow
conn, err := dialogflow.NewConnector("", "default", "xxxxx-creds.json", "europe-west2")
if err != nil {
log.Fatal(err)
}
nlpService := nlp.NewService(conn)
twilio := twilio.NewListener(r)
listener := listen.NewListener(r, twilio)
text := listener.GetText()
events, err := nlpService.DetectIntent(text, "en")
if err != nil {
log.Fatal(err)
}
cfg := &MongoConfig{
DBURI: "mongodb://localhost:27017",
DBName: "test",
}
mongoConnector, err := mongo.NewConnector(cfg.DBURI, cfg.DBName)
if err != nil {
log.Fatal(err)
}
sessionMgr := sessions.NewSessManager(mongoConnector)
sessionData := make(map[string]interface{})
sessionData["message"] = "hello world"
if err := sessionMgr.StartSession("", sessionData); err != nil {
log.Fatal(err)
}
menus := menu.Menu{}
itemA := menu.Item{Key: 12345, Title: "1. Mare's Specials 🍰"}
itemB := menu.Item{Key: 678910, Title: "2. Smoothies 🍝"}
items := []menu.Item{}
menuA = append(items, itemA, itemB)
header := "Welcome to Mare's Foodies Corner. See our menu options below:"
footer := "Kindly reply with 1 or 2"
if err := menus.Set(true, menuA, "0", header, footer); err != nil {
log.Fatal(err)
}
navigate := navigation.NewNavigator(menus)
m, err := navigate.NextMenu("menu key here")
if err != nil {
log.Fatal(err)
}
/examples
provides examples on how to use this package.