Merge pull request #25 from idelgado/task/GSDK-1090-video-notify-example

Allow for generic token, registration, and send-notification routes
This commit is contained in:
Jeff Linwood
2017-08-10 15:54:10 -05:00
committed by GitHub
3 changed files with 22 additions and 17 deletions

33
app.py
View File

@@ -10,7 +10,11 @@ from twilio.jwt.access_token.grants import (
) )
from dotenv import load_dotenv, find_dotenv from dotenv import load_dotenv, find_dotenv
from os.path import join, dirname 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):
return dict(map(lambda (key, value): (underscore(key), value), somedict.items()))
app = Flask(__name__) app = Flask(__name__)
fake = Factory.create() fake = Factory.create()
@@ -50,8 +54,8 @@ def config():
TWILIO_SYNC_SERVICE_SID=os.environ['TWILIO_SYNC_SERVICE_SID'], TWILIO_SYNC_SERVICE_SID=os.environ['TWILIO_SYNC_SERVICE_SID'],
) )
@app.route('/token') @app.route('/token', methods=['POST', 'GET'])
def token(): def token(identity = None):
# get credentials for environment variables # get credentials for environment variables
account_sid = os.environ['TWILIO_ACCOUNT_SID'] account_sid = os.environ['TWILIO_ACCOUNT_SID']
api_key = os.environ['TWILIO_API_KEY'] api_key = os.environ['TWILIO_API_KEY']
@@ -59,9 +63,8 @@ def token():
sync_service_sid = os.environ['TWILIO_SYNC_SERVICE_SID'] sync_service_sid = os.environ['TWILIO_SYNC_SERVICE_SID']
chat_service_sid = os.environ['TWILIO_CHAT_SERVICE_SID'] chat_service_sid = os.environ['TWILIO_CHAT_SERVICE_SID']
# assign the provided identity or generate a new one
# create a randomly generated username for the client identity = identity or fake.user_name()
identity = fake.user_name()
# Create access token with credentials # Create access token with credentials
token = AccessToken(account_sid, api_key, api_secret, identity=identity) token = AccessToken(account_sid, api_key, api_secret, identity=identity)
@@ -98,14 +101,13 @@ def register():
# Body content # Body content
content = request.get_json() content = request.get_json()
content = snake_case_keys(content)
# Get a reference to the notification service # Get a reference to the notification service
service = client.notify.services(service_sid) service = client.notify.services(service_sid)
# Create the binding # Create the binding
binding = service.bindings.create( binding = service.bindings.create(**content)
identity=content["identity"],
binding_type=content["BindingType"],
address=content["Address"])
print(binding) print(binding)
@@ -126,12 +128,13 @@ def send_notification():
service = client.notify.services(service_sid) service = client.notify.services(service_sid)
# Create a notification for a given identity # Get the request json or form data
identity = request.form.get('identity') content = request.get_json() if request.get_json() else request.form
notification = service.notifications.create(
identity=identity, content = snake_case_keys(content)
body='Hello world!'
) # Create a notification with the given form data
notification = service.notifications.create(**content)
return jsonify(message="Notification created!") return jsonify(message="Notification created!")

View File

@@ -2,3 +2,4 @@ Flask==0.10.1
fake-factory==0.5.3 fake-factory==0.5.3
twilio==6.2.0a1 twilio==6.2.0a1
python-dotenv==0.6.0 python-dotenv==0.6.0
inflection==0.3.1

View File

@@ -2,11 +2,12 @@ $(function() {
$('#sendNotificationButton').on('click', function() { $('#sendNotificationButton').on('click', function() {
$.post('/send-notification', { $.post('/send-notification', {
identity: $('#identityInput').val() identity: $('#identityInput').val(),
body: "Hello, World!"
}, function(response) { }, function(response) {
$('#identityInput').val(''); $('#identityInput').val('');
$('#message').html(response.message); $('#message').html(response.message);
console.log(response); console.log(response);
}); });
}); });
}); });