2

enter image description hereHello StackOverflow Friends:

The following code works fine, I've tested with nGrok running locally.

Two of the requirements I have left and can't figure out are:

  1. Capture the phone number of the sender (put in variable)
  2. Capture the body of the text the sender sent (put in variable)

Many thanks in advance!

const express = require('express');
const MessagingResponse = require('twilio').twiml.MessagingResponse;
const app = express();


});

app.post('/sms', (req, res) => {

// Start our TwiML response.
 const twiml = new MessagingResponse();


// Add a text message.
const msg = twiml.message('some canned response');



res.writeHead(200, {'Content-Type': 'text/xml'});
res.end(twiml.toString());
});

app.listen(3000, () => {
console.log('Example app listening on port 3000!');
});
davos
  • 151
  • 10

1 Answers1

4

Twilio developer evangelist here.

You can get the inbound message from an incoming SMS webhook request with req.body.Body and the inbound phone number with req.body.From.

To save into a variable, maybe something like const inbMsg = req.body.Body and const inbPhoneNum = req.body.From.

For more information on parsing an incoming Twilio SMS Webhook with Node.js, I'd recommend this blog post by my teammate Sam Agnew.

Let me know if this helps at all!

lizziepika
  • 3,231
  • 1
  • 14
  • 23
  • Hi @lizziepika I'll read the link from you with Sam Agnew's blog post tonight. I tried the above and get the following error. I'm also going to have to learn how to set break points in VS Code and Node.js, when I had done this before in C# I could at least step through the code, now it just blows past the breakpoint. – davos Jun 15 '21 at 20:02
  • sourceComponent "14100" httpResponse "500" url "https://8.ngrok.io/sms" ErrorCode "11200" LogLevel "ERROR" Msg "Internal Server Error" EmailNotification "false" – davos Jun 15 '21 at 20:05
  • I tried the link example you mentioned and got the following error. sourceComponent "14100" httpResponse "500" url "https://4.ngrok.io/sms" ErrorCode "11200" LogLevel "ERROR" Msg "Internal Server Error" EmailNotification "false" – davos Jun 15 '21 at 23:06
  • Hi @lizzipika I added const inbPhoneNum = req.body.From and that was added just below the existing const twiml = new MessagingResponse(); – davos Jun 15 '21 at 23:45
  • 1
    does it run if you comment out that line you added? – lizziepika Jun 15 '21 at 23:54
  • 1
    Do you have any body parsing middleware setup? Parameters in Twilio webhooks are form encoded, so you likely need `app.use(express.urlencoded());` so that Express parses the parameters into the `req.body` object. – philnash Jun 16 '21 at 05:24
  • @philnash Thanks. No. I spent an hour trying to find out how to include the above in my code and look for a more complete code example on Twilio and no luck yet. – davos Jun 16 '21 at 12:13
  • @philnash and lizziepika: I found this Twilio document (link below) and between both of your's fine help and it I've gotten the syntax and the incoming number. I can't thank you enough!!!!!!! https://www.twilio.com/blog/parsing-an-incoming-twilio-sms-webhook-with-node-js – davos Jun 16 '21 at 12:19
  • 1
    Hooray, glad you got it sorted and sorry I wasn’t more clear in my comment. – philnash Jun 16 '21 at 14:20
  • 2
    Thank you phil – lizziepika Jun 16 '21 at 15:15
  • 1
    @philnash oh you were good, I'm just spooling up to JS - it's me! Thanks again. – davos Jun 16 '21 at 17:29