-
Notifications
You must be signed in to change notification settings - Fork 57
/
server.py
109 lines (91 loc) · 3.45 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import os
from flask import Flask, request
from twilio.jwt.access_token import AccessToken
from twilio.jwt.access_token.grants import VoiceGrant
from twilio.rest import Client
from twilio.twiml.voice_response import VoiceResponse
ACCOUNT_SID = 'AC***'
API_KEY = 'SK***'
API_KEY_SECRET = '***'
PUSH_CREDENTIAL_SID = 'CR***'
APP_SID = 'AP***'
"""
Use a valid Twilio number by adding to your account via https://www.twilio.com/console/phone-numbers/verified
"""
CALLER_NUMBER = '1234567890'
"""
The caller id used when a client is dialed.
"""
CALLER_ID = 'client:quick_start'
IDENTITY = 'alice'
app = Flask(__name__)
"""
Creates an access token with VoiceGrant using your Twilio credentials.
"""
@app.route('/accessToken', methods=['GET', 'POST'])
def token():
account_sid = os.environ.get("ACCOUNT_SID", ACCOUNT_SID)
api_key = os.environ.get("API_KEY", API_KEY)
api_key_secret = os.environ.get("API_KEY_SECRET", API_KEY_SECRET)
push_credential_sid = os.environ.get("PUSH_CREDENTIAL_SID", PUSH_CREDENTIAL_SID)
app_sid = os.environ.get("APP_SID", APP_SID)
grant = VoiceGrant(
push_credential_sid=push_credential_sid,
outgoing_application_sid=app_sid
)
identity = request.values["identity"] \
if request.values and request.values["identity"] else IDENTITY
token = AccessToken(account_sid, api_key, api_key_secret, identity=identity)
token.add_grant(grant)
return token.to_jwt()
"""
Creates an endpoint that plays back a greeting.
"""
@app.route('/incoming', methods=['GET', 'POST'])
def incoming():
resp = VoiceResponse()
resp.say("Congratulations! You have received your first inbound call! Good bye.")
return str(resp)
"""
Makes a call to the specified client using the Twilio REST API.
"""
@app.route('/placeCall', methods=['GET', 'POST'])
def placeCall():
account_sid = os.environ.get("ACCOUNT_SID", ACCOUNT_SID)
api_key = os.environ.get("API_KEY", API_KEY)
api_key_secret = os.environ.get("API_KEY_SECRET", API_KEY_SECRET)
client = Client(api_key, api_key_secret, account_sid)
to = request.values.get("to")
call = None
if to is None or len(to) == 0:
call = client.calls.create(url=request.url_root + 'incoming', to='client:' + IDENTITY, from_=CALLER_ID)
elif to[0] in "+1234567890" and (len(to) == 1 or to[1:].isdigit()):
call = client.calls.create(url=request.url_root + 'incoming', to=to, from_=CALLER_NUMBER)
else:
call = client.calls.create(url=request.url_root + 'incoming', to='client:' + to, from_=CALLER_ID)
return str(call)
"""
Creates an endpoint that can be used in your TwiML App as the Voice Request Url.
In order to make an outgoing call using Twilio Voice SDK, you need to provide a
TwiML App SID in the Access Token. You can run your server, make it publicly
accessible and use `/makeCall` endpoint as the Voice Request Url in your TwiML App.
"""
@app.route('/makeCall', methods=['GET', 'POST'])
def makeCall():
resp = VoiceResponse()
to = request.values.get("to")
if to is None or len(to) == 0:
resp.say("Congratulations! You have just made your first call! Good bye.")
elif to[0] in "+1234567890" and (len(to) == 1 or to[1:].isdigit()):
resp.dial(callerId=CALLER_NUMBER).number(to)
else:
resp.dial(callerId=CALLER_ID).client(to)
return str(resp)
@app.route('/', methods=['GET', 'POST'])
def welcome():
resp = VoiceResponse()
resp.say("Welcome to Twilio")
return str(resp)
if __name__ == "__main__":
port = int(os.environ.get("PORT", 5000))
app.run(host='0.0.0.0', port=port, debug=True)