How to specify dial code in code using Twilio - twilio

I am currently developing a phone system using Twilio and want to set the dial code for GB automatically in my code without the user having to specify +44 and removing the 0 from the number when dialing out using Twilio

In its simplest form it will be something like the below. May need to expand on this if you are likely to make international calls.
//presuming your twilio callback is providing a To number in the request
if(To.startsWith("0"))
{
To = To.Replace("0", "+44")
}

Related

Gathering speech during outbound twilio voice call

I am trying to create a voice call from twilio to a number using programmable voice.
I also intend to gather recipient's speech transcribed. However, the documentation contains examples and tutorials only for inbound voice calls.
This is what I have tried-
Initiating voice call:
client.calls
.create({
twiml: `<Response><Gather input="speech" action="https://ngrok-url-for-my-local-server/voice" method="POST" speechTimeout="5"><Say>Hey there, How are you?</Say><Pause length="4" /></Gather></Response>`,
to: toPhoneNumber,
from: myPhoneNumber,
})
.then((call) => {
console.log(call.sid)
console.log(call)
})
.catch((e) => {
console.log(e)
})
Code in handler for gathering speech-
const VoiceResponse = twiml.VoiceResponse
const res = new VoiceResponse()
res.gather()
console.log(res.toString())
However I am not getting anything useful in the console.
Can anybody point me to a useful tutorial or example or tell me what I should do ?
You need to first make the call and then modify the call to gather.
When you make the call using the new VoiceResponse() call you will be returned a SID for the call. Next, use that SID to modify a call that is in progress by redirecting to the Twiml to gather the digits.
You will have three legs:
Make the outbound call (you provided this code in your questions).
Modify the active outbound call by using the call's SID to redirect to a Gather twiml.
After the gather has finished tell the call what do to using the action parameter of the gather. Here is an example.
If you are trying to go for a conference bridge type feature where you press * to move into an admin type menu, you will need a more complex solution involving a conference call.

Twilio how to manage local phone numbers through api

I am trying to move my account off of using local numbers and onto toll free numbers.
I would like to get a list of all my local numbers. The incoming_phone_numbers api does not seem to return the Type attribute which tells me local vs toll free.
I believe I need to use the Active Number api, which is in preview, to get that info https://www.twilio.com/docs/phone-numbers/global-catalog/api/active-numbers
I am receiving a 404 response when I use the below script.
I've found post of people talking about this api, but all the comments were about the issue I am having with no resolution https://stackoverflow.com/a/63297180/3599659
from twilio.rest import Client
import keyring
import requests
from requests.auth import HTTPBasicAuth
account_sid = keyring.get_password("twilio", "account_sid")
auth_token = keyring.get_password("twilio", "auth_token")
client = Client(account_sid, auth_token)
numbers = client.incoming_phone_numbers.list(limit=1)
print(f"getting info for {numbers[0].sid}")
response = requests.get(f"https://preview.twilio.com/Numbers/ActiveNumbers/{numbers[0].sid}", auth=HTTPBasicAuth(account_sid, auth_token))
if not response:
print(f"Request for active numbers failed. status code:{response.status_code}. {response.content}")
quit()
print(response.content)
This API is in Private Developer Preview. I am not sure if it still open to accepting new customers. I am looking to discover this.
It does cost $0.005 per call but if you only have US numbers you can make a call to the lookups api requesting carrier information for the number.
response = requests.get(f"https://lookups.twilio.com/v1/PhoneNumbers/{numbers[0].phoneNumber}?Type=carrier", auth=HTTPBasicAuth(account_sid, auth_token))
This should list the carrier name as either Twilio - SMS/MMS-SVR or Twilio - Toll-Free - SMS-Sybase365/MMS-SVR so you can identify the toll free numbers that way. This is assuming the numbers were issued by twilio and haven't been ported in etc.

Twilio JS send digits in VoIP call

