To run FB Messenger Bot, it requires Facebook App and Facebook Page.
Our YouPin Bot uses the followings:
- YouPin App, linked to the official YouPin Page
- YouPin Test App (for development/testing), linked to the YouPin Test Page
If you fork this repo or try to run locally, you need to create a new Facebook App and/or Page first.
You can follow the instructions provided here: https://developers.facebook.com/docs/messenger-platform/quickstart. But you can skip step 2 (Webhooks), we will be back with Webhooks in the next section.
You should copy the App Secret and Page Access Token. We will use them in our config file.
- Clone this repo.
git clone https://github.com/youpin-city/youpin-bot.git
.
-
Install docker.
-
Start API with MongoDB containers.
docker-compose up -d api
- Create a user account for your bot
curl -X POST localhost:9100/users -H 'Content-type: application/json' -d '{"name":"<your_name>","password":"<your_password>","email":"<your_email>","role":"user"}'
.
It will return _id
of your user.
<your_email>
, <your_password>
, and _id
will be used in step 6.
-
Make a copy of
config/default.json
and rename it toconfig/development.json
. -
Modify content of
config/development.json
with your created user account info and Facebook app/page secret.{ "appSecret": "<your_facebook_app_secret>", "pageAccessToken": "<your_facebook_page_access_token>", "validationToken": "<whatever, it will be used later>", "sessionMaxLength": 600000, "apiUri": "http://api:9100", "apiUsername": "<your_email>", "apiPassword": "<your_password>", "apiUserId": "<_id>" }
-
Start the bot container.
docker-compose up -d bot
- Install ngrok to help Facebook connect to localhost
npm install -g ngrok
- Start ngrok for tunneling.
ngrok http 5100
It will print
Tunnel Status online
Update update available (version 2.1.3, Ctrl-U to update)
Version 2.1.1
Region United States (us)
Web Interface http://127.0.0.1:4040
Forwarding http://<id>.ngrok.io -> localhost:5100
Forwarding https://<id>.ngrok.io -> localhost:5100
Connections ttl opn rt1 rt5 p50 p90
0 0 0.00 0.00 0.00 0.00
Copy your https://<id>.ngrok.io
to tell Facebook to connect. (Must be https
not just http
).
- Go to your created Facebook app. Under
Products
section,+Add Product
and chooseWebhooks
.
Enter your https://<id>.ngrok.io/webhook
into Callback URL
.
Enter your validationToken
in Step 6. into Verify token
.
Select message_deliveries
, messages
, messaging_optins
, and messaging_postbacks
.
Click Verify and save
. The window will be closed automatically.
Note: If the verfication fails, it be might caused by
1) validationToken is not matched.
2) ngrok does not start.
3) API docker container fails to start.
- Let's say hello to your bot in your created Facebook page. Enjoy!
This wrapper module provides functions for posting Pins and uploading photos using youpin-api. See the repo for more info regarding the API.
- Implement functions for other available API calls and share this module with youpin-web.
- Implement more advanced functions involving the API, such as retrieving potential duplicated Pins (#10). (But maybe this should be done in the backend, so that the web counter part can also use it.)
This wrapper module provides commonly used functions for creating and sending messages via Facebook Messenger.
This module manages conversation context for each user, e.g., whether he or she has posted a photo. The current implementation is super hacky. We should switch to Redis for in-memory data storage (#4).
This module implements conversation logics for the bot. There are a lot of easy improvements we can potentially implement to make the bot appears smarter.
The flow diagram of the bot can be found here: raw, pdf
When adding new sentence, one should run npm run i18n
to add the new senctences the localized files in ./locales
. Please make sure to install dev
dependencies and be aware that node_modules
might affect Docker
build.