How to send custom notifications using twilio whatsapp without registering receiver's number? - node.js

I have followed the following steps from https://www.twilio.com/docs/sms/whatsapp/quickstart/node which works great with my the number I have registered with custom messages.
Now I changed the number with another WhatsApp number I had but it's not sending messages to that. How do I send without registering?
Please note that I have a trial account.
// This works
const whatsappMessage = await textTransport.messages
.create({
from: 'whatsapp:+14155238886',
body: 'Hello there!',
to: 'whatsapp:+91888888888' // registered number to twilin
});
// This doesnt work
const whatsappMessage = await textTransport.messages
.create({
from: 'whatsapp:+14155238886',
body: 'Hello there!',
to: 'whatsapp:+91999999999' // not registered
});

Twilio developer evangelist here.
The Twilio API for WhatsApp allows you to test using our sandbox. However, to send messages to your users without the sandbox restrictions you need to request your own WhatsApp enabled number.

To receive message to new number must send join [KEY-WORD] from the new number to the WhatsApp number, as you do with the first number

Related

how to send sms from within Twilio programmable voice function

I have a Twilio function which executes whenever someone calls a certain number. I'm trying to have the function send an sms. It's not working, but I'm not getting any error. Any help debugging would be great.
exports.handler = function(context, event, callback) {
let response = new Twilio.twiml.MessagingResponse();
response.message({
to: '+11234567890',
}, 'new sms from testing');
};
Additionally, If you could let me know how to access the incoming phone number within this function, I would greatly appreciate it. Thanks in advance for any suggestions or insights.
You will not be able to send a messaging response to a voice event, you'll need to create a messaging client and send separately. See docs here: https://www.twilio.com/docs/sms/tutorials/how-to-send-sms-messages-node-js .
For incoming phone numbers, that is included in the event object. It should be event.From, but you can log the event object to get all the parameters. They should follow the Call object schema: https://www.twilio.com/docs/voice/api/call

why i m not receiving message with my twilio number which i bought?

When receive message from my twilio account.
i got message with different number like 400012 or 641011 like this
but not my twilio number which i use to send message is not display in my inbox when i receive message
can i do something so that whenever someone receive message from my twilio number the number in inbox showing is also twilio number?
var client = new twilio.RestClient(twilio_account_sid, twilio_auth_token);
client.messages.create({
body: msg,
to: to, // Text this number
from: twilio_number // From a valid Twilio number
}, function(err, message) {
if(err) {
console.error(err.message);
}else{
console.log("here send sms ... ... ...");
}
});
my code is simple like above
Twilio developer evangelist here.
There are some limitations when sending messages to certain countries from a Twilio number. India is particularly restrictive and will not let you retain your original number. Users can still send messages to your Twilio number, they will just receive them from a shortened number like the ones you shared.
You can read up on the restrictions for sending messages to India in this support article and by checking out the SMS guidelines for India.
The restrictions for the other countries you need to send to will be different. For example, in Malaysia your long code phone number is preserved. Check out the restrictions for your countries here: https://www.twilio.com/sms/guidelines
Let me know if that helps at all.

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.

Trouble with Twilio / TwiML statusCallback using Node.js

When a user texts my number, I would like to respond with a message using TwiML. I'd also like to know whether my response message sends or fails. I'm creating my TwiML response using the Twilio library for Node.js.
Here is my code:
const response = new twilio.TwimlResponse();
response.message(Response, {
statusCallback: '/sms/status/'
});
Here is the code for the route. Please note that the router is mounted upon the "/sms/" router:
router.post('/status/', acceptRequestsOnlyFromTwilio, (req, res) => {
const Event = require('./../models/schemas/event');
const event = new Event({
description:
`MessageSid = ${req.body.MessageSid}
MessageStatus = ${req.body.MessageStatus}`
});
event.save();
console.log(
`MessageSid = ${req.body.MessageSid}
MessageStatus = ${req.body.MessageStatus}`
);
res.sendStatus(200);
});
When I check my logs using the Heroku CLI, I don't log nor do I see a record when I check MongoDB.
Anyone have any ideas or tips on how I can go about debugging this or if I'm implementing this incorrectly?
Thanks for you time!
Twilio developer evangelist here.
Firstly, have you set your Messaging request URL for the Twilio phone number you are using? Is it pointing at the correct route for your Message TwiML response?
Also, your first code block should also be within a router.post endpoint for your server. Do you correctly return the TwiML response as the server response?
As a tip, it might be better to develop this application locally and test your webhook endpoints using a tool like ngrok. I love using ngrok to test webhooks.

how to connect two anonymous callers together using twilio

I am currently doing using node js twilio module for a functionality in a project that i am working on for a client. Basically the server is going to initiate a call using twilio api to call a certain person A the connect him to another person B. I am new to twilio so i am still a noob, but this is the code i have written so far. Please i need u guys input on how to achieve this. cheers
var client = require('twilio')(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN);
exports.makeCall = function(callDetails, cb) {
client.makeCall({
to:'+16515556677', // Any number Twilio can call
from: TWILIO_CALLER_ID,
url: 'https://demo.twilio.com/welcome/voice' // A URL that produces an XML document (TwiML) which contains instructions for the call
}, function(err, responseData) {
//executed when the call has been initiated.
console.log(responseData.from); // outputs "+14506667788"
var resp = new client.TwimlResponse();
resp.say('Welcome to Acme Customer Service!')
.gather({
action:'http://www.example.com/callFinished.php',
finishOnKey:'*'
}, function() {
this.say('Press 1 for customer service')
.say('Press 2 for British customer service', { language:'en-gb' });
});
});
};
Twilio developer evangelist here.
You're part way there with this, but you don't want to be creating a TwiML response in the callback to your call to client.makeCall. The responseData there is a representation of the call in Twilio's system.
What you need to do is provide a URL to the makeCall function that hosts some TwiML that connects the call to another phone number. Currently you have the demo URL in place, so you'll need to point that to a URL that you own.
There's a very good in depth tutorial on how to accomplish all of this on the Twilio site which you might find helps out. You can find the tutorial here, give it a go and let me know if that helps.

Resources