Skip to content

Instantly share code, notes, and snippets.

@ishanbakshi
Created August 3, 2016 18:36
Show Gist options
  • Save ishanbakshi/dc027e7c6041f922623ee10322d68d84 to your computer and use it in GitHub Desktop.
Save ishanbakshi/dc027e7c6041f922623ee10322d68d84 to your computer and use it in GitHub Desktop.
'use strict';
var https = require('https');
var PAGE_TOKEN = "EAAJ6****";
var VERIFY_TOKEN = "my_token******";
exports.handler = (event, context, callback) => {
// process GET request
if(event.params && event.params.querystring){
var queryParams = event.params.querystring;
var rVerifyToken = queryParams['hub.verify_token']
if (rVerifyToken === VERIFY_TOKEN) {
var challenge = queryParams['hub.challenge']
callback(null, parseInt(challenge))
}else{
callback(null, 'Error, wrong validation token');
}
// process POST request
}else{
var messagingEvents = event.entry[0].messaging;
for (var i = 0; i < messagingEvents.length; i++) {
var messagingEvent = messagingEvents[i];
var sender = messagingEvent.sender.id;
if (messagingEvent.message && messagingEvent.message.text) {
var text = messagingEvent.message.text;
console.log("XXXXXX Receive a message: " + text);
parentMessageTextProcessor(sender,text);
callback(null, "Done")
}else {
console.log("Entered payload else");
//check for payloads
if(messagingEvent.postback && messagingEvent.postback.payload){
console.log("XXXXXX Payload checker : "+messagingEvent.postback.payload);
var payloadValue = messagingEvent.postback.payload;
thumbnailClickProcessor(sender, payloadValue);
}
}
}
callback(null, event);
}
};
/*
This function process al the thumbnail clicks
*/
function thumbnailClickProcessor(senderFbId, payloadValue){
switch(payloadValue){
case "get_fruits":
sendTextMessage(senderFbId, "so you selected fruits, wait a second ... I am fetching all the available fruits for you");
sendTextMessage(senderFbId, "you can now add these items to you cart");
showFruitList(senderFbId);
break;
case "get_deli":
sendTextMessage(senderFbId, "so you selected deli, I will get them for you");
showDeliList(senderFbId);
break;
case "get_bakery":
sendTextMessage(senderFbId, "awesome , you can now add these items to you cart");
showBakeryList(senderFbId);
break;
case "get_pantry":
sendTextMessage(senderFbId, "so you selected pantry");
showPantryList(senderFbId);
break;
}
}
/*
This function contains the basic workflow of the typed messages
*/
function parentMessageTextProcessor(sender, text){
var txtWords = text.split(" ");
if(text.toUpperCase().includes("BANAN")){
showBananaList(sender);
}else if(txtWords[0].toUpperCase() == "YES" || txtWords[0].toUpperCase() == "OK"){
//Redirect to the initial fruit catalog
showCatalogList(sender);
}
else if(text.toUpperCase().includes("HELLO") || txtWords[0].toUpperCase() =="HI"){
//process a welcome greeting
sendTextMessage(sender, "Woof Wooof ! My name is Guppy and I can help you find the best groceries in town ;) . \n "
+ "Type \" yes \" If you want to know \n ..... woof wooof wooof!");
}else if(text.toUpperCase().includes("WHAT") || text.toUpperCase().includes("WHY") || text.toUpperCase().includes("WHERE")){
//process an aplogy message
sendTextMessage(sender, "If you don't believe me, check it ut yourself at "
+" https://www.yourgrocer.com.au ");
}else if(text.toUpperCase().includes("PLAY")){
//process fun over here
sendTextMessage(sender, " woof woof ... throw me a ball @dailycute ");
}
else{
sendTextMessage(sender, "Woof Wooof. ... I am just a puppy and I don't understand everything. "
+" \n But one day I will be a big smart dog, and I will understand everything you say ....");
sendTextMessage(sender, "Type \' play \' if you want to play with me ...");
}
}
/*
Echo back a message
*/
function sendTextMessage(senderFbId, text) {
var json = {
recipient: {id: senderFbId},
message: {text: text},
};
var body = JSON.stringify(json);
var path = '/v2.6/me/messages?access_token=' + PAGE_TOKEN;
var options = {
host: "graph.facebook.com",
path: path,
method: 'POST',
headers: {'Content-Type': 'application/json'}
};
var callback = function(response) {
var str = ''
response.on('data', function (chunk) {
str += chunk;
});
response.on('end', function () {
});
}
var req = https.request(options, callback);
req.on('error', function(e) {
console.log('problem with request: '+ e);
});
req.write(body);
req.end();
}
/*
**
**this function returns list of bananas
*/
function showBananaList(senderFbId) {
console.log("start function function function function -- > showBananaList");
var json = {
recipient: {id: senderFbId},
message: {
"attachment":{
"type":"template",
"payload":{
"template_type":"generic",
"elements":[
{
"title":"Small Bananas",
"image_url":"https://res.cloudinary.com/yourgrocer/image/upload/c_limit,f_auto,h_100,w_100/v1435484836/xr3urxqls4k79r9jblge.jpg",
"subtitle":"Price : $ 0.58/kg",
"buttons":[
{
"type":"postback",
"title":"Select",
"payload":"add_banana1"
}
]
},
{
"title":"Lady Finder Bananas",
"image_url":"https://res.cloudinary.com/yourgrocer/image/upload/c_limit,f_auto,h_100,w_100/v1435484649/bl10m6vllkdtn1idmbr2.jpg",
"subtitle":"Price : $ 1.28/kg",
"buttons":[
{
"type":"postback",
"title":"Select",
"payload":"add_banana2"
}
]
},
{
"title":"Banana chips",
"image_url":"https://res.cloudinary.com/yourgrocer/image/upload/c_limit,f_auto,h_100,w_100/v1456378660/vfx5yvgcjtesrh983izh.jpg",
"subtitle":"Price : $ 7.50",
"buttons":[
{
"type":"postback",
"title":"Select",
"payload":"add_banana3"
}
]
}
]
}
}
},
};
var body = JSON.stringify(json);
var path = '/v2.6/me/messages?access_token=' + PAGE_TOKEN;
var options = {
host: "graph.facebook.com",
path: path,
method: 'POST',
headers: {'Content-Type': 'application/json'}
};
var callback = function(response) {
var str = ''
response.on('data', function (chunk) {
str += chunk;
});
response.on('end', function () {
});
}
var req = https.request(options, callback);
req.on('error', function(e) {
console.log('problem with request: '+ e);
});
req.write(body);
req.end();
}
/*
**
**this function returns list of bananas
*/
function showFruitList(senderFbId) {
console.log("start function function function function -- > showBananaList");
var json = {
recipient: {id: senderFbId},
message: {
"attachment":{
"type":"template",
"payload":{
"template_type":"generic",
"elements":[
{
"title":"Small Bananas",
"image_url":"https://res.cloudinary.com/yourgrocer/image/upload/c_limit,f_auto,h_100,w_100/v1435484836/xr3urxqls4k79r9jblge.jpg",
"subtitle":"Price : $ 0.58/kg",
"buttons":[
{
"type":"postback",
"title":"Add to cart",
"payload":"add_banana1"
}
]
},
{
"title":"Oranges",
"image_url":"http://us.123rf.com/450wm/cjung/cjung1206/cjung120600155/14215768-fresh-oranges.jpg?ver=6",
"subtitle":"Price : $ 1.28/kg",
"buttons":[
{
"type":"postback",
"title":"Add to cart",
"payload":"add_banana2"
}
]
},
{
"title":"Apples",
"image_url":"http://us.123rf.com/450wm/kovalevaka/kovalevaka1509/kovalevaka150900027/45293072-whole-green-apple-and-half-with-leaf-isolated-on-white-background-as-package-design-element.jpg?ver=6",
"subtitle":"Price : $ 7.50",
"buttons":[
{
"type":"postback",
"title":"Add to cart",
"payload":"add_banana3"
}
]
}
]
}
}
},
};
var body = JSON.stringify(json);
var path = '/v2.6/me/messages?access_token=' + PAGE_TOKEN;
var options = {
host: "graph.facebook.com",
path: path,
method: 'POST',
headers: {'Content-Type': 'application/json'}
};
var callback = function(response) {
var str = ''
response.on('data', function (chunk) {
str += chunk;
});
response.on('end', function () {
});
}
var req = https.request(options, callback);
req.on('error', function(e) {
console.log('problem with request: '+ e);
});
req.write(body);
req.end();
}
/*
**
**this function returns list of bananas
*/
function showDeliList(senderFbId) {
console.log("start function function function function -- > showBananaList");
var json = {
recipient: {id: senderFbId},
message: {
"attachment":{
"type":"template",
"payload":{
"template_type":"generic",
"elements":[
{
"title":"Leg ham",
"image_url":"http://us.123rf.com/450wm/alexan66/alexan661408/alexan66140800047/30648003-raw-pork-ham-pork-leg.jpg?ver=6",
"subtitle":"Price : $ 0.58/kg",
"buttons":[
{
"type":"postback",
"title":"Add to cart",
"payload":"add_banana1"
}
]
},
{
"title":"sausages",
"image_url":"http://us.123rf.com/450wm/magone/magone1010/magone101000038/8061453-sausages-and-meat.jpg?ver=6",
"subtitle":"Price : $ 1.28/kg",
"buttons":[
{
"type":"postback",
"title":"Add to cart",
"payload":"add_banana2"
}
]
}
]
}
}
},
};
var body = JSON.stringify(json);
var path = '/v2.6/me/messages?access_token=' + PAGE_TOKEN;
var options = {
host: "graph.facebook.com",
path: path,
method: 'POST',
headers: {'Content-Type': 'application/json'}
};
var callback = function(response) {
var str = ''
response.on('data', function (chunk) {
str += chunk;
});
response.on('end', function () {
});
}
var req = https.request(options, callback);
req.on('error', function(e) {
console.log('problem with request: '+ e);
});
req.write(body);
req.end();
}
/*
**
**this function returns list of bananas
*/
function showPantryList(senderFbId) {
console.log("start function function function function -- > showBananaList");
var json = {
recipient: {id: senderFbId},
message: {
"attachment":{
"type":"template",
"payload":{
"template_type":"generic",
"elements":[
{
"title":"Almond mix",
"image_url":"http://us.123rf.com/450wm/boarding1now/boarding1now1211/boarding1now121100046/16213556-collection-of-different-nuts-forming-a-background.jpg?ver=6",
"subtitle":"Price : $ 0.58/kg",
"buttons":[
{
"type":"postback",
"title":"Add to cart",
"payload":"add_banana1"
}
]
}
]
}
}
},
};
var body = JSON.stringify(json);
var path = '/v2.6/me/messages?access_token=' + PAGE_TOKEN;
var options = {
host: "graph.facebook.com",
path: path,
method: 'POST',
headers: {'Content-Type': 'application/json'}
};
var callback = function(response) {
var str = ''
response.on('data', function (chunk) {
str += chunk;
});
response.on('end', function () {
});
}
var req = https.request(options, callback);
req.on('error', function(e) {
console.log('problem with request: '+ e);
});
req.write(body);
req.end();
}
/*
**
**this function returns list of bakery items
*/
function showBakeryList(senderFbId) {
console.log("start function function function function -- > showBakeryList");
var json = {
recipient: {id: senderFbId},
message: {
"attachment":{
"type":"template",
"payload":{
"template_type":"generic",
"elements":[
{
"title":"Breads",
"image_url":"http://www.123rf.com/photo_38169803_crusty-homemade-ciabatta-bread.html?term=bread&vti=nm481mnntvcj7is1lh",
"subtitle":"Price : $ 0.58/kg",
"buttons":[
{
"type":"postback",
"title":"Add to cart",
"payload":"add_banana1"
}
]
},
{
"title":"Donuts",
"image_url":"http://us.123rf.com/450wm/magone/magone1210/magone121000003/15661557-baked-donuts.jpg?ver=6",
"subtitle":"Price : $ 1.28/kg",
"buttons":[
{
"type":"postback",
"title":"Add to cart",
"payload":"add_banana2"
}
]
}
]
}
}
},
};
var body = JSON.stringify(json);
var path = '/v2.6/me/messages?access_token=' + PAGE_TOKEN;
var options = {
host: "graph.facebook.com",
path: path,
method: 'POST',
headers: {'Content-Type': 'application/json'}
};
var callback = function(response) {
var str = ''
response.on('data', function (chunk) {
str += chunk;
});
response.on('end', function () {
});
}
var req = https.request(options, callback);
req.on('error', function(e) {
console.log('problem with request: '+ e);
});
req.write(body);
req.end();
}
/*
**
**this function returns list of product groups
*/
function showCatalogList(senderFbId) {
console.log("start function function function function -- > showCatalogList");
var json = {
recipient: {id: senderFbId},
message: {
"attachment":{
"type":"template",
"payload":{
"template_type":"generic",
"elements":[
{
"title":"Fruits & Veg\'s",
"image_url":"http://us.cdn1.123rf.com/168nwm/natis76/natis761508/natis76150800133/43946635-harvest-juicy-fruit-and-berries-vector-illustration-isolated-on-white.jpg",
"subtitle":"Freshest fruits in brunswick",
"buttons":[
{
"type":"postback",
"title":"Select",
"payload":"get_fruits"
}
]
},
{
"title":"Deli",
"image_url":"http://us.123rf.com/450wm/shutswis/shutswis1102/shutswis110200108/8783458-various-kinds-of-meat.jpg?ver=6",
"subtitle":"The best quality is delivered from the local grocers",
"buttons":[
{
"type":"postback",
"title":"Select",
"payload":"get_deli"
}
]
},
{
"title":"Eggs & Bakery",
"image_url":"http://us.123rf.com/450wm/fermate/fermate1501/fermate150100024/35375797-bread-rolls-in-a-paper-bag-on-a-rustic-wooden-table-fresh-from-the-bakery-for-breakfast.jpg?ver=6",
"subtitle":"Free range products here",
"buttons":[
{
"type":"postback",
"title":"Select",
"payload":"get_bakery"
}
]
},
{
"title":"Pantry",
"image_url":"http://us.123rf.com/450wm/oksix/oksix1203/oksix120300228/12891306-dried-fruits-and-nuts-heaps-on-wooden-table.jpg?ver=6",
"subtitle":"get some smart snacks",
"buttons":[
{
"type":"postback",
"title":"Select",
"payload":"get_pantry"
}
]
}
]
}
}
},
};
var body = JSON.stringify(json);
var path = '/v2.6/me/messages?access_token=' + PAGE_TOKEN;
var options = {
host: "graph.facebook.com",
path: path,
method: 'POST',
headers: {'Content-Type': 'application/json'}
};
var callback = function(response) {
var str = ''
response.on('data', function (chunk) {
str += chunk;
});
response.on('end', function () {
});
}
var req = https.request(options, callback);
req.on('error', function(e) {
console.log('problem with request: '+ e);
});
req.write(body);
req.end();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment