Skip to content

lemmings-io/02-facebook-example

Repository files navigation

Facebook Example

A Facebook Messenger Bot Example in Clojure

Facebook and app setup

  1. Make sure you have followed the Lemmings Clojure and Atom Editor setup.

  2. Start a new vagrant session by opening a new terminal window (on Windows you can use Git BASH), navigate to the 'clojure-master' directory you downloaded before which contains the Vagrantfile and run vagrant ssh.

  3. In the VM, change into the projects directory

    cd projects/
  4. "Clone" the Facebook example into your Vagrant environment

    git clone https://github.com/lemmings-io/02-facebook-example.git

    Now change into the Facebook example directory:

    cd 02-facebook-example
  5. Start the nREPL server

    Start another VM session by opening a new terminal window (Cmd-T) and run vagrant ssh.

    Navigate to the project folder you just created with cd projects/02-facebook-example.

    Start the nREPL server with lein repl :headless :host 0.0.0.0 :port 7888 as described in the Clojure and Atom Editor Setup

  6. Open your project in Atom.

    Start Atom and open the folder clojure-master/projects/02-facebook-example as an Atom project.

    There's two things to know about Atom:

    1. Ctrl-P on Windows and Cmd-P on macOS opens a file search. You can use this to jump to any file in your project quickly.

    2. Ctrl-Shift-P on Windows and Cmd-Shift-P on macOS opens the Atom Command Palette. You use it to start the nREPL or enable Autoeval.

  7. Setup a Facebook Page, Facebook app, create a Page Access Token and link the app to the page by following this step-by-step guide.

  8. Provide your Facebook Page Access Token, Verify Token and Page Secret for local development by creating a file called profiles.clj in your working directory <your-project-name>/profiles.clj

     {:dev {:env {:page-access-token "REPLACE"
        		   	  :verify-token "REPLACE"}}}
    

Starting the development environment

  1. Start a new VM shell session via vagrant ssh in your terminal.

  2. Once logged in to the VM shell change into the facebook-example project directory

     cd 02-facebook-example/
    
  3. Run ngrok http 3000 (read more about ngrok)

    ngrok Server

  4. Start another new VM shell session via vagrant ssh in a new terminal window.

  5. Start the local server

     lein ring server
    

    Lein Server

    Via the lein-ring doc: by default, this command attempts to find a free port, starting at 3000.

Check if the app is running

  1. Visit the https URL of the ngrok process you've started earlier in this guide.
    E.g. https://0db8caac.ngrok.io

    If everything went right, you'll see "Hello Lemming :)" in your Browser 🎈

    Hello Lemming

  2. In your Facebook Developer App go to "Webhooks" in the left sidebar and add Callback URL and Verify Token accordingly:

    Webhook Setup

  3. Click "Verify and Save" and your app is connected to Facebook's Messenger API. 🎈

    Webhook Success

  4. Then go to "Messenger" in the left sidebar and in the section "Webhooks" select your page to subscribe your webhook to the pages events. Subscribe to the events messages and messaging_postbacks, you can subscribe to more events later.

    Webhook Subscription Webhook Events

  5. Go to your Facebook Page and send a message and your bot echo's your input. Congratulations!💧

    Echo Bot

Check out the code to find out more. Also try sending "image", "help" or thumbs up to your bot. 🙂

Deploying to Heroku

  1. If you haven't yet, create an account at Heroku.

  2. In the VM shell (ssh) session make sure you are in the right directory

cd projects/02-facebook-example
  1. In the VM shell session login to your Heroku account via heroku login.

    -> heroku login
    Enter your Heroku credentials.
    Email:
    Password:
    
  2. Create an app on Heroku for your bot. Type heroku create into your command prompt at the root of the project.

    -> heroku create
    Creating gentle-plateau-38046... done, stack is cedar-14
    https://gentle-plateau-38046.herokuapp.com/ | https://git.heroku.com/gentle-plateau-38046.git
    Git remote heroku added
    
  3. Do a command line heroku config for each one of your tokens:

    heroku config:set PAGE_ACCESS_TOKEN=your_page_access_token
    heroku config:set VERIFY_TOKEN=your_verify_token
    
  4. You can deploy to heroku with the following command:

    ./heroku.sh

    You should see it deploy. 🍵

  5. Now we need to setup your app on Facebook with your app hosted on Heroku.

    As you've done it earlier for your local environment, in your Facebook Developer App click on the "Webhooks" section in the left sidebar and add Callback URL (e.g. https://gentle-plateau-38046.herokuapp.com/webhook) and Verify Token accordingly.

    Heroku Webhook Setup

  6. Click "Verify and Save" and your Heroku app is connected to Facebook's Messenger API 🎈

    Heroku Webhook Success

  7. Go to your Facebook Page and send a message and your bot should respond to you. Kudos! 🙂💧

    Echo Bot

Links

About

A Facebook Messenger Bot Example in Clojure

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages