Proper way to use Twilio Add-ons for "Lookups" in Python - python

What is the proper way to us a Twilio Add-on that provides intelligence on a phone number for "Lookups" in Python?
Currently, I have it working with "Incoming Voice Call" and "Incoming SMS Message" (Note: you can see "lookups", "Incoming Voice Call: and "Incoming SMS Message" in the attached picture).
With "Incoming Voice Call" and "Incoming SMS Message", the information is automatically provided with the rest of the information pertaining to the call or SMS when Twilio makes it's initial request to my application. The problem with this is that in practice, I am paying 10 times over for many spam numbers that are constantly dialing in.
Instead, I would like to look up the information manually, only when I do not already have the information cached.
It looks like I can make a raw HTTPS request from Python to do this. Example:
https://lookups.twilio.com/v1/PhoneNumbers/+16505399600/?AddOns=nomorobo_spamscore&AddOns.nomorobo_spamscore.secondary_address=+15108675309
However, it seems like a more "proper" way would be to use the TwilioRestClient that comes with the Twilio python helper library. However, its not clear get this Add-on information through the Twilio python helper library. If I'm being honest, I have a lot of trouble understanding those docs.

Twilio developer evangelist here.
You can use the Twilio Python library to achieve this with Lookups too. You can do a lookup, with the example URL you gave, like this:
import os
from twilio.rest import Client
account_sid = os.environ['TWILIO_ACCOUNT_SID']
auth_token = os.environ['TWILIO_AUTH_TOKEN']
client = Client(account_sid, auth_token)
number = client.lookups.phone_numbers("+16505399600").fetch(
add_ons="nomorobo_spamscore",
add_ons_data={
"nomorobo_spamscore": {
"secondary_address": "+15108675309"
}
}
)
print(number.add_ons)
number.add_ons will be a dict of the returned data from the Add-on. You pass the Add-ons you want to use with the add_ons argument (as a string or array of strings) and any further data in the add_ons_data argument.
Let me know if that helps at all.

Related

connecting dialogflow voicebot to twilio number

I am a noob to dialogflow as well as twilio. I am trying to connect my dialogflow bot to a Twilio number.
I got a twilio number and i am using dialogflow small talk set of intents.
What are the steps to connect a dialogflow (voice)bot to a twilio number with voice (not sms, or chat messaging) ?
On twilio side, i found this code
from flask import Flask
from twilio.twiml.voice_response import Gather, Redirect, VoiceResponse, Say
app = Flask(name)
#app.route("/answer", methods=['GET', 'POST'])
def answer_call():
"""Respond to incoming phone calls with a brief message."""
# Start our TwiML response
response = VoiceResponse()
# Read a message aloud to the caller
gather = Gather(input='speech',action='some_url')
gather.say('Welcome to Paradise, please tell us why you\'re calling')
response.append(gather)
return str(response)
if __name__ == "__main__":
app.run(debug=True)
1) I understood that i should put the url of my dialogflow bot into the action argument. Am I right?
2) If yes, where do i find this url? Is it related to this ? => https://cloud.google.com/dialogflow-enterprise/docs/reference/rest/v2/projects.agent.sessions/detectIntent
3) Then, what would be the session name?
I am trying to use the box on the right "Try this API": but whatever string i write, the output received is :
"name does not match pattern: /^projects/[^/]+/agent/sessions/[^/]+/contexts/[^/]+$/"
As mentionned i am a newby, so any insights on the above would be greatly appreciated!
Thank you so much in advance!
Twilio developer evangelist here.
I've not worked with Dialogflow and Twilio Voice directly (I prefer to connect voice with Twilio Autopilot as it works out of the box). However I know that there is no direct connect between Twilio Voice and Dialogflow.
You are on the right track though. Using <Gather> will capture the user's speech and translate it to text (actually using Google's Cloud Speech API). That text will be sent to your action URL as the SpeechResult. You can't connect that directly to your Dialogflow API endpoint because Dialogflow will expect the parameter to be different and Twilio will expect the result to be TwiML.
Instead you will want to setup the action endpoint on your own server, retrieve the SpeechResult and then send that on to Dialogflow for the result. You might find it easier to interact with the Dialogflow API by installing the Dialogflow Python client and using it to send the request (check out the documentation here). Once you get the result back from Dialogflow you can then use it to construct TwiML to create a new <Gather> for further input or just a <Say> to return the response.
Let me know if this points you in the right direction.

How to submit a POST request to the Calls resource? (making outgoing calls in Twilio)

I'm having trouble with making outgoing calls using Twilio and I believe I skipped this step where I should submit a POST request to the Calls resource. I don't really understand what to do with this and I need someone to explain it for me since I'm just a beginner. And by the way, my Twilio account is just free trial. Does this have something to do as to why I can't make local calls?
Thanks.
Twilio developer evangelist here.
Best place for you to start would be the Python making calls quick start. Follow that tutorial through and you should understand how to make a call with Python.
Though, as you say, the key is the POST request to the calls resource. It's easiest to perform this using our Python helper library and looks a bit like this:
from twilio.rest import TwilioRestClient
# Get these credentials from http://twilio.com/user/account
account_sid = "ACXXXXXXXXXXXXXXXXX"
auth_token = "YYYYYYYYYYYYYYYYYY"
client = TwilioRestClient(account_sid, auth_token)
# Make the call
call = client.calls.create(to="+14085551234", # Any phone number
from_="+12125551234", # Must be a valid Twilio number
url="http://twimlets.com/holdmusic?Bucket=com.twilio.music.ambient") # Just plays hold music
Let me know if that helps at all.

How do I retrieve inbound SMS phone number from twilio request in Python

I'm attempting to get the "from" number from an SMS message that I send to my Twilio number.
I have a Django server running that takes this message and performs some back-end processing, but I can't seem to retrieve the "from" number that sends that SMS to the Twilio number via Python. I keep getting 500 errors that state that this 'from' attribute doesn't exist but the Twilio documentation states otherwise.
def respond_to_sms(request):
# save the http request message
twilio_request = decompose(request)
# this works fine
twilio_message_body = twilio_request.body
# this does not
from_number = twilio_request.from
# perform backend stuff on from_number
Anyone know how to do this? I'm using the Django-Twilio package.
Twilio developer evangelist here.
You almost got it right, but it turns out that in the Python library, the name of the attribute is from_ as opposed to just from. That's because in Python, from is a reserved keyword as you can see here.
So try changing your code to the following:
from_number = twilio_request.from_
And you'll get it working. More information about the library can be found here.
Hope this helps you out!

Can I specify the "friendly name" in place of the phone number when sending SMS via Twilio?

Instead of
client = TwilioRestClient(account_sid, auth_token)
message = client.messages.create(to="+12316851234", from_="+15555555555",
body="Hello there!")
I'd like to use the "friendly name", such as "twilio 1".
message = client.messages.create(to="+12316851234", from_="twilio 1",
body="Hello there!")
Twilio developer evangelist here.
You can do this... sometimes.
Twilio allows you to use an alphanumeric string as the sender ID for SMS messages. And you would do it exactly as you have shown, by setting the from parameter as the string. You do need to have an upgraded Twilio account, you can't use this on the free accounts.
However, it is only supported in about 145 countries and sadly, the US is not one of those countries.
I wrote up a blog post with more detail on how you do this. It is in Ruby, but I've not doubt you can follow along.
Edit
And re-reading the post shows that maybe I misunderstood here.
If you are trying to refer to the actual number to send by its friendly name, then that will not work. As I say above, using a string as the from argument will try to send the SMS using that string as an alphanumeric sender ID. It will not send the message from the number. If you are trying to send a message in the US this will then fail as you cannot send from an alphanumeric ID there.
To send a message from your Twilio number, you need to use the number itself as the from argument.
SMS generally does not have a "friendly name" or "string alias" or anything like that. The names that you see on a device come from the device owner's contact list, not the message. Here is a protocol reference: https://en.wikipedia.org/wiki/GSM_03.40
Although the protocol has evolved quite a bit, I don't think there are any plans to add this type of data, as the message is intended to be very compact.

What is the Twilio URL parameter?

I am trying to write a Twilio script to make a call in Python. I am trying to follow the documentation, but am baffled by the url parameter.
The example the documentation supplies is as follows:
from twilio.rest import TwilioRestClient
account_sid = "ACXXXXXXXXXXXXXXXXX"
auth_token = "YYYYYYYYYYYYYYYYYY"
client = TwilioRestClient(account_sid, auth_token)
call = client.calls.create(to="+14085551234", # Any phone number
from_="+12125551234", # Must be a valid Twilio number
url="http://twimlets.com/holdmusic?Bucket=com.twilio.music.ambient")
print call.sid
What is the url parameter doing here? Should I copy it or supply my own URL? Does that mean I have to write my own endpoint?
The documentation on URL parameter is not much more enlightening: https://www.twilio.com/docs/api/rest/making-calls#url-parameter
Please could someone explain what this is, and whether I need to change it or not?
The Url parameter determines what is heard on the call when the phone is answered. This is customizable using TwiML (Twilio Markup Language) a set of roughly a dozen XML tags, which your application should return at the specified url. The XML your application returns at the Url decides how to handle the call, for example whether to forward the call to another line, speak some words or send an SMS reply. You can read a bit more about TwiML here:
https://twilio.radicalskills.com/projects/getting-started-with-twiml/2.html

Categories

Resources