Skip to content

Commit

Permalink
add env var
Browse files Browse the repository at this point in the history
  • Loading branch information
afrizaloky committed Oct 25, 2020
1 parent 9a559bf commit 79bae5e
Showing 1 changed file with 172 additions and 50 deletions.
222 changes: 172 additions & 50 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,65 +1,187 @@
'use strict';

/**
* Copyright 2017-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the license found in the
* LICENSE file in the root directory of this source tree.
*
* Messenger Platform Quick Start Tutorial
*
* This is the completed code for the Messenger Platform quick start tutorial
*
* https://developers.facebook.com/docs/messenger-platform/getting-started/quick-start/
*
* To run this code, you must do the following:
*
* 1. Deploy this code to a server running Node.js
* 2. Run `npm install`
* 3. Update the VERIFY_TOKEN
* 4. Add your PAGE_ACCESS_TOKEN to your environment vars
*
*/

require('dotenv').config()

'use strict';
const PAGE_ACCESS_TOKEN = process.env.PAGE_ACCESS_TOKEN;
// Imports dependencies and set up http server
const
const
request = require('request'),
express = require('express'),
bodyParser = require('body-parser'),
app = express().use(bodyParser.json()); // creates express http server
body_parser = require('body-parser'),
app = express().use(body_parser.json()); // creates express http server

// Sets server port and logs message on success
app.listen(process.env.PORT || 1337, () => console.log('webhook is listening'));

// Creates the endpoint for our webhook
// Accepts POST requests at /webhook endpoint
app.post('/webhook', (req, res) => {

let body = req.body;

// Checks this is an event from a page subscription
if (body.object === 'page') {

// Parse the request body from the POST
let body = req.body;

// Check the webhook event is from a Page subscription
if (body.object === 'page') {

body.entry.forEach(function(entry) {

// Gets the body of the webhook event
let webhook_event = entry.messaging[0];
console.log(webhook_event);


// Get the sender PSID
let sender_psid = webhook_event.sender.id;
console.log('Sender ID: ' + sender_psid);

// Check if the event is a message or postback and
// pass the event to the appropriate handler function
if (webhook_event.message) {
handleMessage(sender_psid, webhook_event.message);
} else if (webhook_event.postback) {

handlePostback(sender_psid, webhook_event.postback);
}

});
// Return a '200 OK' response to all events
res.status(200).send('EVENT_RECEIVED');

} else {
// Return a '404 Not Found' if event is not from a page subscription
res.sendStatus(404);
}

});

// Accepts GET requests at the /webhook endpoint
app.get('/webhook', (req, res) => {

// Iterates over each entry - there may be multiple if batched
body.entry.forEach(function(entry) {
/** UPDATE YOUR VERIFY TOKEN **/
const VERIFY_TOKEN = process.env.VERIFY_TOKEN;

// Gets the message. entry.messaging is an array, but
// will only ever contain one message, so we get index 0
let webhook_event = entry.messaging[0];
console.log(webhook_event);
});
// Parse params from the webhook verification request
let mode = req.query['hub.mode'];
let token = req.query['hub.verify_token'];
let challenge = req.query['hub.challenge'];

// Check if a token and mode were sent
if (mode && token) {

// Returns a '200 OK' response to all requests
res.status(200).send('EVENT_RECEIVED');
// Check the mode and token sent are correct
if (mode === 'subscribe' && token === VERIFY_TOKEN) {

// Respond with 200 OK and challenge token from the request
console.log('WEBHOOK_VERIFIED');
res.status(200).send(challenge);

} else {
// Returns a '404 Not Found' if event is not from a page subscription
res.sendStatus(404);
// Responds with '403 Forbidden' if verify tokens do not match
res.sendStatus(403);
}

});

// Adds support for GET requests to our webhook
app.get('/webhook', (req, res) => {
}
});

// Your verify token. Should be a random string.
let VERIFY_TOKEN = "verysecret"

// Parse the query params
let mode = req.query['hub.mode'];
let token = req.query['hub.verify_token'];
let challenge = req.query['hub.challenge'];

// Checks if a token and mode is in the query string of the request
if (mode && token) {

// Checks the mode and token sent is correct
if (mode === 'subscribe' && token === VERIFY_TOKEN) {

// Responds with the challenge token from the request
console.log('WEBHOOK_VERIFIED');
res.status(200).send(challenge);

} else {
// Responds with '403 Forbidden' if verify tokens do not match
res.sendStatus(403);
function handleMessage(sender_psid, received_message) {
let response;

// Checks if the message contains text
if (received_message.text) {
// Create the payload for a basic text message, which
// will be added to the body of our request to the Send API
response = {
"text": `You sent the message: "${received_message.text}". Now send me an attachment!`
}
} else if (received_message.attachments) {
// Get the URL of the message attachment
let attachment_url = received_message.attachments[0].payload.url;
response = {
"attachment": {
"type": "template",
"payload": {
"template_type": "generic",
"elements": [{
"title": "Is this the right picture?",
"subtitle": "Tap a button to answer.",
"image_url": attachment_url,
"buttons": [
{
"type": "postback",
"title": "Yes!",
"payload": "yes",
},
{
"type": "postback",
"title": "No!",
"payload": "no",
}
],
}]
}
}
}
});
}

// Send the response message
callSendAPI(sender_psid, response);
}

// Sets server port and logs message on success
app.listen(process.env.PORT || 1337, () => console.log('webhook is listening'));
function handlePostback(sender_psid, received_postback) {
console.log('ok')
let response;
// Get the payload for the postback
let payload = received_postback.payload;

// Set the response based on the postback payload
if (payload === 'yes') {
response = { "text": "Thanks!" }
} else if (payload === 'no') {
response = { "text": "Oops, try sending another image." }
}
// Send the message to acknowledge the postback
callSendAPI(sender_psid, response);
}

function callSendAPI(sender_psid, response) {
// Construct the message body
let request_body = {
"recipient": {
"id": sender_psid
},
"message": response
}

// Send the HTTP request to the Messenger Platform
request({
"uri": "https://graph.facebook.com/v2.6/me/messages",
"qs": { "access_token": PAGE_ACCESS_TOKEN },
"method": "POST",
"json": request_body
}, (err, res, body) => {
if (!err) {
console.log('message sent!')
} else {
console.error("Unable to send message:" + err);
}
});
}

0 comments on commit 79bae5e

Please sign in to comment.