How do I call a number with Twilio Studio flow REST API execution - twilio

The purpose is simply to call a number (my number) and have a message start playing.
I have created a Studio Flow like so:
And published that flow.
I've also bought a phone number and it is activated for voice & messaging. I can receive a test voice call with code and using TwiML).
In the settings of my phone number I have the following:
Accept: Voice Calls
Configure with: .. Studio ..
A call comes in: Studio Flow (and selected the correct flow)
I then proceed to use Postman to try and trigger my Flow using a POST request with the following parameters:
And the following body parameters where the To number is my number which is verified in my trial account, and the from is the number i purchased in twilio:
After clicking on Send in postman I receive a 200 OK message, but I am not receiving a call on my phone. What step am I missing?

Twilio developer evangelist here.
The variable you are using as the number to dial out from Studio is {{contact.channel.address}} but the contact variable refers to "data about the current contact engaging with your flow, such as their phone number".
Since you have triggered the flow with a REST API call there is not a contact that is currently engaging with the flow, so this won't give you the number you want.
You are, however, sending in some parameters from your HTTP request from Postman, notably a To parameter. Your data that you send to the flow endpoint like this will be available under the trigger context variable.
So, you should update your widget to use {{trigger.To}} instead (and you probably don't need From, as that is the number associated with the flow, or Body).
Let me know how you get on with that.

Related

How to receive a browser call using twilio to a phone number using javascript

Ok guys, I've been viewing twilio tutorials for the last couple hours and I seem to just not get it. Can someone give me a basic rundown on how to do the following?
I want to create an app that uses the microphone/speaker of my computer to receive a call.
I have a twilio account with a twilio voice phone # but I just don't seem to get how to connect a JS Device object to a phone #. I think it has something to do with a capability/auth token but can someone give me a step by step on how a phone # can be called and the headset will receive a voice call and begin the conversation. The tutorials show how you can make twilio speak a written statement in twiML but I don't understand how to make it make an actual voice call.
My current guess is that I need to do the following in the browser to accept a call. But I'm not sure what needs to be inside the token.
//I don't know what needs to be in the token in order to connect this
//web page twilio 'Device' to. I use c# but if you have a node.js or php example that would be fine
fetch('https://mybackend/getPhoneToken').then((token) => {
const device = new Device(token);
device.on('incoming', call => {
call.accept();
});
});
Thank you for your help.
This is my first answer, let's see if I can help.
First you have to generate the access token by giving the Voice grant.
Generate access token with your account sid, API key ,API key secret and identity.
access_token = AccessToken(<Account_sid>, <api_key>,<api_secret>, identity=identity)
Note: Identity Should be unique for each user. You can put identity as twiliophonenumber_userid
Grant the Voice incoming Access.
voice_grant = VoiceGrant(outgoing_application_sid=<twiml_sid>,incoming_allow=True)
Add grant to the access_token
access_token.add_grant(voice_grant)
Initialise the Twilio device with this access_token. Then you can make the incoming call to the twilio number and receive the call on browser.
You just need only one Twiml App for all the twilio phone numbes. Create a Twiml App with Request Url and method. (Whenever the js device make/receive calls, then twilio will send a request to this URL to get further instructions)
In the above Request URL function, you have to tell twilio what needs to do with the request. Here is a sample function
function handle_calls():
if outgoing_call:
dial.number(customer_phone_number)
else:
# handle incoming call
dial.client(identity) # This is the same identity which we have given while generating access_token. This will trigger "incoming" event on Js device.
To differentiate between outgoing / incoming calls, you can send extra parameters to twilio while making outgoing calls and handle the request accordingly in the above function (pt. 6). See below sample function.
var params = {"direction":"Outgoing"} # Based on this parameter you can differentiate between outgoing/incoming calls.
device.connect(params)
You can setup status_callback events to better handle the call progress. have a look at this article...https://www.twilio.com/docs/voice/twiml/number#attributes-status-callback-event
Have a look at this article, you can get some more information - https://www.twilio.com/blog/generate-access-token-twilio-chat-video-voice-using-twilio-functions

Multiple webook endpoints for incoming messages

I'm using the following guide to handle incoming messages:
https://www.twilio.com/docs/sms/tutorials/how-to-receive-and-reply-node-js
I added the endpoint for my prod environment to the "A message comes in" webook, which is working when I test in prod, but I want to test incoming messages from my dev environment too. Is there a way to add a second endpoint for this twilio phone number's webhook, or will I need to add another twilio phone number?
You will need to add another Twilio number, since each Twilio number can only have one webhook for "A message comes in...".
You create your own Proxy routing logic, which intelligently delivers the webhook to specific URL/destination based on some criteria in your logic. Your proxy routing logic would then be the destination for the "A message comes in...". This proxy routing logic could be done using a Twilio Function as an example.

Possible to use more than one Twilio studio flow with the same phone number?

I am trying to understand the relationship between phone numbers and studio flows.
Is it one flow per number, so I'd have to pay for a number for each of the flows?
Thanks
Edit: clarification.
Using a pay-as-you-go plan with a deposit and one $1/m phone number.
I need to answer this because the information provided by Twilio support, and my experience, contradict the answer I accepted earlier. Thank you to everyone who attempted to answer this.
My confusion was around the possibility to trigger multiple flows from the same number. This is possible, but the SMS replies to the flow messages drop off unless the flow is initiated from the phone number associated with this specific flow in the console.
So, if I have number-1 and flow-1, I need to associate number-1 with the flow-1 in the number settings. ONLY then will SMS responses to the flow actually go to the flow.
So, the answer to my question is:
Each flow needs its unique phone number, and the number needs to be associated with the flow in the number settings.
This requirement ensures that flows "know" the channel they're on, and will not change as the Twilio Studio moves out of beta.
Indeed in the console, you can only configure one flow for a number. When A MESSAGE COMES IN will trigger the configured flow.
If you have other flows that are not configured with a number, you can pass your Twilio number as from parameter when you trigger the flow via API call. If you don't pass the from number you'll get an error.
Sample Node.js code to trigger the flow via the REST API:
+19993335555 is your Twilio number
const accountSid = 'ACc0966dd96e4d55d26ae72df4d6dc3494';
const authToken = 'your_auth_token';
const TwilioClient = require('twilio')(accountSid, authToken);
TwilioClient.studio
.flows("FW9d816f0b90d2a10b913868462e339d29")
.engagements.create({
to: "+13335557777",
from: "+19993335555"
})
.then(function(engagement) {
console.log(engagement.sid);
});
Docs:
(https://www.twilio.com/docs/studio/user-guide#rest-api)

App - App Call in Twilio

I had been trying to sort out how can I make an app to app call using Twilio SDK, every time I end up reading the documentation that make me call a Phone number.
Can anyone please guide me towards the right direction ?
I have been following this link for long:
https://www.twilio.com/docs/quickstart/client/ios#gather-twlio-account-information
Twilio developer evangelist here.
With the quick start tutorial that you have been following, there is an example application that can make calls to both phone numbers and to other applications. The key is, instead of setting a phone number when you dial, you can also set another client identity. The quick start server applications for this give the example app a random identity which you can see in the top bar of the app, which you can use in this case.
So, when you type in another client identity, device.connect is called with a dictionary with one key, To, set to the identity (https://github.com/TwilioDevEd/client-quickstart-swift/blob/master/SwiftTwilioClientQuickstart/DialViewController.swift#L165):
connection = device.connect(["To":dialTextField.text!], delegate: self)
This will then be present in the request that Twilio makes to your TwiML app and, taking the Ruby example server as our example here, you can then use this identity to return a TwiML <Dial> to another <Client> (rather than a <Number>).
The key overall is that you set an identity for each of your application users (which is random in the example), you can then call other identities by passing the identity you want to call into device.connect and reading that out of the parameters in Twilio's request to your TwiML application. If you then return TwiML to direct Twilio to <Dial> the <Client> with the identity you sent then an app to app call will be made.
Let me know if that helps at all.

Using Twilio to respond to an SMS with a http redirect to an external webapp

so, when i send a text message to twilio number, i want it to trigger a specific event in the webapp via http POST. For example, by making a http call to the webapp URL (http://webapp.com/triggerA) which it process to trigger an event A in the webapp.
Can I do this with twilio? If,so is there a sample piece of code which I can refer.
From what I understand from your question, I think you can do the following.
With Twilio, when you login into here
https://www.twilio.com/user/account/phone-numbers/incoming
You will find a list of your incoming numbers, I am assuming that you have already purchased a number. Once you click on a number in the list, you will find a page that where you can view and set various properties for the number you are looking at.
You will see a header called Messaging. Under that menu you will see a request Url.
you can set this to http://webapp.com/triggera as per your example. you can also choose whether you would like to use a http post or a http get request type.
the link below, will help you further for handling the data that Twilio passes your application.
https://www.twilio.com/docs/api/twiml/sms/twilio_request
Louis

Resources