Skip to content

fhenri42/bot-Messenger-golang

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

README is not up to date (messing the config file part)!

Start coding your bot: Recast.AI + Facebook messenger

  • Here, you'll learn how to build a bot with Facebook Messenger and Recast.AI.

Requirements

Set up your Recast.AI account

Create your bot
  • Log into your Recast.AI account.
  • Create a new bot.
Get your token
  • 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.

Create your facebook page

alt text

  • Choose the category of your page.
  • Fill out the Facebook requirements step by step.

alt text

Set up your facebook account

  • Log on to your Facebook Developers account.
  • Create a new Facebook app.

alt text

alt text

alt text

Start your bot in local

git clone https://github.com/fhenri42/bot-Messenger-golang.git

Ngrok

  • 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.

Complete the config.go

  • 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 :)

Launching your Bot

  • make sure to have ngrok launched and the correct URL in you config file.
 go run src/*.go

Config webhook

  • go back to the Facebook Developer page and add a new webhook.

alt text

  • Subscribe your page to the webhook you just created.

alt text

Result

alt text

Your bot

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! :)

Author

Henri Floren [email protected]

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published