Build a Personal Facebook Messenger Bot in 10 minutes with Nodejs and Standard Library

Code on Standard Library Setup

This is where our code lives that will enable all the communication. When we send a message to our bot on Facebook, it’ll first go to Dialogflow. Dialogflow will route it to the right intent and then forward it to our function — this function.

First search for fb-messenger-bot in Community API Sources. Click on Create New API.

Once created, you will see three files in the functions folder.

  1. __main__.js 
    The main function fetches the Reddit posts and sends them to the users. Modify the multi object at the top of the function, to customise the subreddits.
  2. webhook.js 
    This function will handle all the webhook requests from Dialogflow. Here is where you’ll add the custom intents if you want to extend the bot functionality.
  3. privacy.js 
    Did you know Code on Standard Library functions can return HTML files? This is a Privacy File that you can use on the FB Developer Portal. The URL will look something like[email protected]/privacy . You should add your own email to the bottom of this document.

Now, navigate to the env.json file and populate the environments with your Standard Library Token and Facebook Page Access Token.

To retrieve your Standard Library Token, place the cursor in between the yellow quotes and right click. Select your Standard Library Token from the drop-down menu and the variable should automatically populate.

Enter your FB Access Token and the Standard Library Token in your env.json file

Once you have saved the env.json file, click on Run.

Click Run to deploy the service

Remember that every time you change any code, you need to save and then Run it, to deploy the new code.

Once deployed, copy the API Endpoint URL that’s shown. This is your webhook endpoint.

Now let us finish Dialogflow setup. Switch to Dialogflow and click on Fulfillment and turn on the Webhook.

Enable the webhook and add URL

Append the copied URL with /webhook/ and click on SAVE at the bottom of the page. Your webhook URL should look like[email protected]/webhook/

Append /webhook/ to your base URL

We are almost done. All we need to do now, is to enable our bot on the Facebook Page. Go to your Facebook page and click on Add a Button. Choose Send Message, complete the details and save.

Add a Send Message Button to your Facebook Page

Now when you hover over the Send Message button, a dropdown will show. Click Test button.

Click on Test button to Test your bot

This will open a chat with a Get Started button. Click on it and if you have set everything up correctly, you will be greeted with “Woohoo!. Welcome {Name}. You have been added to the database.”.

This means you have been added as a user in your function on Code on Standard Library local storage.

If you message your bot “Hi” again, it will reply with “Hi {Name}, you have already been added to the database.

Now to setup tasks.

Tasks on Code on Standard Library

One of the coolest features of Code on Standard Library is Tasks. Setting up Tasks makes your code run automatically on a specified schedule.

Click on Tasks to set up your code to run on a schedule

I’ve currently setup the function for four notifications a day. You can change it to whatever number you prefer. Remember that you’ll also need to change max_notifications variable at the top of the function and add the same number of repeatable tasks in the cron specification.

In the Tasks window, choose your bot function to add a schedule to it.

Choose your function to add a Task schedule

You can setup the task schedule using the Schedule dropdown. But we want a little more control, so let’s use Advanced (cron).

Click on Advanced (cron) to use cron expressions for task schedule

The cron expression can be very confusing to understand. The expression takes 5 values — * * * * *. The first one is the minutes value and the second is the hours value.

So if you enter 30 5,8,11,15 * * *, it translates to 4 tasks run at 05:30, 08:30, 11:30 and 15:30, repeated every day. The times are in UTC, so you’ll need to add or subtract your timezone offset for the correct local time.

As I live in India and my timezone offset is +05:30, this schedule will run at 11:00, 14:00, 17:00 and 21:00 local time.

If the cron expression is valid, the next three invocation times are shown below it

Click on Schedule Task and you’re all done. You should now receive notifications on your messenger at the scheduled times.

If you want to test out the tasks schedule, choose once every minute. Once you get the right messages, turn it off and use the above cron expression or something similar to customise the schedule.

Congratulations! You have a personal chatbot now. I’ll add more tutorials in the coming months on what other interesting things you can do with a chatbot.

Check out Create an Alexa Radio Skill, if you want to build an Alexa skill with your personalized radio stations.

read original article here