Skip to content
This repository has been archived by the owner on Mar 21, 2022. It is now read-only.
/ bot-messenger Public archive

Start coding your bot: Recast.AI + Messenger

Notifications You must be signed in to change notification settings

jeremybell/bot-messenger

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

43 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Start coding your bot: Recast.AI + Facebook messenger

  • Here, you'll learn how to build a bot with Facebook Messenger and Recast.AI.
  • If you don't know how to use Recast.AI, you can check this SDK first: Recast.AI-nodejs-SDK

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/RecastAI/bot-messenger.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.js

  • 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
vim config.js
const config = {}

config.pageAccessToken = 'PAGE_TOKEN'
config.validationToken = 'ABCD1234'
config.recastToken = 'RECAST_TOKEN'
config.language = 'en'

module.exports = config

Launching your Bot

  • make sure to have ngrok launched and the correct URL in you config file.
npm install
npm start

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

SDK usage

  • All you need for you bot is in the bot.js file. The call to Recast.AI is already done.
  • client.textConverse(message.body, { conversationToken: senderID }) To use this method you need to pass the user's input, and a unique conversation token. This token can be the senderId of the messenger chat. This token will create for each users a specific conversation with your bot.
  • res.reply() To get the first reply of your bot.
  • res.replies To get an array of all your replies.
  • res.action Get the object action. When an action is complete you will have the action.done = true and you will be able to trigger a specific behavior.

Start-kit helpers

function handleMessage(event) {
  const senderID = event.sender.id
  const messageText = event.message.text
  const messageAttachments = event.message.attachments
  if (messageText) {
    client.textConverse(messageText, { conversationToken: senderID }).then((res) => {
      const reply = res.reply()               /* To get the first reply of your bot. */
      const replies = res.replies             /* An array of all your replies */
      const action = res.action               /* Get the object action. You can use 'action.done' to trigger a specification action when it's at true. */

      if (!reply) {
        const options = {
          messageText: null,
          buttonTitle: 'My first button',    /* Option of your button. */
          buttonUrl: 'https://recast.ai/',   /* If you like more option check out ./facebook.js the function replyButton, and look up */
          buttonType: 'web_url',             /* the facebook doc for button https://developers.facebook.com/docs/messenger-platform/send-api-reference#message */
          elementsTitle: 'I don\'t get it :(',
        }
        replyButton(senderID, options)        /* to reply a button */
      } else {
        if (action && action.done === true) {
          console.log('action is done')
          // Use external services: use res.memory('notion') if you got a notion from this action
        }
        let promise = Promise.resolve()
        replies.forEach(rep => {
          promise = promise.then(() => replyMessage(senderID,rep))
        })
        promise.then(() => {
          console.log('ok')
        })
      }
    }).catch(err => {
      console.log(err)
    })
  } else if (messageAttachments) {
    replyMessage(senderID, 'Message with attachment received')
  }
}
  • Have fun coding your bot! :)

Author

Henri Floren - Recast.AI henri.floren@recast.ai

You can follow us on Twitter at @recastai for updates and releases.

License

Copyright (c) [2016] Recast.AI

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

Start coding your bot: Recast.AI + Messenger

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published