I want to implement one functionality in VoIP call using twilio. Like if someone is calling to the customer care numbers and in that case they have to dial some numbers to navigate like dial some number choose language and dial some number to talk to representative.
So, to implement this in my application with VoIP call, I have tried this so far.
Here is my generated TwiML response at the time of VoIP start
<Response>
<Dial timeLimit="7200" callerId="+19782880482" record="record-from-answer-dual">
<Number statusCallbackEvent="initiated ringing answered completed" statusCallback="http://0affe80b.ngrok.io/call/twilio/events" statusCallbackMethod="POST"></Number>
</Dial>
<Record timeout="10" maxLength="7200"/>
</Response>
And in frontend part, on each digit press I am sending a digit to current active connection like this,
function onNumberPress(number){
var connection = Twilio.Device.activeConnection();
connection.sendDigits(number);
}
But, now at the time of sendDigits function, I am getting this error
TypeError: a.match is not a function
at a.sendDigits (twilio-1.3.21.min.js:19)
Note: I am able to get active connection here.
After surfing I found that I need to supply Gather keyword in TwiML. But do I need to pass those in this case? I think its not needed in My described case.
Am I on right track? Is this possible to achieve? If then what am I missing here?
Twilio developer evangelist here.
The connection.sendDigits function takes a string as an argument (as it can be any digit as well as # or *). I think you may be passing a number to it instead. Try ensuring that the digits you send are strings.

Twilio caller name on receive programmable voice call in ios application

I’m using Twilio’s Programmable Voice in one of the projects. My primary requirement is to place VoIP class between mobile devices (no PSTN calls). I am able to place calls from one device to another, but unable to set appropriate Caller Name on Incoming Call screen.
Please guide me about how to display Caller’s name on receiving device. TVOCallInvite’s “from” value shows a mobile number “+18xxxxxxxx”, but I need to display the name of the caller.
.
We have created TwiML PHP file which contains the dialled client name and callerID (my twill number). We have assigned url of this file in TwiML app’s request URL (https://www.twilio.com/console/voice/twiml/apps/myappid).
We can assign name of the caller in CallKit’s “localizedCallerName”, but we are receiving phone number instead of caller’s identity.
Details:
Tutorial Followed : https://github.com/twilio/voice-quickstart-swift
TwilioVoice -> 2.0.0
iOS Version : 10.1
Device : iPhone 7 & iPhone 5S
Please find the attached screenshot.
Please note that I have searched google but I could not found the answer.
Thanks.
Below is my voice.php file
<?php
require __DIR__ . '/TwilioSdk/Twilio/autoload.php';
include('config.php');
use Twilio\Twiml;
$response = new Twiml;
if (isset($_REQUEST['To']) && strlen($_REQUEST['To']) > 0)
{
$number = htmlspecialchars($_REQUEST['To']);
$dial = $response->dial(array('callerId' => $callerid)); // callerid is +18XXXXXXXXX
if (preg_match("/^[\d\+\-\(\) ]+$/", $number))
{
$dial->number($number);
}
else
{
$dial->client($number);
}
}
else
{
$response->say("Thanks for calling!");
}
header('Content-Type: text/xml');
echo $response;
?>
Twilio console for call logs
Twilio developer evangelist here.
In order to get a name to appear on the iOS call screen in CallKit you need to pass a client identifier as the callerId rather than a phone number.
Client identifiers should be prefixed with client:. So in the code above, the important part is generating the TwiML, which should look like this:
$response->dial(array('callerId' => 'client:' . $clientName));
Note, you must use a number as a callerID if you a dialling a phone number. If you are dialling another client, then you can use a phone number or client identifier. If you want the name to appear in the application, then I recommend a client identifier as above.

Retrieve Key Input before recording a users call when making calls within Twilio

We have currently developed a phone system for our call centre using Twilio (mainly c#, angular2 and typescript). Our company is currently in the UK but we have now started expanding out to the USA and because of laws in the US it looks like we'd need the ability to choose when we turn on the call recording. In the US people have to consent to recording the call before you can start recording them. I am trying to - when we make an outbound call via twilio to first play a message and get the user to input a key on their dialer to consent to recording the call before continuing with the call. I have attempted to use the gather and say verbs after we have dialled out but this doesn't seem to work. Code below:
`
public override void ApplyAction(TwilioResponse response)
{
var attributes = this.GetAttributes<DialAttributes>("attribute-");
attributes.callerId = this.PhoneNumber;
response.Dial(new Number(this.ReceiverPhoneNumber), attributes);
response.Gather(
new
{
timeout = 10,
finishOnKey = '*'
});
response.Say("We record all calls - please press the star key to consent to call recording");
}`
Twilio developer evangelist here.
The problem is that you are performing the <Dial> before the <Gather>
and <Say>. If you want the user to approve the call first you need to nest the <Say> in the <Gather> and provide an action URL for the <Gather>. When the user presses a button as part of the <Gather> Twilio will make an HTTP request to the action URL with the results in the Digits parameter. Then you should return the <Dial> as the response to that request, if the user signifies agreement. This way you ask them first and then connect the call.
Let me know if that helps.

Resources