Twilio Send Message to Whatsapp - twilio

I tried to send Whatsapp message through Twilio
const accountSid = 'AC4fcb197075xxxxxxxcccccccvvvvvvvvv';
const authToken = 'fedd7f35082ccccccccccc49cxxxxxxxxxxx';
const client = require('twilio')(accountSid, authToken);
client.messages.create({
body: 'This is the ship that made the Kessel Run in fourteenparsecs',
from: '+15yyy61xxx',
to: '+91mmm911kkk7'
}).then(message => console.log(message.sid));
Message SID is logged to console but in Twilio Debugger, following error codes are found
30004 - Message blocked
11200 HTTP retrieval failure

I manage the WhatsApp team at Twilio -- happy to help!
Chenna's comments are correct. With WhatsApp, your to and from values would be prefixed with "whatsapp:".
To use WhatsApp with Twilio's Programmable Messaging API, you'll have to do one of two things first:
Setup the Sandbox using your account on the Twilio Console. Details: https://www.twilio.com/docs/sms/whatsapp/api#twilio-sandbox-for-whatsapp. You then send messages using the US Sandbox number: from=whatsapp:+14155238886
Request a Twilio phone number to be onboarded to Facebook/WhatsApp and Twilio WhatsApp. One approved and onboarded, you can use that number to send and receive messages through the API. Details: https://www.twilio.com/docs/sms/whatsapp/api#enabling-whatsapp-with-a-twilio-number, https://www.twilio.com/console/messaging/whatsapp/numbers, and https://www.twilio.com/whatsapp/request-access
If you need any more help, feel free to reach out to Twilio Support!

Related

How to receive webhook response to incoming messages on Twilio SMS Service

I'm using the Twilio API to create a Service that will send SMS. The Service should have its own unique webhook endpoint to receive replies (incoming SMS).
var service = ServiceResource.Create(
friendlyName: Campaign.SMSCampaignDisplayName,
usecase: "marketing",
stickySender: true,
useInboundWebhookOnNumber: false,
inboundRequestUrl: new Uri($"<my site>/Admin/twilio/receive/{Campaign.SMSCampaignGuid}")
);
var message = MessageResource.Create(
body: Campaign.SMSCampaignBody,
from: new PhoneNumber(fromNumber),
to: new PhoneNumber(Campaign.SMSCampaignTo),
messagingServiceSid: service.Sid
);
The Service is created and the SMS is successfully sent. In the Twilio console I can see the service has the correct webhook URL:
But when I respond to the SMS, the webhook is not sent. In fact, the incoming message no longer appears in the Twilio console's Monitor > Messaging logs. Note that webhooks and incoming SMS logging was working fine when I was not using a Service and the incoming SMS webhook was defined on the sender phone number.
What am I missing, or is this some issue with Services? Do I need to add a Sender to the Service even though it's sending fine? How do I get the phoneNumberSid?
Edit
I'm dumb, I could have just tested adding the phone number to the Service manually. That indeed did work, but I need to do it programmatically. I'm not having much luck finding the API/endpoint to get the correct phoneNumberSid for this call, does it not exist?
You can use the IncomingPhoneNumber resource, as documented below, to get the Phone Number SID.
List all IncomingPhoneNumber resources for your account

Bot Framework Twilio With Multiple Numbers

