Reputation: 151
I'm trying to receive text messages sent to my Twilio number using Parse and Parse's cloud code Twilio Module. I have followed the instructions here.
I set the SMS request URL for the Twilio number I want to receive text messages from to
https://myAppID:[email protected]/1/functions/receiveSMS
and of course I used my parse app ID and my parse javascript key.
I then created the cloud function in Parse:
Parse.Cloud.define("receiveSMS", function(request, response) {
response.success("Received a new text: " + request.params.From);
});
and then I have a little webpage with some jquery calling that cloud function
$('button').on('click', function() {
alert('button');
Parse.Cloud.run('receiveSMS', {
}, {
// Success handler
success: function(message) {
alert('Success: ' + message);
},
// Error handler
error: function(message) {
alert('Error: ' + message);
}
});
});
I tried texting my Twilio number and then used my page with the code above to call the cloud code function "receiveSMS". The response is successful and I get the message "Received a new text:undefined". So it seems like I'm able to detect a new text message was sent but I'm getting undefined instead of the number the text message was from. I'd also like to get the message too. What am I doing wrong? Thanks
Upvotes: 0
Views: 568
Reputation: 73057
Ok, I know what's happened now, thanks for getting me more details in the comments.
You are calling your receiveSMS
function in two ways. Firstly, it gets called when you send an SMS to your Twilio number. When that request is received you do get all the parameters logged out and your log will show "Received a new text: +YOUR_PHONE_NUMBER"
.
The problem is when you are calling your receiveSMS
function from a web page. At that point, there are no parameters in the request and it is why you see undefined
in the log. The receiveSMS
function is for Twilio's benefit really, in that you are able to respond with TwiML to tell Twilio to do about the message.
If you want to get back the details of SMS messages sent to your Twilio number, you can retrieve the data using our REST API in a different Cloud Function like so:
var client = require('twilio')('TWILIO_SID', 'TWILIO_AUTH_TOKEN');
Parse.Cloud.define('fetchSMS', function(request, response) {
client.messages.list({ to: YOUR_TWILIO_NUMBER }, function(err, data){
numbers = data.messages.map(function(message){
return message.from;
}).join(', ');
response.success("Received messages from: " + numbers);
})
});
Then, if you change your webpage to call Parse.Cloud.run('fetchSMS', { ... })
instead then your response should be a list of numbers you have received SMS messages from. Hopefully you can build on that.
Do let me know if this helps.
Upvotes: 3