I am using Twilio Programmable Voice and Python. Following documentation on how to make an outbound call, here is my code:
# Download the helper library from https://www.twilio.com/docs/python/install
import os
from twilio.rest import Client
# Find your Account SID and Auth Token at twilio.com/console
# and set the environment variables. See http://twil.io/secure
account_sid = os.environ['TWILIO_ACCOUNT_SID']
auth_token = os.environ['TWILIO_AUTH_TOKEN']
client = Client(account_sid, auth_token)
string = 'Input from a function'
call = client.calls.create(
twiml='<Response><Say>string</Say></Response>',
to='+15558675310',
from_='+15552223214'
)
print(call.sid)
How can I include that "string" variable to be spoken via phone? This variable "string" will be the output from a function, so it changes.
In order to have that string input from your function be part of the returning TwiML you would need to use string interpolation. If you're using Python 3.6 or higher you should be able to use the string interpolation function like this:
twiml=f'<Response><Say>{string}</Say></Response>',
For more information on string interpolation and how it works you can take a look here: Python Literal String Interpolation
twiml='<Response><Say>' + URL_STRING_VAR + '</Say></Response>',
Related
I want to create rest interface in flask to support opencv image detection. One of parameters needed for opencv matchTemplate method is match method. This is an integer which is defined as variable in opencv like cv2.TM_CCOEFF_NORMED. I'm wondering how to pass this like name 'cv2.TM_CCOEFF_NORMED' instead of integer. Do you know how to do this? I want to sent post request with, for example, matchingMethod=cv2.TM_CCOEFF_NORMED parameter and then pass it to the cv2.matchTemplate method(https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_template_matching/py_template_matching.html).
You can get an entity's name as a string by calling its __name__ property.
from cv2 import TM_CCOEFF_NORMED
coeff = TM_COEFF_NORMED.__name__
print(coeff) # should return 'TM_CCOEFF_NORMED'
You can now pass coeff's value to your frontend app as a string.
Use the built-in getattr function to call it when you receive it from your app.
import cv2
cv_coeff = 'TM_CCOEFF_NORMED' # comes from frontend app
match_coeff = getattr(cv2, cv_coeff) # equals TM_CCOEFF_NORMED's integer value
cv2.matchTemplate(img, template, match_coeff)
I am working on Python code to create Google Cloud trigger, I am not able to add substitutions variable.
Currently I have below code
from google.cloud.devtools import cloudbuild_v1
client = cloudbuild_v1.CloudBuildClient()
build_trigger_template = cloudbuild_v1.types.BuildTrigger()
build_trigger_template.description = 'test to create trigger'
build_trigger_template.name = 'github-cloudbuild-trigger1'
build_trigger_template.github.name = 'github-cloudbuild'
build_trigger_template.github.pull_request.branch = 'master'
build_trigger_template.filename = 'cloudbuild.yaml'
response = client.create_build_trigger('dev',
build_trigger_template)
I want to add two substitutions variables _ENV and _PROJECT, I tried below mentioned way but not working.
build_trigger_template.substitutions = {'_ENV': 'test',
'_PROJECT': 'pro-test'}
Error: AttributeError: Assignment not allowed to repeated field "substitutions" in protocol message object.
Thanks,
Raghunath.
This is an issue with assigning protobuf object.
If you look on the object using dir(build_trigger_template.substitutions)
you'll find an .update method that will accept a dictionary.
so try the below, it should return None but your structure will be updated.
build_trigger_template.substitutions.update({'_ENV': 'test',
'_PROJECT': 'pro-test'})
I'm trying to form a Marvel API Call.
Here's a link on authorization:
https://developer.marvel.com/documentation/authorization
I'm attempting to create a server-side application, so according to the link above, I need a timestamp, apikey, and hash url parameters. The hash needs be a md5 hash of the form: md5(timestamp + privateKey + publicKey) and the apikey url param is my public key.
Here's my code, I'm making the request in Python 3, using the request library to form the request, the time library to form the timestamp, and the hashlib library to form the hash.
#request.py: making a http request to marvel api
import requests;
import time;
import hashlib;
#timestamp
ts = time.time();
ts_str = str(float(ts));
#keys
public_key = 'a3c785ecc50aa21b134fca1391903926';
private_key = 'my_private_key';
#hash and encodings
m_hash = hashlib.md5();
ts_str_byte = bytes(ts_str, 'utf-8');
private_key_byte = bytes(private_key, 'utf-8');
public_key_byte = bytes(public_key, 'utf-8');
m_hash.update(ts_str_byte + private_key_byte + public_key_byte);
m_hash_str = str(m_hash.digest());
#all request parameters
payload = {'ts': ts_str, 'apikey': 'a3c785ecc50aa21b134fca1391903926', 'hash': m_hash_str};
#make request
r = requests.get('https://gateway.marvel.com:443/v1/public/characters', params=payload);
#for debugging
print(r.url);
print(r.json());
Here's the output:
$python3 request.py
https://gateway.marvel.com:443/v1/public/characters...${URL TRUNCATED FOR READABILITY)
{'code': 'InvalidCredentials', 'message': 'That hash, timestamp, and key combination is invalid'}
$
I'm not sure what exactly is causing the combination to be invalid.
I can provide more info on request. Any info would be appreciated. Thank you!
EDIT:
I'm a little new to API calls in general. Are there any resources for understanding more about how to perform them? So far with my limited experience they seem very specific, and getting each one to work takes a while. I'm a college student and whenever I work in hackathons it takes me a long time just to figure out how to perform the API call. I admit I'm not experienced, but in general does figuring out new API's require a large learning curve, even for individuals who have done 10 or so of them?
Again, thanks for your time :)
I've also had similar issues when accessing the Marvel API key. For those that are still struggling, here is my templated code (that I use in a jupyter notebook).
# import dependencies
import hashlib #this is needed for the hashing library
import time #this is needed to produce a time stamp
import json #Marvel provides its information in json format
import requests #This is used to request information from the API
#Constructing the Hash
m = hashlib.md5() #I'm assigning the method to the variable m. Marvel
#requires md5 hashing, but I could also use SHA256 or others for APIS other
#than Marvel's
ts = str(time.time()) #This creates the time stamp as a string
ts_byte = bytes(ts, 'utf-8') #This converts the timestamp into a byte
m.update(ts_byte) # I add the timestamp (in byte format) to the hash
m.update(b"my_private_key") #I add the private key to
#the hash.Notice I added the b in front of the string to convert it to byte
#format, which is required for md5
m.update(b"b2aeb1c91ad82792e4583eb08509f87a") #And now I add my public key to
#the hash
hasht = m.hexdigest() #Marvel requires the string to be in hex; they
#don't say this in their API documentation, unfortunately.
#constructing the query
base_url = "https://gateway.marvel.com" #provided in Marvel API documentation
api_key = "b2aeb1c91ad82792e4583eb08509f87a" #My public key
query = "/v1/public/events" +"?" #My query is for all the events in Marvel Uni
#Building the actual query from the information above
query_url = base_url + query +"ts=" + ts+ "&apikey=" + api_key + "&hash=" +
hasht
print(query_url) #I like to look at the query before I make the request to
#ensure that it's accurate.
#Making the API request and receiving info back as a json
data = requests.get(query_url).json()
print(data) #I like to view the data to make sure I received it correctly
Give credit where credit is due, I relied on this blog a lot. You can go here for more information on the hashlib library. https://docs.python.org/3/library/hashlib.html
I noticed in your terminal your MD5 hash is uppercase. MD5 should output in lowercase. Make sure you convert to that.
That was my issue, I was sending an uppercase hash.
As mentioned above, the solution was that the hash wasn't formatted properly. Needed to be a hexadecimal string and the issue is resolved.
Your final URL should be like this:
http:// gateway.marvel.com/v1/public/characters?apikey=(public_key)&ts=1&hash=(md5_type_hash)
So, you already have public key in developer account. However, how can you produce md5_type_hash?
ts=1 use just 1. So, your pattern should be this:
1 + private_key(ee7) + public_key(aa3). For example: Convert 1ee7aa3 to MD5-Hash = 1ca3591360a252817c30a16b615b0afa (md5_type_hash)
You can create from this website: https://www.md5hashgenerator.com
Done, you can use marvel api now!
Whatever and whenever user speaks I want to pass it to my API and send API response to Alexa using python flask-ask.
I don't want to use AWS lambda. I want to know is it possible and if it does then how can I achieve it.
eg:
UserVoiceInput = anything that user says
def anyfunc():
abc = MyApiUrl?message=UserVoiceInput
return statement(abc["Response_Message"])
how can I achieve the above logic using python flask-ask
Yes you can achieve this with flask-ask
#ask.intent("IntentName")
def function(slotvalue):
example = get_api_response(slotvalue)
example_msg = 'the result is {}'.format(example)
return statement(example_msg)
then define your function some thing like below.
def get_api_response(value):
# Do your api call
return response
Python newbie here, I'm trying to search a video API which, for some reason won't allow me to search video titles with certain characters in the video title such as : or |
Currently, I have a function which calls the video API library and searches by title, which looks like this:
def videoNameExists(vidName):
vidName = vidName.encode("utf-8")
bugFixVidName = vidName.replace(":", "")
search_url ='http://cdn-api.ooyala.com/v2/syndications/49882e719/feed?pcode=1xeGMxOt7GBjZPp2'.format(bugFixVidName) #this URL is altered to protect privacy for this post
Is there an alternative to .replace() (or a way to use it that I'm missing) that would let me search for more than one sub-string at the same time?
Take a look a the Python re module, specifically at the method re.sub().
Here's an example for your case:
import re
def videoNameExists(vidName):
vidName = vidName.encode("utf-8")
# bugFixVidName = vidName.replace(":", "")
bugFixVidName = re.sub(r'[:|]', "", vidName)
search_url ='http://cdn-api.ooyala.com/v2/syndications/49882e719/feed?pcode=1xeGMxOt7GBjZPp2'.format(bugFixVidName) #this URL is altered to protect privacy for this post