Today, we are going to learn how to design, build, deploy and manage a Facebook chatbot written in Node.js and deploy using Cloud 66.
For this exercise, we've prepared a bootstrap Node.js chatbot to get you started right away. You can find the code on GitHub.
Our Awesome Brainless Bot
Our Facebook Messenger bot has the smallest brain you can imagine. It can only echo each message it receives, and when it receives
cloud66 , it replies how awesome Cloud 66 is.
Design Your World-Dominating Bot
Whatever you try to achieve, we want to iterate fast and embrace change. Running your bot,
$ node bot.js, it's crucial to use a tunnel service like ngrok to tunnel the HTTPS request to your development box.
Facebook demands your bot is using https and especially in the early stages you don't have a staging or production stack ready to test your bot.
Before running your code, make sure you follow the quick start guide to setting up a Facebook Application with Messenger support. During the setup, you need to start your application on your local machine.
Here are the steps you need to take:
- Use a boilerplate to kickstart your bot.
- Set the environment variable before the bot verification procedure:
$ export MESSENGER_VERIFICATION_CODE=__change_with_very_secret_code__.
- Run the code
$ node bot.js.
- Create a tunnel:
$ ngrok http 3000.
- Verify your bot using the Facebook UI. Make sure it's using the same
- Generate the page access token in the Facebook UI.
- Stop your bot service.
- Set your page access token:
$ export MESSENGER_PAGE_ACCESS_TOKEN=__change_with_page_access_token__.
- Run your bot again and start testing.
Every time your change your code, you can reload the running process and start chatting with your bot and make changes.
Ready to rock! Chatbot world domination is around the corner.
Bots in a Container
To make life easier, we also included a containerized version of the bot in the Git repository. You can build the bot
docker-compose build and run the bot with
docker-compose run --service-ports bot. You need to edit the environment variables in the
docker-compose.yml file to make the bot work.
Build and Deploy Your Bot
You got your bot and you want to deploy. We are using a product called Cloud 66 for Node. We are just five minutes away deploying your bot on any cloud or your own servers.
Let's do this thing!
Create a new Node stack and dial in your Git repo and the right branch.
Hit Analyze to get the analysis results of your stack and the opportunity to add some environment variables to make your bot work.
We are almost done. Soon, we can deploy. Hit Add environment variables before deployment and enter the right values for the Bot to work.
Save it and hit Deploy stack and the cloud provider of your choice or deploy it on your own server, using registered servers.
In this example, I'm using a 1GB/1CPU VM, which is enough to get started. Hit Deploy Stack and let Cloud 66 do all the heavy lifting. After provisioning the server and installing all packages and security, we have our bot running in production.
Check our step-by-step instructions how to get started with Node stacks if you need more information.
Manage Your Bot
Great! Your bot is deployed. We know Facebook only wants to use our bot if we are using SSL.
Not to worry — we got you covered. Just select Secure your site — add a SSL certificate and use our Let'sEncrypt function to generate free SSL certificates.
If you need more capacity for your bot, there's an easy mechanism for scaling up your application and running job instances. If your server has the capacity and is not fully utilized you can scale your application instances simply and quickly. Here is how:
- Click on your stack.
- Click on your Node Server.
- Find the application (web) or job instance you want to scale.
- Scale up the instances by pressing the "Scale up" (+) button.
- Hit scale.
- Wait a couple of seconds.
- You're scaled up!
If you need more capacity and your server is running low on resources (> 80% utilization), then it’s time to scale vertically and/or horizontally.