jcook
July 13, 2021, 1:48am
1
I have my Twilio number set up to make an HTTP POST request to my WIX site’s http-functions.js (https://www.otecamp.com/_functions/twilioSMS/).
The goal is to save each inbound SMS into a WIX collection that I will use for some features of my site.
I have installed the npm packages for both Twilio and Express, and attempted to follow the Twilio documentation here .
Here’s my http-functions.js code:
import { ok } from ‘wix-http-functions’ ;
import wixSecretsBackend from ‘wix-secrets-backend’ ;
import wixData from ‘wix-data’ ;
import { twilio , twiml } from ‘twilio’ ;
import express from ‘express’ ;
import http from ‘http’ ;
export async function post_twilioSMS ( request ) {
const SID = await wixSecretsBackend . getSecret ( ‘twilioSID2’ );
const token = await wixSecretsBackend . getSecret ( ‘twilioTOKEN2’ );
let client = new twilio ( SID , token );
let app = new express ();
let MessagingResponse = client . twiml . MessagingResponse ;
app . post ( ‘/sms’ , ( req , res ) => {
let twiml = new MessagingResponse ();
twiml . message ( ‘The Robots are coming! Head for the hills!’ );
res . writeHead ( 200 , { ‘Content-Type’ : ‘text/xml’ });
res . end ( twiml . toString ());
});
http . createServer ( app ). listen ( 1337 , () => {
console . log ( ‘Express server listening on port 1337’ );
});
}
I am currently getting this error code in the Site Events log:
[“_twilio.twilio is not a constructor”]
What am I missing???
jcook
July 22, 2021, 5:25pm
2
BUMP… Anyone??? Bueller… Bueller…
Have you tried
const twilio = require ( ‘twilio’ );
Instead of importing? I vaguely remember having trouble with import of twilio.
jcook
July 23, 2021, 1:12pm
5
I initially tried “require” which didn’t work… However, Velo-Ninja’s reply has the require in the code. Gonna give it a try.
jcook
July 23, 2021, 3:19pm
6
This tutorial was very helpful, and it allowed me to rework the code to get the Twilio functions to stop throwing errors. However, I now have a new issue. It seems the request body is empty. From what I can tell the issue is that Twilio is making the request via “application/x-www-form-urlencoded”.
How can I get the URL parameters in my http-functions function?
jcook
July 23, 2021, 4:04pm
7
I figured it out. You need to set the Twilio webhook to HTTP GET so that the Twilio parameters are sent via query params. Then change the http-function in Wix to a get_ function. You can then access the query vars via the request.query object. I will post my code here once I have it all working for others to reference in the future.
James Cook
did you manage to solve it? I have the same problem. Help
Also experiencing this issue!
export function post_smsResponder ( request ) {
const response = {
“headers” : {
“Content-Type” : “text/xml”
}
};
try {
console . log ( request )
const twiml = new MessagingResponse ();
twiml . message ( ‘The Robots are coming! Head for the hills!’ );
response . body = twiml . toString ()
return ok ( response );
} catch ( err ) {
response . body = ‘Something went wrong’
return badRequest ( response );
}
}
Console output
"[{"functionName":"guessResponder","path":[],"url":"https://murdermysteries.net/_functions/guessResponder","baseUrl":"https://murdermysteries.net/_functions","method":"POST","body":{},"headers":{"host":"murdermysteries.net","user-agent":"TwilioProxy/1.1","transfer-encoding":"chunked","accept":"*/*","accept-encoding":"gzip,deflate",
Notice how the request.body is empty…
Did you ever post your working code somewhere? I’ve been dying over this not able to get it to work