untitled notebook

node v8.11.1
version: master
endpointsharetweet
let { botId } = process.env; let { token } = process.env; let msg="Sorry"; let chatfuelbroadcast=require('chatfuel-broadcast').default; let url = require('url'); let req= require('request'); let requestprom= require('request-promise'); let createTextMsg = (text) => { if (text.constructor===Array) {        let messages=[]; let redirect_count=0; let redirect_to_blocks;        for (let i=0; i<text.length; i++) {         if (!text[i].redirect_to_blocks)           {               messages[i-redirect_count]={text: text[i]};           }           else           {               redirect_to_blocks = text[i].redirect_to_blocks;               redirect_count++;           }       }        if(redirect_to_blocks)        {            return (!messages[0]) ? {redirect_to_blocks} : { messages, redirect_to_blocks}; } else       { return { messages}; } } else { return { messages: [ { text } ] }; } } let sendResponse = ({ response, message }) => { response.writeHead(200, { 'Content-Type': 'application/json' }); response.end(JSON.stringify(message)); } let createChatfuelBroadcastUrl = (botId, userId) => `https://api.chatfuel.com/bots/${botId}/users/${userId}/send`; let cfbroadcast = ({botId, token, userId, blockId}) => { let chatfuelBroadcastUrl = createChatfuelBroadcastUrl(botId, userId); let query = Object.assign( {}, { chatfuel_token: token, chatfuel_block_name: blockId, }); let chatfuelApiUrl = url.format({ pathname: chatfuelBroadcastUrl, query, }); console.log(chatfuelApiUrl); let requestOptions = { uri: chatfuelApiUrl, headers: { 'Content-Type': 'application/json', }, json: true, }; try { req.post(requestOptions).on('response', function(response) { console.log(response.statusCode); msg="Successfully sent the confirmation message"; }); } catch(error) { console.log(error.message); msg=error.message; cfbroadcast({botId, token, userId, blockId}); } } exports.endpoint = (request,response) => { let query = url.parse(request.url, true).query; let coordinator = (query['coordinator']==="Yes") ? true : false; let confirmation = (query['confirmation']==="Yes") ? true : false; let userId = query['messenger user id']; let blockId = (confirmation) ? "Authenticated" : "Wrong Code"; let options = {botId, token, userId, blockId}; console.log(coordinator); if(!coordinator) { msg=[{redirect_to_blocks : ["Not Coordinator"]}]; let message = createTextMsg(msg); } else { chatfuelbroadcast(options) .then(() => { console.log('Call succeeded'); msg="Successfully sent the confirmation message"; },(error) => { console.log(error); if(error.name==="StatusCodeError") { if(error.message.slice(0,3)==="422") { msg=error.message.slice(6); var errobj=JSON.parse(msg); msg=errobj.result.slice(10); console.log(msg); } else { msg=error.message; } } else { msg=error.message; } }); } let message = createTextMsg(msg); sendResponse({response, message}); }
Loading…

no comments

    sign in to comment