Skip to content

Instantly share code, notes, and snippets.

@matbor
Last active December 22, 2020 02:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matbor/a4f451eda8188312448a443d0dbef9a8 to your computer and use it in GitHub Desktop.
Save matbor/a4f451eda8188312448a443d0dbef9a8 to your computer and use it in GitHub Desktop.
Quick example of using python and Twilio for connecting agent to customer using the VERIFIED caller id of the agent to make the call.
# Download the helper library from https://www.twilio.com/docs/python/install
from twilio.rest import Client
import os
from twilio.twiml.voice_response import Dial, VoiceResponse
from flask import Flask, request
app = Flask(__name__)
# Your Account SID from twilio.com/console
account_sid = os.environ.get('TWILIO_ACCOUNT_SID')
# Your Auth Token from twilio.com/console
auth_token = os.environ.get('TWILIO_AUTH_TOKEN')
# YOUR verfied caller id
callerid = str(os.environ.get('TWILIO_MYCALLERID'))
# the number you have purchased in twilio that makes the initial call to the agent
twilio_number = str(os.environ.get('TWILIO_NUMBER'))
# URL of where this file is hosted... {URL}/connectcustomer
twimlurl = 'http://xxxxxxxx.ngrok.io/connectcustomer' #using ngrok locally to test you will need to change this url so that when the agent is connected it calls back this program.
customer_number = '' # leave blank, just setting up var
callerid_yesno = "yes" # use verified caller id
client = Client(account_sid, auth_token)
# provides the xml Twiml to connect the customer to the agent
@app.route("/connectcustomer", methods=['GET', 'POST'])
def connect_customer():
response = VoiceResponse()
if callerid_yesno == "yes":
response.dial(number=customer_number, callerId=callerid)
else:
response.dial(number=customer_number)
print(str(response))
return str(response)
#form main menu
@app.route('/')
def form():
return '''
<form action="/connect_agent" method="post">
Agent #: <input name="agent_number" type="text" /><br>
Customer #: <input name="customer_number" type="text" /><br>
Caller ID? yes/no : <input name="callerid_yesno" type="text" value="yes" /><br><br>
<input value="Connect" type="submit" />
</form>
'''
return "ok"
# connect agent first
@app.route("/connect_agent", methods=['POST'])
def connect_agent():
global customer_number
global callerid_yesno
if request.method == 'POST':
agent_number = request.form['agent_number']
customer_number = request.form['customer_number']
callerid_yesno = request.form['callerid_yesno']
call = client.calls.create(
url=twimlurl,
to=agent_number,
from_=twilio_number
)
print(call.sid)
return "ok"
return "not post"
if __name__ == "__main__":
print("running")
app.run(debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment