- Here, you'll learn how to build a bot with Facebook Messenger and Recast.AI.
- Create an account on Recast.AI.
- Create an account on Facebook Developers. (same account than your personal Facebook account)
- Log into your Recast.AI account.
- Create a new bot.
- In your profile, click your bot.
- In the tab-menu, click on the the little screw.
- Here is the
request access token
you will need to configure your bot.
- Choose the category of your page.
- Fill out the Facebook requirements step by step.
- Log on to your Facebook Developers account.
- Create a new Facebook app.
- Get your app secret and ID Dashboard.
- Get your page Token Messanger.
git clone https://github.com/fhenri42/bot-Messenger-golang.git
- Download the appropriate version of Ngrok.
- Open a new tab in your terminal:
./ngrok http 5000
- Copy past the
https://*******ngrok.io
you get, you will need it for the next step. - Leave your Ngrok serveur running.
-
Copy your Recast.AI
Recast.AI access token
-
Copy your page access Token
Token of your Page
-
Copy your validationToken
The token of your Webhook
-
Incoming :)
- make sure to have ngrok launched and the correct URL in you config file.
go run src/*.go
- go back to the Facebook Developer page and add a new webhook.
- Subscribe your page to the webhook you just created.
func call_recast(msg string) string {
fmt.Printf("start call to recast")
client := &http.Client{}
form := url.Values{}
form.Add("text", msg)
req, err := http.NewRequest("POST","https://api.recast.ai/v2/converse" ,strings.NewReader(form.Encode()))
if err != nil {
log.Println(err)
return "err"
}
req.Header.Set("Authorization", fmt.Sprintf("Token ADD_YOUR_TOKEN"))
resp, err := client.Do(req)
if err != nil {
log.Println(err)
return "err"
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println(err)
return "err"
}
log.Println("\n \n ",string(body),"\n \n")
var rep RecastRep
err = json.Unmarshal(body, &rep)
if err != nil {
log.Println(err)
return "err"
}
return rep.Results.Action.Reply
}
func message_handler(data Data) {
message := data.Entry[0].Messaging[0].Message.Text
recipient := data.Entry[0].Messaging[0].Sender.ID
msg := call_recast(message)
post_facebook(msg, recipient)
}
- Have fun coding your bot! :)
Henri Floren [email protected]