Retrieving Message Service Name from Twilio's Phone Numbers API - twilio

I'm using the IncomingPhoneNumber resource (https://www.twilio.com/docs/phone-numbers/api/incomingphonenumber-resource) to retrieve infomration about my phone numbers in Twilio.
Both .ReadAsync and .FetchAsync return numbers that I've bought via the Twilio console, and some of those numbers are in a Sender Pool for messaging services.
However, the payload returned by either of those two methods does not contain whether or not a phone number is in a message service pool.
On the console, you can see if a phone number belongs to a message service.
Is it possible, using the IncomingPhoneNumber Resource REST API to find out if a phone number is part of a messaging service?

No, the IncomingPhoneNumber resource won't be able to tell you that. But the PhoneNumber Resource of the Messaging Service API will be able to tell you (or help modify the assignments).
// Download the helper library from https://www.twilio.com/docs/node/install
// Find your Account SID and Auth Token at twilio.com/console
// and set the environment variables. See http://twil.io/secure
const accountSid = process.env.TWILIO_ACCOUNT_SID;
const authToken = process.env.TWILIO_AUTH_TOKEN;
const client = require('twilio')(accountSid, authToken);
client.messaging.v1.services('MGXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
.phoneNumbers
.list({limit: 20})
.then(phoneNumbers => phoneNumbers.forEach(p => console.log(p.sid)));

Related

Conf call: Wait for first person to respond, then connect second person

Basic case: System will call person A. If person A picks up the phone it will call person B and the 2 will be connected.
I read several answers here such as https://stackoverflow.com/a/20976996/1907888 but it's still unclear.
Would the following work? It would call PERSON_A if person responds it will connect to conference then call PERSON_B and connect to same conference? Do I need to start the conference first?
$response = new VoiceResponse();
$dial = $response->dial('PERSON_A');
if($dial->conference('Room 1234')) {
$dial = $response->dial('PERSON_B');
$dial->conference('Room 1234');
}
Twilio developer evangelist here.
When you control calls with Twilio there are two mechanisms by which it works. There is the Twilio REST API which your application can use to make things happen, like start or change a call. Then there are webhooks, which are HTTP requests that Twilio makes to your application when things change in a call, like someone dialling your Twilio number, entering data on the phone, or an person answering an outbound call. You respond to webhooks with TwiML, a subset of XML, with instructions for what to do with the call next.
In this case, you want to place a call to person A to start with. For this, you will need the REST API to make that call. When person A answers the phone, Twilio will then make a webhook request to your application to find out what to do next. It is at this point that you can both call person B, again using the REST API, and place person A into a conference call, by responding with TwiML.
So, your initial outbound REST API call should look a bit like this:
use Twilio\Rest\Client;
// Find your Account Sid and Auth Token at twilio.com/console
// and set the environment variables. See http://twil.io/secure
$sid = getenv("TWILIO_ACCOUNT_SID");
$token = getenv("TWILIO_AUTH_TOKEN");
$twilio = new Client($sid, $token);
$call = $twilio->calls
->create($personANumber, // to
$yourTwilioNumber, // from
["url" => "http://example.com/conference.php"]
);
The URL you send when you place the call will be where Twilio sends the webhook request. So, in response to example.com/conference.php in this case, you will need to dial another person and respond with TwiML to direct person A into the conference call.
This time, instead of sending a URL, you can actually send TwiML in the REST API response. Something like this:
use Twilio\Rest\Client;
use Twilio\TwiML\VoiceResponse;
// Find your Account Sid and Auth Token at twilio.com/console
// and set the environment variables. See http://twil.io/secure
$sid = getenv("TWILIO_ACCOUNT_SID");
$token = getenv("TWILIO_AUTH_TOKEN");
$twilio = new Client($sid, $token);
$twiml = new VoiceResponse();
$dial = $twiml->dial();
$dial->conference("Conference Name");
$call = $twilio->calls
->create($personBNumber, // to
$yourTwilioNumber, // from
["twiml" => $twiml->toString()]
);
echo $twiml.toString();
In this case, I have used the same TwiML for both legs of the call, because they are both entering the same conference. You could respond with different TwiML based on what's happening.
Let me know if that helps at all.

Get phone number with twilio conference participant sid

How can I get a conference participant's phone number using the Twilio API? I am using the official Twilio PHP Library.
Hello Twilio Developer Evangelist here. 👋
What you can do is to fetch all active participants with a snippet like this (you can find more details in the docs for the conference participants.
<?php
// Update the path below to your autoload.php,
// see https://getcomposer.org/doc/01-basic-usage.md
require_once '/path/to/vendor/autoload.php';
use Twilio\Rest\Client;
// Find your Account Sid and Auth Token at twilio.com/console
// DANGER! This is insecure. See http://twil.io/secure
$sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$token = "your_auth_token";
$twilio = new Client($sid, $token);
$participants = $twilio->conferences("CFXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
->participants
->read(array(), 20);
foreach ($participants as $record) {
print($record->callSid);
// "CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
}
Now, when you did this you'll see that the participant record does not include the phone number of the participant. But what it includes is the call_sid. The call sid helps you to identify the caller and retrieve call details.
There you have two options (I described something similar just recently here).
1. Persist the call information when you route callers into the conference
What you could do when you route people into the conference is to persist their numbers and call sids in your application. This way you could have a registry with all the people that entered your conference and access their phone numbers quickly when you need them.
2. Make another API call to get the call details and caller number
When you have the call SID what you can always do is to fetch the call details one by one.
<?php
$call = $twilio->calls("CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
->fetch();
print($call->from);
The first approach saves you API calls with the cost of maintaining your own persistence layer. The second approach makes more API calls but it is easier to set up.
As always, it depends on the situation.
I hope that helps. :)

Twillio Notify Bulk Sms With Custom Number

I want to send bulk sms using twillio notify in php having a custom text in place of the number ("From") but dont seem to know how to go about it. Am using a messaging service. I would like to show the custom text instead of my sending number when the message is sent. Below is my code for sending the message
<?php
require_once '/path/to/vendor/autoload.php';
use Twilio\Rest\Client;
$accountSid = "your_account_sid";
$authToken = "your_auth_token";
$serviceSid = "ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$client = new Client($accountSid, $authToken);
$recipients = array($num1, $num2, ...); // Your array of phone numbers
$binding = array();
foreach ($recipients as $recipient) {
$binding[] = '{"binding_type":"sms", "address":"+1'.$recipient.'"}'; // +1 is used for US country code. You should use your own country code.
}
$notification = $client
->notify->services($service_sid)
->notifications->create([
"toBinding" => $binding,
"body" => $text
]);
?>
You might be able to send them from Twilio verified phone numbers, but in-general that's not good practice, as your "From" number will get flagged as spam by phone companies
Further than that, you won't be able to change the 'From' attribute, as Twilio doesn't want you fraudulently impersonating other people's phone numbers.
If you can't afford a short code, use a messaging service and buy a bunch of numbers to accommodate your volume.
Twilio developer evangelist here.
When you are using a messaging service, you can setup an alphanumeric sender ID as part of the copilot features for the service. Open up your messaging service settings and add an alphanumeric sender as shown in the screen shot below:

The From phone number is not a valid, SMS-capable inbound phone number

I need to send SMS messages from a website.
I made an account to Twilio and I wanted to try if I really get a message from them.
I use this code that I founded on the net:
const string accountSid = "....";
const string authToken = "....";
TwilioClient.Init(accountSid, authToken);
var to = new PhoneNumber("+40740539623");
var message = MessageResource.Create(
to,
from: new PhoneNumber("+1402-704-3438"),
body: "This is the ship that made the Kessel Run in fourteen parsecs?");
AccountSid and AuthToken are correct. But I keep getting the error message that the From number is not a valid, SMS-capable inbound phone number or short code for your account.'
I tried to use the number that I have on my Twilio account, I only have a test account, so I thought that I can use this number to send test SMS.
Can you please advise what I'm missing here? How can I send test SMS from Twilio? From Trial Account I can't send messages?
Exception:
pass the phone numbers in as a string in the format "+18001234567", where +1 is the US country code, and whatever after is the 10 digit US phone number.
Also, I have no idea what the +4 area code is you're using in your to number, but you may have a number that doesn't support international SMS. I've only worked with US numbers, both to and from, so can't offer any guidance on how to work with international SMS.

Twilio configuration Profile SID

I am using the Twilio Video API. In my node.js I used this code,
var grant = new VideoGrant();
It requires configurationProfileSid but I can't find docs on how to get it?
I think it is a capability token.
But how can I get it using twilio node js?
Or is there any other way to get it?
Twilio developer evangelist here.
Configuration Profiles used to be required, but have been deprecated. So you no longer need the configurationProfileSid. You can give access to particular rooms though.
Here's an example Node.js application that generates access tokens for a Video application. The important part is the route that generates the token:
app.get('/', function(request, response) {
// Create an access token which we will sign and return to the client,
// containing the grant we just created
var token = new AccessToken(
process.env.TWILIO_ACCOUNT_SID,
process.env.TWILIO_API_KEY,
process.env.TWILIO_API_SECRET
);
// Assign identity to the token
token.identity = request.query.identity || 'identity';
// Grant the access token Twilio Video capabilities
var grant = new VideoGrant();
grant.room = request.query.room;
token.addGrant(grant);
// Serialize the token to a JWT string
response.send(token.toJwt());
});
This documentation on access tokens should help too.

Resources