Skip to content

Instantly share code, notes, and snippets.

@krectra
Last active June 2, 2019 15:19
Show Gist options
  • Save krectra/f3f329e2c51fe14e3d61 to your computer and use it in GitHub Desktop.
Save krectra/f3f329e2c51fe14e3d61 to your computer and use it in GitHub Desktop.
Twilio Call API
# coding: utf8
from twilio.util import TwilioCapability
import twilio.twiml
import re
import json
caller_id ="number"
def voice():
"""Generate Dial/Call Response"""
# Destination/To number
dest_number = request.get_vars['to']
resp = twilio.twiml.Response()
if (len(dest_number) < 10):
caller_id = "client:" + request.get_vars['from']
else:
caller_id = caller_id
with resp.dial(callerId=caller_id, timeout=300) as r:
#If we have a number, and it looks like a phone number:
if dest_number and (len(dest_number) > 9) and re.search('^[\d\(\)\- \+]+$', dest_number):
r.number(dest_number)
else:
r.client(pref_set + dest_number)
return str(resp)
def generate_token():
"""Generate Capability Token"""
token = ''
success = False
try:
# Caller/From number
client_name = request.get_vars['client']
apikey = request.get_vars['apikey'] if 'apikey' in request.get_vars else ''
user_token = request.get_vars['token'] if 'token' in request.get_vars else ''
# Account Sid and Auth Token from twilio.com/user/account
account_sid = "Account Sid"
auth_token = "Auth Token"
# Twilio Application Sid
application_sid = "APP SID"
capability = TwilioCapability(account_sid, auth_token)
capability.allow_client_outgoing(application_sid)
capability.allow_client_incoming(client_name)
token = capability.generate()
success = True
except twilio.TwilioRestException as e:
token = 'Error occured'
return json.dumps({'capability_token':token, 'success':success})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment