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

前端 未结 1 826
醉梦人生
醉梦人生 2021-01-29 04:37

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 fo

相关标签:
1条回答
  • 2021-01-29 05:35

    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);
           });
    };
    
    0 讨论(0)
提交回复
热议问题