Giter Club home page Giter Club logo

messengerbottutorial's Introduction

Facebook recently released the Messenger Bot API. Using the API we can automate our page repllies. Here I will guide you to create your own Facebook Messenger bot. We will create a Messenger bot for a college page.

####Step 1

  • Create a Facebook App
  • Go to the App Dashboard and in the bottom left select "Messenger" tab. Then lick on "Get Started" on the right. If you don't see anything on the right after clicking "Messenger" wait for few hours and try again.
  • Select your page and generate access token. You will have something like this by now.
    Access Token

####Step 2

  • Deploy App to Heroku
  • Finish heroku signup/login and add your Facebook Pages Access Token created above and some random string. Wait for build to finish.
  • Click on "View" and copy the url of your app. You should see something like this but with different url .herokuapp.com
    JavaForEverBot

####Step 3

  • Go back to the page where you generate Page access token. Click on "Setup Webhooks"
  • Enter your app url
  • Enter VERIFY_TOKEN as entered during creating app on heroku
  • Select all options, Confirm and verify

####Step 3


![Postman response](https://s3-ap-southeast-1.amazonaws.com/javaforever/postman1.PNG)

Now your bot is functional. Message your page "/help" and you'll see the help menu I have already configured. I will tell you further how to customize it. Finish bot

####Step 4

  • Now when you messaged your page in above step you had no idea what commands I have setup. To show a welcome screen follow these steps.
  • Open Postman select "Import" then paste the following code. Replace <PAGE_ID> with your page id and <PAGE_ACCESS_TOKEN> with your access token. To get your page Id go to your page About-> See botton for Facebook Page ID. If you want to customize welcome screen change the "text" now or you can change this later.
curl -X POST -H "Content-Type: application/json" -d '{
  "setting_type":"call_to_actions",
  "thread_state":"new_thread",
  "call_to_actions":[
    {
      "message":{
        "text":"Welcome to My College Bot\nI respond to the following commands\n/address To get the college address\n/contact To get college contact details\n/timings To get regular college hours\n/help To get these instructions again"
      }
    }
  ]
}' "https://graph.facebook.com/v2.6/<PAGE_ID>/thread_settings?access_token=<PAGE_ACCESS_TOKEN>"
  • Click "Send"
  • You should see the following response.
    Postman response

####How to customize responses?

  • Download and install the Heroku toolbelt
  • If you haven't already, log in to your Heroku account and follow the prompts to create a new SSH public key.
$ heroku login
  • Use Git to cloneyour apps source code to your local machine.
$ heroku git:clone -a yourappname<br>
$ cd yourappname
  • Make some changes to the code you just cloned and deploy them to Heroku using Git. Most probably you want to edit the app->controllers->webhooks_controller.rb file. If you know how to program notice the if else and edit the response accordingly.
$ git add . 
$ git commit -am "make it better"
$ git push heroku master

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.