-
-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathapp.py
58 lines (44 loc) · 1.83 KB
/
app.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
import os
from dotenv import load_dotenv
from flask import Flask, render_template, request, abort
from twilio.jwt.access_token import AccessToken
from twilio.jwt.access_token.grants import VideoGrant, ChatGrant
from twilio.rest import Client
from twilio.base.exceptions import TwilioRestException
load_dotenv()
twilio_account_sid = os.environ.get('TWILIO_ACCOUNT_SID')
twilio_api_key_sid = os.environ.get('TWILIO_API_KEY_SID')
twilio_api_key_secret = os.environ.get('TWILIO_API_KEY_SECRET')
twilio_client = Client(twilio_api_key_sid, twilio_api_key_secret,
twilio_account_sid)
app = Flask(__name__)
def get_chatroom(name):
for conversation in twilio_client.conversations.conversations.stream():
if conversation.friendly_name == name:
return conversation
# a conversation with the given name does not exist ==> create a new one
return twilio_client.conversations.conversations.create(
friendly_name=name)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/login', methods=['POST'])
def login():
username = request.get_json(force=True).get('username')
if not username:
abort(401)
conversation = get_chatroom('My Room')
try:
conversation.participants.create(identity=username)
except TwilioRestException as exc:
# do not error if the user is already in the conversation
if exc.status != 409:
raise
token = AccessToken(twilio_account_sid, twilio_api_key_sid,
twilio_api_key_secret, identity=username)
token.add_grant(VideoGrant(room='My Room'))
token.add_grant(ChatGrant(service_sid=conversation.chat_service_sid))
return {'token': token.to_jwt().decode(),
'conversation_sid': conversation.sid}
if __name__ == '__main__':
app.run(host='0.0.0.0')