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

Commit

Permalink
Clean up facebook_bot.js
Browse files Browse the repository at this point in the history
  • Loading branch information
ouadie-lahdioui committed Jun 27, 2017
1 parent 1da1b3d commit ba64ec6
Showing 1 changed file with 144 additions and 30 deletions.
174 changes: 144 additions & 30 deletions examples/facebook_bot.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,160 @@
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
______ ______ ______ __ __ __ ______
/\ == \ /\ __ \ /\__ _\ /\ \/ / /\ \ /\__ _\
\ \ __< \ \ \/\ \ \/_/\ \/ \ \ _"-. \ \ \ \/_/\ \/
\ \_____\ \ \_____\ \ \_\ \ \_\ \_\ \ \_\ \ \_\
\/_____/ \/_____/ \/_/ \/_/\/_/ \/_/ \/_/
This is a sample Facebook bot built with Botkit.
This bot demonstrates many of the core features of Botkit:
* Connect to Facebook's Messenger APIs
* Receive messages based on "spoken" patterns
* Reply to messages
* Use the conversation system to ask questions
* Use the built in storage system to store and retrieve information
for a user.
# RUN THE BOT:
Follow the instructions here to set up your Facebook app and page:
-> https://developers.facebook.com/docs/messenger-platform/implementation
Run your bot from the command line:
app_secret=<MY APP SECRET> page_token=<MY PAGE TOKEN> verify_token=<MY_VERIFY_TOKEN> node facebook_bot.js [--lt [--ltsubdomain LOCALTUNNEL_SUBDOMAIN]]
Use the --lt option to make your bot available on the web through localtunnel.me.
# USE THE BOT:
Find your bot inside Facebook to send it a direct message.
Say: "Hello"
The bot will reply "Hello!"
Say: "who are you?"
The bot will tell you its name, where it running, and for how long.
Say: "Call me <nickname>"
Tell the bot your nickname. Now you are friends.
Say: "who am I?"
The bot will tell you your nickname, if it knows one for you.
Say: "shutdown"
The bot will ask if you are sure, and then shut itself down.
Make sure to invite your bot into other channels using /invite @<my bot>!
# EXTEND THE BOT:
Botkit has many features for building cool and useful bots!
Read all about it here:
-> http://howdy.ai/botkit
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/


if (!process.env.page_token) {
console.log('Error: Specify page_token in environment');
process.exit(1);
}

if (!process.env.verify_token) {
console.log('Error: Specify verify_token in environment');
process.exit(1);
}

if (!process.env.app_secret) {
console.log('Error: Specify app_secret in environment');
process.exit(1);
}

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

const ops = 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},
]);

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,
log: true,
access_token: 'EAAShWXeInZCcBAGD9z7ffW3Dmflwh2X8otTTH1R1OzBniNe6vQPyGduijVwk68Of3r1nZAVte76LdQ00P2qSCQip5hbnqxpaGZAFEibAiDZCHHd3JE0caYzD3cHP7U3rEnSSghMcTITCesrgsWWDhT6SyF7dl5HwJINvht0HFwZDZD',
verify_token: 'pass;1234',
app_secret: '197fc98968cd0197cd8c838e9b68e466',
access_token: process.env.page_token,
verify_token: process.env.verify_token,
app_secret: process.env.app_secret,
validate_requests: true, // Refuse any requests that don't come from FB on your receive webhook, must provide FB_APP_SECRET in environment variables
});

var bot = controller.spawn({
});

controller.setupWebserver(1337, function(err, webserver) {
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(['attachment_upload'], 'message_received', function(bot, message) {
var attachment = {
"type":"image",
"payload":{
"url":"https://pbs.twimg.com/profile_images/803642201653858305/IAW1DBPw_400x400.png",
"is_reusable": true
}
};

controller.api.attachment_upload.upload(attachment, function (err, attachmentId) {
if(err) {
// Error
} else {
var image = {
"attachment":{
"type":"image",
"payload": {
"attachment_id": attachmentId
}
}
};
bot.reply(message, image);
}
});
});

Expand Down Expand Up @@ -87,32 +227,6 @@ controller.hears(['code'], 'message_received,facebook_postback', function(bot, m
});
});

controller.hears(['attachment_upload'], 'message_received', function(bot, message) {
var attachment = {
"type":"image",
"payload":{
"url":"https://pbs.twimg.com/profile_images/803642201653858305/IAW1DBPw_400x400.png",
"is_reusable": true
}
};

controller.api.attachment_upload.upload(attachment, function (err, attachmentId) {
if(err) {
// Error
} else {
var image = {
"attachment":{
"type":"image",
"payload": {
"attachment_id": attachmentId
}
}
};
bot.reply(message, image);
}
});
});

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

bot.reply(message, {
Expand Down

0 comments on commit ba64ec6

Please sign in to comment.