Advertisement
tusaveeiei

fb-webhook

Aug 16th, 2017
553
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. 'use strict';
  3.  
  4. const
  5.     bodyParser = require('body-parser'),
  6.     config = require('config'),
  7.     crypto = require('crypto'),
  8.     express = require('express'),
  9.     https = require('https'),
  10.     request = require('request');
  11.  
  12. var app = express();
  13. app.set('port', process.env.PORT || 5000);
  14. app.set('view engine', 'ejs');
  15. app.use(bodyParser.json({ verify: verifyRequestSignature }));
  16. app.use(express.static('public'));
  17.  
  18.  
  19. // App Secret can be retrieved from the App Dashboard
  20. const APP_SECRET = (process.env.MESSENGER_APP_SECRET) ?
  21.     process.env.MESSENGER_APP_SECRET :
  22.     config.get('appSecret');
  23.  
  24. // Arbitrary value used to validate a webhook
  25. const VALIDATION_TOKEN = (process.env.MESSENGER_VALIDATION_TOKEN) ?
  26.     (process.env.MESSENGER_VALIDATION_TOKEN) :
  27.     config.get('validationToken');
  28.  
  29. // Generate a page access token for your page from the App Dashboard
  30. const PAGE_ACCESS_TOKEN = (process.env.MESSENGER_PAGE_ACCESS_TOKEN) ?
  31.     (process.env.MESSENGER_PAGE_ACCESS_TOKEN) :
  32.     config.get('pageAccessToken');
  33.  
  34. // URL where the app is running (include protocol). Used to point to scripts and
  35. // assets located at this address.
  36. const SERVER_URL = (process.env.SERVER_URL) ?
  37.     (process.env.SERVER_URL) :
  38.     config.get('serverURL');
  39.  
  40. if (!(APP_SECRET && VALIDATION_TOKEN && PAGE_ACCESS_TOKEN && SERVER_URL)) {
  41.     console.error("Missing config values");
  42.     process.exit(1);
  43. }
  44.  
  45. /*
  46.  * Use your own validation token. Check that the token used in the Webhook
  47.  * setup is the same token used here.
  48.  *
  49.  */
  50. app.get('/webhook', function(req, res) {
  51.     if (req.query['hub.mode'] === 'subscribe' &&
  52.         req.query['hub.verify_token'] === VALIDATION_TOKEN) {
  53.         console.log("Validating webhook");
  54.         res.status(200).send(req.query['hub.challenge']);
  55.     } else {
  56.         console.error("Failed validation. Make sure the validation tokens match.");
  57.         res.sendStatus(403);
  58.     }
  59. });
  60.  
  61.  
  62. /*
  63.  * All callbacks for Messenger are POST-ed. They will be sent to the same
  64.  * webhook. Be sure to subscribe your app to your page to receive callbacks
  65.  * for your page.
  66.  * https://developers.facebook.com/docs/messenger-platform/product-overview/setup#subscribe_app
  67.  *
  68.  */
  69. app.post('/webhook', function(req, res) {
  70.     var data = req.body;
  71.  
  72.     // Make sure this is a page subscription
  73.     if (data.object == 'page') {
  74.         // Iterate over each entry
  75.         // There may be multiple if batched
  76.         data.entry.forEach(function(pageEntry) {
  77.             var pageID = pageEntry.id;
  78.             var timeOfEvent = pageEntry.time;
  79.  
  80.             // Iterate over each messaging event
  81.             pageEntry.messaging.forEach(function(messagingEvent) {
  82.                 if (messagingEvent.optin) {
  83.                     receivedAuthentication(messagingEvent);
  84.                 } else if (messagingEvent.message) {
  85.                     receivedMessage(messagingEvent);
  86.                 } else if (messagingEvent.delivery) {
  87.                     receivedDeliveryConfirmation(messagingEvent);
  88.                 } else if (messagingEvent.postback) {
  89.                     receivedPostback(messagingEvent);
  90.                 } else if (messagingEvent.read) {
  91.                     receivedMessageRead(messagingEvent);
  92.                 } else if (messagingEvent.account_linking) {
  93.                     receivedAccountLink(messagingEvent);
  94.                 } else {
  95.                     console.log("Webhook received unknown messagingEvent: ", messagingEvent);
  96.                 }
  97.             });
  98.         });
  99.  
  100.         // Assume all went well.
  101.         //
  102.         // You must send back a 200, within 20 seconds, to let us know you've
  103.         // successfully received the callback. Otherwise, the request will time out.
  104.         res.sendStatus(200);
  105.     }
  106. });
  107.  
  108. /*
  109.  * This path is used for account linking. The account linking call-to-action
  110.  * (sendAccountLinking) is pointed to this URL.
  111.  *
  112.  */
  113. app.get('/authorize', function(req, res) {
  114.     var accountLinkingToken = req.query.account_linking_token;
  115.     var redirectURI = req.query.redirect_uri;
  116.  
  117.     // Authorization Code should be generated per user by the developer. This will
  118.     // be passed to the Account Linking callback.
  119.     var authCode = "1234567890";
  120.  
  121.     // Redirect users to this URI on successful login
  122.     var redirectURISuccess = redirectURI + "&authorization_code=" + authCode;
  123.  
  124.     res.render('authorize', {
  125.         accountLinkingToken: accountLinkingToken,
  126.         redirectURI: redirectURI,
  127.         redirectURISuccess: redirectURISuccess
  128.     });
  129. });
  130.  
  131. function verifyRequestSignature(req, res, buf) {
  132.     var signature = req.headers["x-hub-signature"];
  133.  
  134.     if (!signature) {
  135.         // For testing, let's log an error. In production, you should throw an
  136.         // error.
  137.         console.error("Couldn't validate the signature.");
  138.     } else {
  139.         var elements = signature.split('=');
  140.         var method = elements[0];
  141.         var signatureHash = elements[1];
  142.  
  143.         var expectedHash = crypto.createHmac('sha1', APP_SECRET)
  144.             .update(buf)
  145.             .digest('hex');
  146.  
  147.         if (signatureHash != expectedHash) {
  148.             throw new Error("Couldn't validate the request signature.");
  149.         }
  150.     }
  151. }
  152.  
  153. function receivedAuthentication(event) {
  154.     var senderID = event.sender.id;
  155.     var recipientID = event.recipient.id;
  156.     var timeOfAuth = event.timestamp;
  157.  
  158.     var passThroughParam = event.optin.ref;
  159.  
  160.     console.log("Received authentication for user %d and page %d with pass " +
  161.         "through param '%s' at %d", senderID, recipientID, passThroughParam,
  162.         timeOfAuth);
  163.  
  164.     // When an authentication is received, we'll send a message back to the sender
  165.     // to let them know it was successful.
  166.     sendTextMessage(senderID, "Authentication successful");
  167. }
  168.  
  169. function receivedMessage(event) {
  170.     var senderID = event.sender.id;
  171.     var recipientID = event.recipient.id;
  172.     var timeOfMessage = event.timestamp;
  173.     var message = event.message;
  174.  
  175.     console.log("Received message for user %d and page %d at %d with message:",
  176.         senderID, recipientID, timeOfMessage);
  177.     console.log(JSON.stringify(message));
  178.  
  179.     var isEcho = message.is_echo;
  180.     var messageId = message.mid;
  181.     var appId = message.app_id;
  182.     var metadata = message.metadata;
  183.  
  184.     // You may get a text or attachment but not both
  185.     var messageText = message.text;
  186.     var messageAttachments = message.attachments;
  187.     var quickReply = message.quick_reply;
  188.  
  189.     if (isEcho) {
  190.         // Just logging message echoes to console
  191.         console.log("Received echo for message %s and app %d with metadata %s",
  192.             messageId, appId, metadata);
  193.         return;
  194.     } else if (quickReply) {
  195.         var quickReplyPayload = quickReply.payload;
  196.         console.log("Quick reply for message %s with payload %s",
  197.             messageId, quickReplyPayload);
  198.  
  199.         sendTextMessage(senderID, "Quick reply tapped");
  200.         return;
  201.     }
  202.  
  203.     if (messageText) {
  204.  
  205.         switch (messageText) {
  206.             case 'image':
  207.                 sendImageMessage(senderID);
  208.                 break;
  209.  
  210.             case 'gif':
  211.                 sendGifMessage(senderID);
  212.                 break;
  213.  
  214.             case 'audio':
  215.                 sendAudioMessage(senderID);
  216.                 break;
  217.  
  218.             case 'video':
  219.                 sendVideoMessage(senderID);
  220.                 break;
  221.  
  222.             case 'file':
  223.                 sendFileMessage(senderID);
  224.                 break;
  225.  
  226.             case 'button':
  227.                 sendButtonMessage(senderID);
  228.                 break;
  229.  
  230.             case 'generic':
  231.                 sendGenericMessage(senderID);
  232.                 break;
  233.  
  234.             case 'receipt':
  235.                 sendReceiptMessage(senderID);
  236.                 break;
  237.  
  238.             case 'quick reply':
  239.                 sendQuickReply(senderID);
  240.                 break;
  241.  
  242.             case 'read receipt':
  243.                 sendReadReceipt(senderID);
  244.                 break;
  245.  
  246.             case 'typing on':
  247.                 sendTypingOn(senderID);
  248.                 break;
  249.  
  250.             case 'typing off':
  251.                 sendTypingOff(senderID);
  252.                 break;
  253.  
  254.             case 'account linking':
  255.                 sendAccountLinking(senderID);
  256.                 break;
  257.  
  258.             default:
  259.                 getJSONFromAPI(senderID, messageText);
  260.         }
  261.     } else if (messageAttachments) {
  262.         sendTextMessage(senderID, "Message with attachment received");
  263.     }
  264. }
  265.  
  266.  
  267. /*
  268.  * Delivery Confirmation Event
  269.  *
  270.  * This event is sent to confirm the delivery of a message. Read more about
  271.  * these fields at https://developers.facebook.com/docs/messenger-platform/webhook-reference/message-delivered
  272.  *
  273.  */
  274. function receivedDeliveryConfirmation(event) {
  275.     var senderID = event.sender.id;
  276.     var recipientID = event.recipient.id;
  277.     var delivery = event.delivery;
  278.     var messageIDs = delivery.mids;
  279.     var watermark = delivery.watermark;
  280.     var sequenceNumber = delivery.seq;
  281.  
  282.     if (messageIDs) {
  283.         messageIDs.forEach(function(messageID) {
  284.             console.log("Received delivery confirmation for message ID: %s",
  285.                 messageID);
  286.         });
  287.     }
  288.  
  289.     console.log("All message before %d were delivered.", watermark);
  290. }
  291.  
  292.  
  293. /*
  294.  * Postback Event
  295.  *
  296.  * This event is called when a postback is tapped on a Structured Message.
  297.  * https://developers.facebook.com/docs/messenger-platform/webhook-reference/postback-received
  298.  *
  299.  */
  300. function receivedPostback(event) {
  301.     var senderID = event.sender.id;
  302.     var recipientID = event.recipient.id;
  303.     var timeOfPostback = event.timestamp;
  304.  
  305.     // The 'payload' param is a developer-defined field which is set in a postback
  306.     // button for Structured Messages.
  307.     var payload = event.postback.payload;
  308.  
  309.     console.log("Received postback for user %d and page %d with payload '%s' " +
  310.         "at %d", senderID, recipientID, payload, timeOfPostback);
  311.  
  312.     // When a postback is called, we'll send a message back to the sender to
  313.     // let them know it was successful
  314.     sendTextMessage(senderID, "Postback called");
  315. }
  316.  
  317. /*
  318.  * Message Read Event
  319.  *
  320.  * This event is called when a previously-sent message has been read.
  321.  * https://developers.facebook.com/docs/messenger-platform/webhook-reference/message-read
  322.  *
  323.  */
  324. function receivedMessageRead(event) {
  325.     var senderID = event.sender.id;
  326.     var recipientID = event.recipient.id;
  327.  
  328.     // All messages before watermark (a timestamp) or sequence have been seen.
  329.     var watermark = event.read.watermark;
  330.     var sequenceNumber = event.read.seq;
  331.  
  332.     console.log("Received message read event for watermark %d and sequence " +
  333.         "number %d", watermark, sequenceNumber);
  334. }
  335.  
  336. function receivedAccountLink(event) {
  337.     var senderID = event.sender.id;
  338.     var recipientID = event.recipient.id;
  339.  
  340.     var status = event.account_linking.status;
  341.     var authCode = event.account_linking.authorization_code;
  342.  
  343.     console.log("Received account link event with for user %d with status %s " +
  344.         "and auth code %s ", senderID, status, authCode);
  345. }
  346.  
  347. /*
  348.  * Send an image using the Send API.
  349.  *
  350.  */
  351. function sendImageMessage(recipientId) {
  352.     var messageData = {
  353.         recipient: {
  354.             id: recipientId
  355.         },
  356.         message: {
  357.             attachment: {
  358.                 type: "image",
  359.                 payload: {
  360.                     url: SERVER_URL + "/assets/rift.png"
  361.                 }
  362.             }
  363.         }
  364.     };
  365.  
  366.     callSendAPI(messageData);
  367. }
  368.  
  369. /*
  370.  * Send a Gif using the Send API.
  371.  *
  372.  */
  373. function sendGifMessage(recipientId) {
  374.     var messageData = {
  375.         recipient: {
  376.             id: recipientId
  377.         },
  378.         message: {
  379.             attachment: {
  380.                 type: "image",
  381.                 payload: {
  382.                     url: SERVER_URL + "/assets/instagram_logo.gif"
  383.                 }
  384.             }
  385.         }
  386.     };
  387.  
  388.     callSendAPI(messageData);
  389. }
  390.  
  391. /*
  392.  * Send audio using the Send API.
  393.  *
  394.  */
  395. function sendAudioMessage(recipientId) {
  396.     var messageData = {
  397.         recipient: {
  398.             id: recipientId
  399.         },
  400.         message: {
  401.             attachment: {
  402.                 type: "audio",
  403.                 payload: {
  404.                     url: SERVER_URL + "/assets/sample.mp3"
  405.                 }
  406.             }
  407.         }
  408.     };
  409.  
  410.     callSendAPI(messageData);
  411. }
  412.  
  413. /*
  414.  * Send a video using the Send API.
  415.  *
  416.  */
  417. function sendVideoMessage(recipientId) {
  418.     var messageData = {
  419.         recipient: {
  420.             id: recipientId
  421.         },
  422.         message: {
  423.             attachment: {
  424.                 type: "video",
  425.                 payload: {
  426.                     url: SERVER_URL + "/assets/allofus480.mov"
  427.                 }
  428.             }
  429.         }
  430.     };
  431.  
  432.     callSendAPI(messageData);
  433. }
  434.  
  435. /*
  436.  * Send a file using the Send API.
  437.  *
  438.  */
  439. function sendFileMessage(recipientId) {
  440.     var messageData = {
  441.         recipient: {
  442.             id: recipientId
  443.         },
  444.         message: {
  445.             attachment: {
  446.                 type: "file",
  447.                 payload: {
  448.                     url: SERVER_URL + "/assets/test.txt"
  449.                 }
  450.             }
  451.         }
  452.     };
  453.  
  454.     callSendAPI(messageData);
  455. }
  456.  
  457. /*
  458.  * Send a text message using the Send API.
  459.  *
  460.  */
  461.  
  462. function getJSONFromAPI(recipientId, messageText) {
  463.     var toSendMessage = messageText;
  464.     var url = 'http://52.221.92.113/api/bot/somsri/synapes?';
  465.     var queryObject = JSON.stringify({
  466.         msg: messageText,
  467.         user: ""
  468.     });
  469.  
  470.     request.get({
  471.         url: url,
  472.         qs: { data: queryObject },
  473.         json: true,
  474.         headers: { 'User-Agent': 'request' }
  475.     }, (err, res, data) => {
  476.         if (err) {
  477.             console.log('Error:', err);
  478.         } else if (res.statusCode !== 200) {
  479.             console.log('Status:', res.statusCode);
  480.         } else {
  481.             sendTextMessage(recipientId, data.data);
  482.             console.log(data.data);
  483.         }
  484.     });
  485. }
  486.  
  487. function sendTextMessage(recipientId, messageText) {
  488.     var messageData = {
  489.         recipient: {
  490.             id: recipientId
  491.         },
  492.         message: {
  493.             text: messageText,
  494.             metadata: "DEVELOPER_DEFINED_METADATA"
  495.         }
  496.     };
  497.  
  498.     callSendAPI(messageData);
  499. }
  500.  
  501. /*
  502.  * Send a button message using the Send API.
  503.  *
  504.  */
  505. function sendButtonMessage(recipientId) {
  506.     var messageData = {
  507.         recipient: {
  508.             id: recipientId
  509.         },
  510.         message: {
  511.             attachment: {
  512.                 type: "template",
  513.                 payload: {
  514.                     template_type: "button",
  515.                     text: "This is test text",
  516.                     buttons: [{
  517.                         type: "web_url",
  518.                         url: "https://www.oculus.com/en-us/rift/",
  519.                         title: "Open Web URL"
  520.                     }, {
  521.                         type: "postback",
  522.                         title: "Trigger Postback",
  523.                         payload: "DEVELOPER_DEFINED_PAYLOAD"
  524.                     }, {
  525.                         type: "phone_number",
  526.                         title: "Call Phone Number",
  527.                         payload: "+16505551234"
  528.                     }]
  529.                 }
  530.             }
  531.         }
  532.     };
  533.  
  534.     callSendAPI(messageData);
  535. }
  536.  
  537. /*
  538.  * Send a Structured Message (Generic Message type) using the Send API.
  539.  *
  540.  */
  541. function sendGenericMessage(recipientId) {
  542.     var messageData = {
  543.         recipient: {
  544.             id: recipientId
  545.         },
  546.         message: {
  547.             attachment: {
  548.                 type: "template",
  549.                 payload: {
  550.                     template_type: "generic",
  551.                     elements: [{
  552.                         title: "rift",
  553.                         subtitle: "Next-generation virtual reality",
  554.                         item_url: "https://www.oculus.com/en-us/rift/",
  555.                         image_url: SERVER_URL + "/assets/rift.png",
  556.                         buttons: [{
  557.                             type: "web_url",
  558.                             url: "https://www.oculus.com/en-us/rift/",
  559.                             title: "Open Web URL"
  560.                         }, {
  561.                             type: "postback",
  562.                             title: "Call Postback",
  563.                             payload: "Payload for first bubble",
  564.                         }],
  565.                     }, {
  566.                         title: "touch",
  567.                         subtitle: "Your Hands, Now in VR",
  568.                         item_url: "https://www.oculus.com/en-us/touch/",
  569.                         image_url: SERVER_URL + "/assets/touch.png",
  570.                         buttons: [{
  571.                             type: "web_url",
  572.                             url: "https://www.oculus.com/en-us/touch/",
  573.                             title: "Open Web URL"
  574.                         }, {
  575.                             type: "postback",
  576.                             title: "Call Postback",
  577.                             payload: "Payload for second bubble",
  578.                         }]
  579.                     }]
  580.                 }
  581.             }
  582.         }
  583.     };
  584.  
  585.     callSendAPI(messageData);
  586. }
  587.  
  588. /*
  589.  * Send a receipt message using the Send API.
  590.  *
  591.  */
  592. function sendReceiptMessage(recipientId) {
  593.     // Generate a random receipt ID as the API requires a unique ID
  594.     var receiptId = "order" + Math.floor(Math.random() * 1000);
  595.  
  596.     var messageData = {
  597.         recipient: {
  598.             id: recipientId
  599.         },
  600.         message: {
  601.             attachment: {
  602.                 type: "template",
  603.                 payload: {
  604.                     template_type: "receipt",
  605.                     recipient_name: "Peter Chang",
  606.                     order_number: receiptId,
  607.                     currency: "USD",
  608.                     payment_method: "Visa 1234",
  609.                     timestamp: "1428444852",
  610.                     elements: [{
  611.                         title: "Oculus Rift",
  612.                         subtitle: "Includes: headset, sensor, remote",
  613.                         quantity: 1,
  614.                         price: 599.00,
  615.                         currency: "USD",
  616.                         image_url: SERVER_URL + "/assets/riftsq.png"
  617.                     }, {
  618.                         title: "Samsung Gear VR",
  619.                         subtitle: "Frost White",
  620.                         quantity: 1,
  621.                         price: 99.99,
  622.                         currency: "USD",
  623.                         image_url: SERVER_URL + "/assets/gearvrsq.png"
  624.                     }],
  625.                     address: {
  626.                         street_1: "1 Hacker Way",
  627.                         street_2: "",
  628.                         city: "Menlo Park",
  629.                         postal_code: "94025",
  630.                         state: "CA",
  631.                         country: "US"
  632.                     },
  633.                     summary: {
  634.                         subtotal: 698.99,
  635.                         shipping_cost: 20.00,
  636.                         total_tax: 57.67,
  637.                         total_cost: 626.66
  638.                     },
  639.                     adjustments: [{
  640.                         name: "New Customer Discount",
  641.                         amount: -50
  642.                     }, {
  643.                         name: "$100 Off Coupon",
  644.                         amount: -100
  645.                     }]
  646.                 }
  647.             }
  648.         }
  649.     };
  650.  
  651.     callSendAPI(messageData);
  652. }
  653.  
  654. /*
  655.  * Send a message with Quick Reply buttons.
  656.  *
  657.  */
  658. function sendQuickReply(recipientId) {
  659.     var messageData = {
  660.         recipient: {
  661.             id: recipientId
  662.         },
  663.         message: {
  664.             text: "What's your favorite movie genre?",
  665.             quick_replies: [{
  666.                     "content_type": "text",
  667.                     "title": "Action",
  668.                     "payload": "DEVELOPER_DEFINED_PAYLOAD_FOR_PICKING_ACTION"
  669.                 },
  670.                 {
  671.                     "content_type": "text",
  672.                     "title": "Comedy",
  673.                     "payload": "DEVELOPER_DEFINED_PAYLOAD_FOR_PICKING_COMEDY"
  674.                 },
  675.                 {
  676.                     "content_type": "text",
  677.                     "title": "Drama",
  678.                     "payload": "DEVELOPER_DEFINED_PAYLOAD_FOR_PICKING_DRAMA"
  679.                 }
  680.             ]
  681.         }
  682.     };
  683.  
  684.     callSendAPI(messageData);
  685. }
  686.  
  687. /*
  688.  * Send a read receipt to indicate the message has been read
  689.  *
  690.  */
  691. function sendReadReceipt(recipientId) {
  692.     console.log("Sending a read receipt to mark message as seen");
  693.  
  694.     var messageData = {
  695.         recipient: {
  696.             id: recipientId
  697.         },
  698.         sender_action: "mark_seen"
  699.     };
  700.  
  701.     callSendAPI(messageData);
  702. }
  703.  
  704. /*
  705.  * Turn typing indicator on
  706.  *
  707.  */
  708. function sendTypingOn(recipientId) {
  709.     console.log("Turning typing indicator on");
  710.  
  711.     var messageData = {
  712.         recipient: {
  713.             id: recipientId
  714.         },
  715.         sender_action: "typing_on"
  716.     };
  717.  
  718.     callSendAPI(messageData);
  719. }
  720.  
  721. /*
  722.  * Turn typing indicator off
  723.  *
  724.  */
  725. function sendTypingOff(recipientId) {
  726.     console.log("Turning typing indicator off");
  727.  
  728.     var messageData = {
  729.         recipient: {
  730.             id: recipientId
  731.         },
  732.         sender_action: "typing_off"
  733.     };
  734.  
  735.     callSendAPI(messageData);
  736. }
  737.  
  738. /*
  739.  * Send a message with the account linking call-to-action
  740.  *
  741.  */
  742. function sendAccountLinking(recipientId) {
  743.     var messageData = {
  744.         recipient: {
  745.             id: recipientId
  746.         },
  747.         message: {
  748.             attachment: {
  749.                 type: "template",
  750.                 payload: {
  751.                     template_type: "button",
  752.                     text: "Welcome. Link your account.",
  753.                     buttons: [{
  754.                         type: "account_link",
  755.                         url: SERVER_URL + "/authorize"
  756.                     }]
  757.                 }
  758.             }
  759.         }
  760.     };
  761.  
  762.     callSendAPI(messageData);
  763. }
  764.  
  765. /*
  766.  * Call the Send API. The message data goes in the body. If successful, we'll
  767.  * get the message id in a response
  768.  *
  769.  */
  770. function callSendAPI(messageData) {
  771.     request({
  772.         uri: 'https://graph.facebook.com/v2.6/me/messages',
  773.         qs: { access_token: PAGE_ACCESS_TOKEN },
  774.         method: 'POST',
  775.         json: messageData
  776.  
  777.     }, function(error, response, body) {
  778.         if (!error && response.statusCode == 200) {
  779.             var recipientId = body.recipient_id;
  780.             var messageId = body.message_id;
  781.  
  782.             if (messageId) {
  783.                 console.log("Successfully sent message with id %s to recipient %s",
  784.                     messageId, recipientId);
  785.             } else {
  786.                 console.log("Successfully called Send API for recipient %s",
  787.                     recipientId);
  788.             }
  789.         } else {
  790.             console.error("Failed calling Send API", response.statusCode, response.statusMessage, body.error);
  791.         }
  792.     });
  793. }
  794.  
  795. // Start server
  796. // Webhooks must be available via SSL with a certificate signed by a valid
  797. // certificate authority.
  798. app.listen(app.get('port'), function() {
  799.     console.log('Node app is running on port', app.get('port'));
  800. });
  801.  
  802. module.exports = app;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement