Bot Framework Twilio With Multiple Numbers - twilio

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.

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

Can I use twilio studio flow with ultimate sms on same phone #?

I'm new to twilio. I have the "ultimate sms" plugin from codecanyon set up on my domain and connected to a twilio #. Is there a way to use the same # to set up a studio flow? I'd like to have people be able to send a text like "join" to my current twilio # and I can add them to a distribution list. As far as I can tell, I seem to only be able to set up 1 kind of messaging configuration for incoming messages. If possible, I'd like to use the same # for both workflows. Thank you!
Twilio developer evangelist here.
Twilio numbers process incoming messages by sending a webhook (HTTP request) to a URL. I have not used "Ultimate SMS", but I would assume the plugin requires you to set the Twilio webhook to your application so that it can manage the incoming requests.
Twilio Studio flows like to control the entire conversation, so is probably not a good idea for dealing with this.
You might be able to put some code between the incoming message and the webhook to do things based on whether the body starts with "join". Using a Twilio Function, you could write something like this:
exports.handler = async (context, event, callback) => {
if (event.Body.startsWith("join") {
// Subscribe the user to your list
const response = new Twilio.twiml.MessagingResponse()
response.message("You have subscribed to the list");
callback(null, response);
} else {
// Send the parameters on to the Ultimate SMS webhook URL and respond with the received response
const response = await getResponseFromWebhook(event)
callback(null, response.body);
}
}
This code is just a rough example, but hopefully you get the idea. You can proxy the webhook request through a Function like this (or an API endpoint written in any language if you don't want to use JavaScript or Twilio Functions) and peel off messages you want to by inspecting the body.

Sending an MS Teams message using the Microsoft Graph API or BOT API

I'm trying to implement the following solution: a web application that subscribes to all MS teams chat messages. If a message contains forbidden text, the application should somehow warn the user (Ideally by replying to the same message, or, if not possible, initiate a conversation with the user).
I'm able to receive all chat webhooks and process them, but I could not find any way to post a message back to the Teams channel using the Graph API (the operation described in https://learn.microsoft.com/en-us/graph/api/channel-post-messagereply?view=graph-rest-beta&tabs=http
is not supported for Application permissions - only delegated ones which isn't suitable for our case).
So I'm trying to send proactive messages using the Bot framework, However, the bot framework requires a teams conversation ID which I don't have (the graph API webhook provides the team, channel and user IDs, none of which are accepted by the Bot API).
Does anyone know of a way I can retrieve the teams conversation ID using the team ID and channel ID provided by the graph API?
Thanks,
Dan
ConversationId for channel messages are combination of channelId and messageId both can be found in payload you get webhook notification. You can reply to existing conversation by using following by building converstionId like this:
conversationId = $"{channelId};messageid={messageId}"
For reply to work, your Bot needs to installed in the team and should have serviceURL saved at some place to refer back. Here is sample code which show how you can reply to existing message.
var serviceURL = "YOUR Service URL- You get this in each bot payload";
MicrosoftAppCredentials.TrustServiceUrl(serviceURL, DateTime.MaxValue);
using var connector = new ConnectorClient(new Uri(serviceURL, MicrosoftAppId, MicrosoftAppPassword);
var conversationId = $"{channelId};messageid={messageId}";
var replyActivity = MessageFactory.Text($"This is simple reply to existing conversation.");
replyActivity.Conversation = new ConversationAccount(id: conversationId);
var response = await connector.Conversations.SendToConversationAsync(conversationId, replyActivity);
For 1:1 reply- please take a look at Sending Proactive Message documentation.

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 Outbound Conference Call API callbacks

I am trying to set up a call transfer workflow using Twilio's Outbound Conference Call API. I want the flow to be as follows:
Call PersonA
Initiate a timer via callback when PersonA answers
After introducing myself to PersonA, put them on hold and dial PersonB
Talk to PersonB and let them know I'll be connecting them with PersonA
Merge PersonA and PersonB call together
Leave the phone call and let PersonA and PersonB continue talking.
Looking at the docs here: https://www.twilio.com/docs/api/rest/participant#list-post it appears that this should be doable with a StatusCallback. I am having some trouble getting the callback to work and was wondering if anyone has an example of setting up a call using the Outbound Conference Call API.
Currently I am trying to start the call like so (from and to are the from and to numbers):
params = {
'From' => from,
'To' => to,
'EarlyMedia' => true,
'ConferenceStatusCallbackEvent' => 'start',
'ConferenceStatusCallback' => Rails.application.routes.url_helpers.call_twilio_conference_callback_url(#call, sid: #call.call_sid, host: DEFAULT_DOMAIN)
}
HTTParty.post("https://api.twilio.com/2010-04-01/Accounts/#{TWILIO['account_sid']}/Conferences/#{#call.browser_call_room_key}/Participants",
body: URI.encode_www_form(params),
basic_auth: {username: TWILIO['account_sid'], password: TWILIO['auth_token']})
When I check the twilio debugger I see:
15003 Call Progress: Error Response to Callback URL
If anyone has an example of using the Outbound Conference API and with a callback to your own endpoint that would be awesome! It is a pretty new feature and there does not seem to be much out there as far as examples of it being used.
Twilio developer evangelist here.
Twilio error 15003 means that Twilio received a 4xx or 5xx response from your application. So, Twilio is making a request to your application but it can't reach it for some reason.
Are you having these problems in development because you haven't exposed your local application to Twilio? Have you tried using ngrok for example?
If you have opened up your application, are you seeing errors in your Rails log when the callback occurs?

Resources