Skip to content
This repository has been archived by the owner on Sep 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request #234 from rocketsummer/master
Browse files Browse the repository at this point in the history
New CLI options for facebook bot to automatically get localtunnel.me URL
  • Loading branch information
Ben Brown committed May 23, 2016
2 parents 420ee1b + df12814 commit 330cc74
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 16 deletions.
39 changes: 32 additions & 7 deletions facebook_bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@ This bot demonstrates many of the core features of Botkit:
Run your bot from the command line:
page_token=<MY PAGE TOKEN> verify_token=<MY_VERIFY_TOKEN> node facebook_bot.js
page_token=<MY PAGE TOKEN> verify_token=<MY_VERIFY_TOKEN> node facebook_bot.js [--lt [--ltsubdomain LOCALTUNNEL_SUBDOMAIN]]
Use localtunnel.me to make your bot available on the web:
lt --port 3000
Use the --lt option to make your bot available on the web through localtunnel.me.
# USE THE BOT:
Expand Down Expand Up @@ -78,9 +76,24 @@ if (!process.env.verify_token) {
process.exit(1);
}


var Botkit = require('./lib/Botkit.js');
var os = require('os');
var commandLineArgs = require('command-line-args');
var localtunnel = require('localtunnel');

const cli = commandLineArgs([
{name: 'lt', alias: 'l', args: 1, description: 'Use localtunnel.me to make your bot available on the web.',
type: Boolean, defaultValue: false},
{name: 'ltsubdomain', alias: 's', args: 1,
description: 'Custom subdomain for the localtunnel.me URL. This option can only be used together with --lt.',
type: String, defaultValue: null},
]);

const ops = cli.parse();
if(ops.lt === false && ops.ltsubdomain !== null) {
console.log("error: --ltsubdomain can only be used together with --lt.");
process.exit();
}

var controller = Botkit.facebookbot({
debug: true,
Expand All @@ -94,13 +107,25 @@ var bot = controller.spawn({
controller.setupWebserver(process.env.port || 3000, function(err, webserver) {
controller.createWebhookEndpoints(webserver, bot, function() {
console.log('ONLINE!');
if(ops.lt) {
var tunnel = localtunnel(process.env.port || 3000, {subdomain: ops.ltsubdomain}, function(err, tunnel) {
if (err) {
console.log(err);
process.exit();
}
console.log("Your bot is available on the web at the following URL: " + tunnel.url + '/facebook/receive');
});

tunnel.on('close', function() {
console.log("Your bot is no longer available on the web at the localtunnnel.me URL.");
process.exit();
});
}
});
});


controller.hears(['hello', 'hi'], 'message_received', function(bot, message) {


controller.storage.users.get(message.user, function(err, user) {
if (user && user.name) {
bot.reply(message, 'Hello ' + user.name + '!!');
Expand Down
6 changes: 5 additions & 1 deletion lib/Facebook.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ function Facebookbot(configuration) {


// set up a web route for receiving outgoing webhooks and/or slash commands
facebook_botkit.createWebhookEndpoints = function(webserver, bot) {
facebook_botkit.createWebhookEndpoints = function(webserver, bot, cb) {

facebook_botkit.log(
'** Serving webhook endpoints for Slash commands and outgoing ' +
Expand Down Expand Up @@ -186,6 +186,10 @@ function Facebookbot(configuration) {
}
});

if (cb) {
cb();
}

return facebook_botkit;
};

Expand Down
2 changes: 1 addition & 1 deletion lib/storage/storage_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var testStorageMethod = function(storageMethod) {
console.log(data);
test.assert(data.foo === testObj0.foo);
});
storageMethod.get('shouldnt-be-here', function (err, data) {
storageMethod.get('shouldnt-be-here', function(err, data) {
test.assert(err.displayName === 'NotFound');
test.assert(!data);
});
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
"dependencies": {
"back": "^1.0.1",
"body-parser": "^1.14.2",
"command-line-args": "^2.1.6",
"express": "^4.13.3",
"https-proxy-agent": "^1.0.0",
"jfs": "^0.2.6",
"localtunnel": "^1.8.1",
"mustache": "^2.2.1",
"request": "^2.67.0",
"ware": "^1.3.0",
Expand Down
12 changes: 5 additions & 7 deletions readme-facebook.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,15 @@ Copy this token, you'll need it!

4) Define your own "verify token" - this a string that you control that Facebook will use to verify your web hook endpoint.

5) Run the example bot app, using the two tokens you just created:
5) Run the example bot app, using the two tokens you just created. If you are _not_ running your bot at a public, SSL-enabled internet address, use the --lt option and note the URL it gives you.

```
page_token=<MY PAGE TOKEN> verify_token=<MY_VERIFY_TOKEN> node facebook_bot.js
page_token=<MY PAGE TOKEN> verify_token=<MY_VERIFY_TOKEN> node facebook_bot.js [--lt [--ltsubdomain CUSTOM_SUBDOMAIN]]
```

6) If you are _not_ running your bot at a public, SSL-enabled internet address, use [localtunnel.me](http://localtunnel.me) to make it available to Facebook. Note the URL it gives you. For example, it may say your url is `https://xyx.localtunnel.me/` In this case, the webhook URL for use in step 7 would be `https://xyx.localtunnel.me/facebook/receive`
6) [Set up a webhook endpoint for your app](https://developers.facebook.com/docs/messenger-platform/implementation#setting_webhooks) that uses your public URL. Use the verify token you defined in step 4!

7) [Set up a webhook endpoint for your app](https://developers.facebook.com/docs/messenger-platform/implementation#setting_webhooks) that uses your public URL, or the URL that localtunnel gave you. Use the verify token you defined in step 4!

8) Your bot should be online! Within Facebook, find your page, and click the "Message" button in the header.
7) Your bot should be online! Within Facebook, find your page, and click the "Message" button in the header.

Try:
* who are you?
Expand All @@ -46,7 +44,7 @@ Try:
### Things to note

Since Facebook delivers messages via web hook, your application must be available at a public internet address. Additionally, Facebook requires this address to use SSL. Luckily, you can use [LocalTunnel](https://localtunnel.me/) to make a process running locally or in your dev environment available in a Facebook-friendly way.
Since Facebook delivers messages via web hook, your application must be available at a public internet address. Additionally, Facebook requires this address to use SSL. Luckily, you can use the --lt option to make a process running locally or in your dev environment available in a Facebook-friendly way.

When you are ready to go live, consider [LetsEncrypt.org](http://letsencrypt.org), a _free_ SSL Certificate Signing Authority which can be used to secure your website very quickly. It is fabulous and we love it.

Expand Down

0 comments on commit 330cc74

Please sign in to comment.