问题
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