/go-whatsapp

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.

Primary LanguageGo

Go-WhatsApp

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.

Listening

You can listen for different types of messages.

Listen for Text

twilio := twilio.NewListener(r)
listener := listen.NewListener(r, twilio)
text := listener.GetText()

Listen for Location

twilio := twilio.NewListener(r)
listener := listen.NewListener(r, twilio)
f, location := listener.Location()

Listen for Attachments

twilio := twilio.NewListener(r)
listener := listen.NewListener(r, twilio)
f, attachments := listener.Attachments()

Sending

Send Text

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)

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

Detect Intect of a WhatsApp message

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)
}

Sessions

Add data to sessions using Mongo as the driver

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

Set a menu

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)
}

Navigation

Navigate to a menu

navigate := navigation.NewNavigator(menus)
m, err := navigate.NextMenu("menu key here")
if err != nil {
	log.Fatal(err)
}

Examples

/examples provides examples on how to use this package.