twilio receive sms without responding back - node.js

Good Day,
How do I properly receive an SMS on my web app using twilio without responding back to the sender.
I read about this.. https://support.twilio.com/hc/en-us/articles/223134127-Receive-SMS-messages-without-Responding but I need to use the webhook to send the sms to my webapplication too, if I do receive it without setting a response, It will generate an Error - 11200 (HTTP retrieval failure) how do I prevent this? also by setting up respones.
my code is
var resp = new twilio.twiml.MessagingResponse();
resp.message('<Response></Response>');
res.writeHead(200, {
'Content-Type':'text/xml'
});
res.end(resp.toString());
sadly this one sends <Response></Response> to the sender instead.. im using nodejs by the way.. Thanks!

Those who are finding this in 2021,
If you need to just recieve message from twilio and not send any message to user then you can use this code.
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end();
For more refer this doc,
Receive SMS and MMS Messages without Responding

I have not tried yet but I think if you comment out this line:
// resp.message("<Response></Response>");
no message will be sent.
Update after reading Phil's comment:
This: resp.message('<Response></Response>');
is equivalent with sending the folowing XML to Twilio:
<Response>
<Response></Response>
</Response>
in which case the sender will receive a message like: <Response></Response>
If you comment out the line
// resp.message("<Response></Response>");
or if you do resp.message("");
is equivalent with sending the folowing XML to Twilio:
<Response />
in which case no message will be sent to the sender.

Related

Delivered Status in Status Callback MMS not working

it seems like the status delivered is not available for MMS. I get the sent status in my callback for a MMS just fine, but not delivered. Why? I use node.
My call looks like this :
var sms = {
to,
from,
body: message.message,
statusCallback: 'callbackurl',
mediaUrl: [
'imageurl1',
'imageurl2',
]
}
clientTwilio.messages.create(sms)
.then((sms) => {
});
I receive the MMS as expected.
Twilio developer evangelist here.
When Twilio alerts you of the status of the message, it does so using the data it gets back from the network you send the message with. If the delivery information is not available, Twilio can only mark the message as sent.

Twilio Functions - post data from multiple sms messages?

I have a function that posts the content from an incoming sms message to a third party api. So, a customer sends us a message and Twilio posts the message to the api. But, I want to add an extra step. So, when a user sends us a message, I want Twilio to reply asking for an email then post the message along with the email from the second message to the api. Unless we get that second message, nothing should be posted to the api. Any ideas on how to do something like that?
I was looking through the docs and saw something about the Message instance resource but I'm not sure if that will do it.
const https = require('https');
exports.handler = function(context, event, callback) {
let twiml = new Twilio.twiml.MessagingResponse();
twiml.message("Your message has been recieved.");
let postData = JSON.stringify({
....
});
let postOptions = {
...
};
let req = https.request(postOptions, function(res) {
res.setEncoding('utf8');
res.on('data', function(chunk) {
console.log(chunk);
callback(null, twiml);
});
});
req.write(postData);
req.end();
};
Twilio developer evangelist here.
In order to take two steps like that, you'd need to save the message from the first message somewhere while you wait for the second message. Since the second message will come in via a new HTTP request, you can't save this within a function anywhere, you'd want to use some sort of third party store. Then you'd write your function to check for the existence of the first message in that store, if it exists, then send both messages to the API, if it doesn't then store the first message and respond to the user with your second question.
Alternatively, you could use the Twilio API to look up all the previous messages that the user has sent to you. If there are no previous messages, then this is definitely the first message they have sent. If there is a previous message then you need to determine whether it was an initiating message for this conversation, possibly by how long ago it was or a heuristic on the content, and then decide whether to send both the messages on to your API or ask for the next response.
Let me know if that helps at all.

How to use Twilio to send SMS, and wait for response from phone to finish request in NodeJS

