forked from TwilioDevEd/sdk-starter-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
171 lines (132 loc) · 5.3 KB
/
app.py
File metadata and controls
171 lines (132 loc) · 5.3 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import os
from flask import Flask, jsonify, request
from faker import Faker
from twilio.rest import Client
from twilio.jwt.access_token import AccessToken
from twilio.jwt.access_token.grants import (
SyncGrant,
VideoGrant,
ChatGrant
)
from dotenv import load_dotenv, find_dotenv
from os.path import join, dirname
from inflection import underscore
# Convert keys to snake_case to conform with the twilio-python api definition contract
def snake_case_keys(somedict):
snake_case_dict = {}
for key, value in somedict.items():
snake_case_dict[underscore(key)] = value
return snake_case_dict
app = Flask(__name__)
fake = Faker()
dotenv_path = join(dirname(__file__), '.env')
load_dotenv(dotenv_path)
@app.route('/')
def index():
return app.send_static_file('index.html')
@app.route('/video/')
def video():
return app.send_static_file('video/index.html')
@app.route('/sync/')
def sync():
return app.send_static_file('sync/index.html')
@app.route('/notify/')
def notify():
return app.send_static_file('notify/index.html')
@app.route('/chat/')
def chat():
return app.send_static_file('chat/index.html')
# Basic health check - check environment variables have been configured
# correctly
@app.route('/config')
def config():
return jsonify(
TWILIO_ACCOUNT_SID=os.environ['TWILIO_ACCOUNT_SID'],
TWILIO_NOTIFICATION_SERVICE_SID=os.environ.get('TWILIO_NOTIFICATION_SERVICE_SID', None),
TWILIO_API_KEY=os.environ['TWILIO_API_KEY'],
TWILIO_API_SECRET=bool(os.environ['TWILIO_API_SECRET']),
TWILIO_CHAT_SERVICE_SID=os.environ.get('TWILIO_CHAT_SERVICE_SID', None),
TWILIO_SYNC_SERVICE_SID=os.environ.get('TWILIO_SYNC_SERVICE_SID', 'default'),
)
@app.route('/token', methods=['GET'])
def randomToken():
return generateToken(fake.user_name())
@app.route('/token', methods=['POST'])
def createToken():
# Get the request json or form data
content = request.get_json() or request.form
# get the identity from the request, or make one up
identity = content.get('identity', fake.user_name())
return generateToken(identity)
@app.route('/token/<identity>', methods=['POST', 'GET'])
def token(identity):
return generateToken(identity)
def generateToken(identity):
# get credentials for environment variables
account_sid = os.environ['TWILIO_ACCOUNT_SID']
api_key = os.environ['TWILIO_API_KEY']
api_secret = os.environ['TWILIO_API_SECRET']
sync_service_sid = os.environ.get('TWILIO_SYNC_SERVICE_SID', 'default')
chat_service_sid = os.environ.get('TWILIO_CHAT_SERVICE_SID', None)
# Create access token with credentials
token = AccessToken(account_sid, api_key, api_secret, identity=identity)
# Create a Sync grant and add to token
if sync_service_sid:
sync_grant = SyncGrant(service_sid=sync_service_sid)
token.add_grant(sync_grant)
# Create a Video grant and add to token
video_grant = VideoGrant()
token.add_grant(video_grant)
# Create an Chat grant and add to token
if chat_service_sid:
chat_grant = ChatGrant(service_sid=chat_service_sid)
token.add_grant(chat_grant)
# Return token info as JSON
return jsonify(identity=identity, token=token.to_jwt().decode('utf-8'))
# Notify - create a device binding from a POST HTTP request
@app.route('/register', methods=['POST'])
def register():
# get credentials for environment variables
account_sid = os.environ['TWILIO_ACCOUNT_SID']
api_key = os.environ['TWILIO_API_KEY']
api_secret = os.environ['TWILIO_API_SECRET']
service_sid = os.environ['TWILIO_NOTIFICATION_SERVICE_SID']
# Initialize the Twilio client
client = Client(api_key, api_secret, account_sid)
# Body content
content = request.get_json()
content = snake_case_keys(content)
# Get a reference to the notification service
service = client.notify.services(service_sid)
# Create the binding
binding = service.bindings.create(**content)
print(binding)
# Return success message
return jsonify(message="Binding created!")
# Notify - send a notification from a POST HTTP request
@app.route('/send-notification', methods=['POST'])
def send_notification():
# get credentials for environment variables
account_sid = os.environ['TWILIO_ACCOUNT_SID']
api_key = os.environ['TWILIO_API_KEY']
api_secret = os.environ['TWILIO_API_SECRET']
service_sid = os.environ['TWILIO_NOTIFICATION_SERVICE_SID']
# Initialize the Twilio client
client = Client(api_key, api_secret, account_sid)
service = client.notify.services(service_sid)
# Get the request json or form data
content = request.get_json() if request.get_json() else request.form
content = snake_case_keys(content)
# Create a notification with the given form data
notification = service.notifications.create(**content)
return jsonify(message="Notification created!")
@app.route('/<path:path>')
def static_file(path):
return app.send_static_file(path)
# Ensure that the Sync Default Service is provisioned
def provision_sync_default_service():
client = Client(os.environ['TWILIO_API_KEY'], os.environ['TWILIO_API_SECRET'], os.environ['TWILIO_ACCOUNT_SID'])
client.sync.services('default').fetch()
if __name__ == '__main__':
provision_sync_default_service()
app.run(debug=True, host='0.0.0.0')