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

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

Related

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 Send Message to Whatsapp

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!

Pass variables from slack webhook to Autopilot

I would like to know if it's possible to use the user_name with Autopilot from the incoming post requests from Slack.
I've tried checking the documents but I'm not able to find any reference to handling incoming requests for Autopilot. Perhaps it isn't supported but does anyone know if it's possible?
Twilio developer evangelist here.
The documentation for handling incoming requests is here: https://www.twilio.com/docs/autopilot/actions/autopilot-request.
You should receive a UserIdentifier with the request which identifies the user in the platform the request came from. In the case of incoming phone calls or sms messages the identifier is the user's phone number. I haven't tested, but I assume that this would either be a user name from Slack, or an identifier you can use with the Slack API to find the user name.
Let me know if that helps at all.
I ended up being able to use the response data from Autopilot and found I was able to get the useridentifier from the json response as such:
user_name = request.form['UserIdentifier']
This has then allowed me to send direct messages via the incoming webhooks application in slack as using the above user name as the value for the channel key as follows.
payload={'text':"Ok, I'm working on it. I'll let you know when your request is ready.", "channel":user_name}
webhook_url= 'https://hooks.slack.com/services/<slack id>/<slack id>'
response = requests.post(
webhook_url, data=json.dumps(payload),
headers={'Content-Type': 'application/json'})
if response.status_code != 200:
raise ValueError(
'Request to slack returned an error %s, the response is:\n%s'
% (response.status_code, response.text)
)

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