I have a bot that I've built that's running on the Azure Bot Service with a Twilio Channel. I'm sending Proactive activities via my Twilio channel. Everything is working fine. I just got a request that a customer wants to have their own phone number. I would like to just have 1 bot service running but have multiple Twilio phone numbers go into this.
My thought was that I could setup an API service which would then be the incoming message call back / webhook from Twilio which then would use the Directline API to the Bot Framework. It would essentially just replace the https://sms.botframework.com/api/sms service. The problem is that I'm not sure I could still have the proactive messages working - it seems like the Directline 3.0 API works only when a conversation is started first with it.
Does anyone have any thoughts on this if this would work or have any other ideas?
Thanks
Yes, the approach which you mentioned above would be ideal. Each Web App Bot/Bot Channels Registration can only be associated with one Twilio number. I will elaborate on the steps which you mentioned above:
Create a server running the Twilio SMS API code which forwards the messages to the bot via the DirectLine API. The user sends a message to this server.
For every activity sent to the bot, make sure to include the number: Activity.ChannelData = new { fromNumber: <123-456-7890> }. The Server forwards the message to the bot.
You will need to re-attach the fromNumber to the bot's outgoing activity so that your Twilio API server knows where to send the outgoing message to. The Bot sends the reply to the server.
The Twilio API server sends Activity.Text to the user. The Server forwards message from bot to user.
For the proactive messages part, you can add a conversation property to the address param, and set the id to the user's phone number.
Example:
bot.beginDialog(
{
user: { id: '+1234567890' },
bot: { id: '+9876543210' },
conversation: { id: '+1234567890' },
channelId: 'sms',
serviceUrl: 'https://sms.botframework.com'
},
);
Hope this helps.

Message Status Callback From A Bot Framework Twilio Channel

I have a Microsoft Bot Framework bot running using a Twilio channel to send SMS messages. Everything is working, however I need to see if I can find a way to determine the SMS message delivery status. Because the Bot Framework via the Twilio channel is handling all the messaging I can't just set a web hook URL in the Twilio request normally since the Bot Framework handles all that.
Hoping I could override this - I tried to put the URL in the TwiML app's Status Callback URL but it's not firing.
Does anyone know if there is a way to somehow override the Status callback URL in Twilio or get an event back to my Bot?
Thanks
When you configure the TwiML App, you can add a status callback URL. Then in your bot you can add a new endpoint to receive the status updates. The incoming requests should have the message ids that you can use to map to your conversations.
Bot Framework SDK v4 (Node)
server.post('/MessageStatus', (req, res) => {
const messageSid = req.body.MessageSid;
const messageStatus = req.body.MessageStatus;
console.log(`SID: ${ messageSid }, Status: ${ messageStatus }`);
res.sendStatus(200);
});
For more details, take a look at the Twillio Staus Callback documentation.

Twilio error, resource not found when looking up SMS by SID

I'm trying to integrate twilio into my rails app. I want to be able to tell if there are any error messages using a response returned from the 'get' method
Twilio's example has this
#notification = #client.account.notifications.get("NO5a7a84730f529f0a76b3e30c01315d1a")
that was on this page https://www.twilio.com/docs/api/rest/notification
Here is how I'm calling it in rails console
client = Twilio::REST::Client.new(ENV['twilio_account_sid'], ENV['twilio_auth_token'])
n = client.account.notifications.get("SMb6e3a5d4649e485ea9fa818ba84ec721")
n.message_text
And I get this error
Twilio::REST::RequestError: The requested resource /2010-04-01/Accounts/[my account]/Notifications/SMb6e3a5d4649e485ea9fa818ba84ec721.json was not found
That sid is a valid sid, and I confirmed it by looking in my logs.
So why can't I look up the message with this method?
Thanks
Twilio developer evangelist here.
I think the problem is that you are not looking up a notification in your example. The SID you are using is "SMb6e3a5d4649e485ea9fa818ba84ec721" which is for an SMS message (indicated by the "SM" at the front).
As you can see in the example from the docs, notification SIDs start with "NO".
You should be able to get hold of the message you are after using client.account.messages.get('SMb6e3a5d4649e485ea9fa818ba84ec721').
You can also find all the notifications to your account by calling client.account.notifications.list.
Hope this helps, let me know if you have any other questions.

retrieve the smsstatus after sending sms Twilio api

i m using Twilio sms api to send message form my Asp.Net application
var delivery = twilioRestClient.SendSmsMessage("+123456", mobilenumber,
string.Format("Your New Password is: {0}", genpass));
the variable delivery contains object of twiliosendsms which also contain sms status currently the status is 'queued' when the message was sent i want to know how to check if the status is changed to Sending,sent,failed,received
Twilio evangelist here.
You can use the StatusCallback to have Twilio tell you when your message was sent.
Hope that helps.

Resources