Creating A Twilio Function to trigger 2 webhook endpoints (Autopilot & FrontApp) For Incoming SMS

北战南征 提交于 2021-02-05 11:29:06

问题


I want to create a Twilio Function that will trigger two webhook endpoints for AutoPilotHQ & FrontApp.

I've tried what's suggested here which suggest create the following function. I did make sure to include the dependencies as well.

const got = require('got');

exports.handler = function(context, event, callback) {
  let twiml = new Twilio.twiml.MessagingResponse();
  Promise.all([
    got.post(FIRST_URL, { body: JSON.stringify(event) }),
    got.post(SECOND_URL, { body: JSON.stringify(event) })
  ]).then(responses => callback(null, twiml));
};

I'm getting the following error I get from twilio is:

UnhandledPromiseRejectionWarning: Unhandled promise rejection: HTTPError: Response code 500 (Internal Server Error)
at PromisableRequest.request.once (/var/task/node_modules/got/dist/source/as-promise/index.js:124:28)
at process._tickCallback (internal/process/next_tick.js:68:7)

The URLs webhooks are specific to FrontApp & AutopilotHQ respectively.


回答1:


Does this below Twilio Function work?

Make sure you add querystring and axios as Twilio Function dependencies.

const axios = require('axios');
const qs = require('querystring');

exports.handler = function(context, event, callback) {

  let twiml = new Twilio.twiml.MessagingResponse();

  let { 
      ApiVersion,
      SmsSid,
      SmsStatus,
      SmsMessageSid,
      NumSegments,
      ToState,
      From,
      MessageSid,
      AccountSid,
      ToCity,
      FromCountry,
      ToZip,
      FromCity,
      To,
      FromZip,
      ToCountry,
      Body,
      NumMedia,
      FromState 
  } = event;

  let requestBody = {
      ApiVersion,
      SmsSid,
      SmsStatus,
      SmsMessageSid,
      NumSegments,
      ToState,
      From,
      MessageSid,
      AccountSid,
      ToCity,
      FromCountry,
      ToZip,
      FromCity,
      To,
      FromZip,
      ToCountry,
      Body,
      NumMedia,
      FromState 
  };

    let url1 = "https://example.com/1";
    let url2 = "https://example.com/2";

   const config = {
     headers: {
     'Content-Type': 'application/x-www-form-urlencoded'
     }};

  Promise.all([
       axios.post(url1, qs.stringify(requestBody), config),
       axios.post(url2, qs.stringify(requestBody), config)
       ]).then(result => {
           callback(null, twiml);
       }).catch(err => {
           console.log(err);
           callback(err);
       });
};


来源:https://stackoverflow.com/questions/61617884/creating-a-twilio-function-to-trigger-2-webhook-endpoints-autopilot-frontapp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!