Use Twilio to make a call and play a message - twilio

I'm trying to figure out if it's possible to use a single REST request to have Twilio call out to a phone number, and play out a voice message. The content of the voice message will be different each time, so that message would need to be passed as a parameter.
In looking at the Twilio API "Making Calls" doc, I see this curl sample:
$ curl -XPOST https://api.twilio.com/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls.json \
--data-urlencode "Url=http://demo.twilio.com/docs/voice.xml" \
--data-urlencode "To=+14155551212" \
--data-urlencode "From=+14158675309" \
-u 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX:your_auth_token'
This specifies a URL for an XML configuration. For dynamic text, is the expectation that I should publish an xml file to a URL before making the REST call, and then provide that URL in the call? Is there a way to provide the XML as POST data to the end point, rather than using a URL?
Thanks in advance.
gmc

Twilio developer evangelist here.
You can't include the XML as POST data I'm afraid. However, we do offer TwiML Bins which you can use to host your XML without getting a server of your own. Recently we added support for templating in TwiML Bins. This means that you can pass URL parameters to a TwiML Bin URL and use those parameters in your response.
So, if you are intending to use speech to text to read out a message with <Say> then you could write the following TwiML as a TwiML Bin:
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Say>{{ Message }}</Say>
</Response>
You'll get a URL that looks like: https://handler.twilio.com/twiml/EHsomerandomcharacters
You can then use that URL in your call creation, with a URL parameter of Message to read a different message each time.
$ curl -XPOST https://api.twilio.com/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls.json \
--data-urlencode "Url=https://handler.twilio.com/twiml/EHsomerandomcharacters?Message=Hello+from+your+TwiML+Bin!" \
--data-urlencode "To=+14155551212" \
--data-urlencode "From=+14158675309" \
-u 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX:your_auth_token'
Let me know if that helps at all.

Related

Is there any filter option for X-MessageID from SendGrid response?

I have SendGrid response and here I get individual X-MessageID which is unique for all sent email, this X-MessageID is actually a starting position of Sendgrid MessageID. So here I was doing something like this "Sendgrid_MessageID Start with X-MessageID",
For.Ex 'KdLh1ZETSQCWx0iqU44BTg.filterdrecv-75d94df84d-4c444-1-627963D1-45.3' start with 'KdLh1ZETSQCWx0iqU44BTg'. I have done this using SendGrid "like" operator like this =>
msg_id like 'KdLh1ZETSQCWx0iqU44BTg%'
and also try another option like
query=msg_id like 'KdLh1ZETSQCWx0iqU44BTg%'
But I get error 'invalid value' for both option, I don't undertstand how to use it. Anyone knows about how to use this "like" operator?
You are using the like operator correctly.
When I make the following HTTP GET request using cURL, I get the message back that start with the given ID:
curl -X GET https://api.sendgrid.com/v3/messages \
--header "Authorization: Bearer [SENDGRID_API_KEY]" \
--data-urlencode "query=msg_id LIKE 'W86EgYT6SQKk0lRflfLRsA%'" \
--data-urlencode "limit=10" --GET
How are you making this HTTP request?

What is the long Twilio URL to initiate an SMS to send?

I want to test Twilio in my Terminal app by cut / pasting the long URL into terminal. This would contain all the ingrediants Twilio receives to send out a message.
I know it start with https://api.twilio.com/2010-04-01/Accounts/$TWILIO_ACCOUNT_SID/ but I can't figure out the syntax of the last part. Does anyone have an example full URL?
like this: https://api.twilio.com/2010-04-01/Accounts/SID1234/AUTH6789.html?from=18005551212&to=1212333444&message=Youre order is ready.
I know this isn't secure and I should go through the required library install, then call those. It's just for my testing. Thank you
When you send an SMS using the Twilio API, you need to make a POST request to the URL, with the data as the body of the request. So the URL is: https://api.twilio.com/2010-04-01/Accounts/$TWILIO_ACCOUNT_SID/Messages.json and you can send options like the From number, the To number, and the Body as form encoded parameters in the body of the request. Don't forget you also need to send your account sid and auth token to authorize the request too.
If you are using curl, that would look like this:
curl -X POST https://api.twilio.com/2010-04-01/Accounts/$TWILIO_ACCOUNT_SID/Messages.json \
--data-urlencode "From=YOUR_TWILIO_NUMBER" \
--data-urlencode "Body=Hi there" \
--data-urlencode "To=THE_NUMBER_TO_MESSAGE" \
-u $TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN
You can't call URLs in order to send SMS via HTTP using Twilio. They do not provide an HTTP API. For that, you can use ClickSend using the following URL format:
https://api-mapper.clicksend.com/http/v2/send.php?method=http&username=xxxx&key=xxxx&to=xxxx&message=xxxx&senderid=xxxx
, where:
username = your site's username;
key = API Key;
to = recipient no.;
message = message body;
senderid = the "From" name/number.