I know how to send an SMS via Twilio to a phone number specified in the request body, however I need a way for this route to wait for the user to respond with a text message. I need to capture that messages body and send it as a response.
router.post('/', function(req, res){
var customerNumber = req.body.mobileNumber;
var twilioNumber = process.env.TWILIO_NUMBER;
client.messages
.create({
to: '+1' + customerNumber,
from: twilioNumber,
body: 'message to user',
provideFeedback: 'true',
}, function(err, message){
if(err) res.send('message err: ' + err);
else{
// wait 10-20 seconds for user to respond
res.send("A response from the user");
}
})
});
I also know how to listen to responses with Twilio, but this doesn't work because I need the above route to respond with the message I get from the user in the route below.
router.post('/sms-response', function(req, res){
var twilio = require('twilio');
var twiml = new twilio.twiml.MessagingResponse();
twiml.message('thanks for your feedback');
res.writeHead(200, {'Content-Type':'text/xml'});
res.end(twiml.toString());
});
I've been looking for a way to do this all day with no success at all. I appreciate your help!
I dont know if that is possible yet, I went through their documentation and didnt see that feature yet. I would suggest using a web socket like socket.io. When message comes in you can just pass the response in through the socket to the user.
Twilio developer evangelist here.
While this could technically be possible I strongly recommend against it. You mention that you might be waiting for 10-20 seconds, but users are much more fickle than that, as are mobile networks. So getting a response may take much longer that that or even never happen. Then you'd be left with a long running connection to your server waiting for a response that might never come.
As Hammer suggested, it would be better to return a response to the user straight away, saying something like "We're waiting for your response", then connecting that user's browser to your server via a websocket or server sent event stream. When you receive the incoming message on your webhook endpoint, you can then send the information down the websocket or event source and react to that in the front end.
Let me know if that helps at all.

How to catch twilio events in nodejs?

I'm new in twilio and trying to understand how can i catch twilio's events in my backend (nodejs). For exemple each time i send a message, i want the console logs "Message sent" just for testing.
I read the twilio webhook documentation , however i can't really understand how to apply it in a nodejs environment.
Thanks for helping.
Twilio developer evangelist here. I think you will find this Twilio tutorial great, as it will walk you through exactly what you're trying to do, and will show you how to add events to the console.
But the gist of what you want to do it is the following:
// Create a new REST API client to make authenticated requests against the
// twilio back end
var client = new twilio.RestClient('TWILIO_ACCOUNT_SID', 'TWILIO_AUTH_TOKEN');
// Pass in parameters to the REST API using an object literal notation. The
// REST client will handle authentication and response serialzation for you.
client.sms.messages.create({
to:'YOUR_NUMBER',
from:'YOUR_TWILIO_NUMBER',
body:'Twilio message from Node.js'
}, function(error, message) {
// The HTTP request to Twilio will run asynchronously. This callback
// function will be called when a response is received from Twilio
// The "error" variable will contain error information, if any.
// If the request was successful, this value will be false
if (!error) {
// The second argument to the callback will contain the information
// sent back by Twilio for the request. In this case, it is the
// information about the text messsage you just sent:
console.log('Success! The SID for this SMS message is:');
console.log(message.sid);
console.log('Message sent on:');
console.log(message.dateCreated);
} else {
console.log('Oops! There was an error.');
}
});
Full documentation for the Node library can be found here.
Hope this helps you out.

Connecting to a Reliable Webservice with Nodejs

My application needs to receive a result from Reliable Webservice. Here is the scenario:-
First I send a CreateSequence request. Then the server replies with a CreateSequenceResponse message. Next I send the actual request to the webservice.
Then the webservice send a response with 202 accept code and sends result in a later message. All these messages contain the header Connection: keep-alive.
I made request with http.ClientRequest. I could capture all responses except the result. http.ClientRequest fires only one response event.
How can I receive the message which contains the result?
Is there any way to listen to socket for remaining data (socket.on('data') did not work). I checked this with ReliableStockQuoteService shipped with Apache Synapse. I appreciate if someone can help me.
When you get the response event, you are given a single argument, which is an http.IncomingMessage, which is a Readable stream. This means that you should bind your application logic on the data event of the response object, not on the request itself.
req.on('response', function (res) {
res.on('data', console.log);
});
Edit: Here is a good article on how to make HTTP requests using Node.

Resources