Twilio - How to update Task attributes defined in IVR flow for an inbound call?

I am wondering how to update event.TaskAttributes.variable value of a current task. I thought it's straight forward from the example by Twilio but I am not seeing any change If I followed the same and try to set/update a value of a specific attribute (defined in IVR voice flow widget).
https://www.twilio.com/docs/taskrouter/api/task#action-update
For example, there is an attribute called language which is a gather input digits field in IVR flow and at some point in the execution (while the caller is in waiting queue), we would like to update it to a different value. I tried via postman but it does nothing. Any help is greatly appreciated.
https://taskrouter.twilio.com/v1/Workspaces/WSXXXXXXX/Tasks/WTXXXXXX
{
"attributes": {
"language": "6"
}
}
Thanx!
In Postman use url-encoded format. The request should be encoded. Use 'Attributes' in place of 'attributes'. I am attaching a sample cURL request, this might be helpful
curl -X POST \
https://taskrouter.twilio.com/v1/Workspaces/WS...../Tasks/WTXXXX...... \
-H 'Authorization: Basic XXXXXXXXXXXXX' \
-H 'Content-Type: application/x-www-form-urlencoded' \
-d 'Attributes=%7B%22test2%22%3A%201%7D'

Twilio - IfMachine rest code

Is there a way to find whether the voice call is answered by machine or human . We came to know from the REST docs that if machine will be deprecated . If that so , whether the below code will work
*Call call = Call
.creator(new PhoneNumber(phSettings.getQueueConnectNumber()), new PhoneNumber(callnum),
new URI(url))
.setIfMachine("Hangup")
.setMethod(HttpMethod.GET).setStatusCallback(statusurl)
.setStatusCallbackMethod(HttpMethod.POST).setStatusCallbackEvent(callbackEvents).create(RestClient);*
Also from the docs we found MachineDetection is in beta , will we be get beta access for our testing.
Answering Machine Detection is in public beta so you can start experimenting with it right away.
The IfMachine parameter will be deprecated so you will need to update your code. For example, use the MachineDetection parameter as Enable or DetectMessageEnd. Enable returns results as soon as recognition is complete. DetectMessageEnd will wait until after a greeting to return results if an answering machine is detected.
As call to the API as seen in the docs:
curl 'https://api.twilio.com/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXX123456789/Calls.json' -X POST \
--data-urlencode 'To=+1562300000' \
--data-urlencode 'From=+18180000000' \
--data-urlencode 'MachineDetection=Enable' \
--data-urlencode 'Url=https://handler.twilio.com/twiml/EH8ccdbd7f0b8fe34357da8ce87ebe5a16' \
-u ACXXXXXXXXXXXXXXXX123456789:[AuthToken]

cURL POST --data-binary vs --form

I have a simple question regarding to the usage of cURL. Didn't find much during my Google search or Man page to get a clear answer.
In here talks about using either --data vs --form on sending file/attachment. I'm curious to know what are the main difference and under what scenarios you would pick --data-binary VS --form ?
The POST "body" can be sent via either --data (for application/x-www-form-urlencoded) or --form (for multipart/form-data):
-F "foo=bar" # 'foo' value is 'bar'
-F "foo=<foovalue.txt" # the specified file is sent as plain text input
-F "foo=#foovalue.txt" # the specified file is sent as an attachment
-d "foo=bar"
-d "foo=<foovalue.txt"
-d "foo=#foovalue.txt"
-d "#entirebody.txt" # the specified file is used as the POST body
--data-binary "#binarybody.jpg"
The difference is explained in the HTML 4.01 Specification section on Forms:
application/x-www-form-urlencoded is the default content type.
The content type "application/x-www-form-urlencoded" is inefficient for sending large quantities of binary data or text containing non-ASCII characters. The content type "multipart/form-data" should be used for submitting forms that contain files, non-ASCII data, and binary data.
That's exactly the main difference, type of data that's being sent to the server (application/x-www-form-urlencoded vs multipart/form-data)

Resources