From 0efbb977dc2eab0ada927ebdd4aa8f4fdbb9efca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Oliveros?= Date: Thu, 30 Mar 2017 17:02:38 -0600 Subject: [PATCH 1/2] Upgradet twilio-python to 6.0.0 --- .env.example | 2 -- README.md | 15 +++++++++------ app.py | 35 +++++++++++++++++++---------------- requirements.txt | 2 +- 4 files changed, 29 insertions(+), 25 deletions(-) diff --git a/.env.example b/.env.example index 9531959..d3ea350 100644 --- a/.env.example +++ b/.env.example @@ -3,8 +3,6 @@ TWILIO_ACCOUNT_SID= TWILIO_API_KEY= TWILIO_API_SECRET= -# Required for Video -TWILIO_CONFIGURATION_SID= # Required for Chat TWILIO_CHAT_SERVICE_SID= diff --git a/README.md b/README.md index 05d39b1..8d4248a 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,12 @@ + + Twilio + + # Twilio SDK Starter Application for Python -This sample project demonstrates how to use Twilio APIs in a Python web +This sample project demonstrates how to use Twilio APIs in a Python web application. Once the app is up and running, check out [the home page](http://localhost:5000) -to see which demos you can run. You'll find examples for [Chat](https://www.twilio.com/chat), +to see which demos you can run. You'll find examples for [Chat](https://www.twilio.com/chat), [Video](https://www.twilio.com/video), [Sync](https://www.twilio.com/sync), and more. Let's get started! @@ -31,18 +35,17 @@ Every sample in the demo requires some basic credentials from your Twilio accoun #### A Note on API Keys -When you generate an API key pair at the URLs above, your API Secret will only be shown once - +When you generate an API key pair at the URLs above, your API Secret will only be shown once - make sure to save this information in a secure location, or possibly your `~/.bash_profile`. ### Configure product-specific settings -Depending on which demos you'd like to run, you'll need to configure a few more values in your +Depending on which demos you'd like to run, you'll need to configure a few more values in your `.env` file. | Config Value | Product Demo | Description | | :------------- |:------------- |:------------- | `TWILIO_CHAT_SERVICE_SID` | Chat | Like a database for your Chat data - [generate one in the console here](https://www.twilio.com/console/chat/services) -`TWILIO_CONFIGURATION_SID` | Video | Identifier for a set of config properties for your video application - [find yours here](https://www.twilio.com/console/video/profiles) `TWILIO_SYNC_SERVICE_SID` | Sync (Preview) | Like a database for your Sync data - generate one with the curl command below. `TWILIO_NOTIFICATION_SERVICE_SID` | Notify (Preview) | You will need to create a Notify service - [generate one here](https://www.twilio.com/console/notify/services) @@ -64,7 +67,7 @@ You will need to create a Notify Service through the [Twilio Console](https://ww ## Run the sample application -This application uses the lightweight [Flask Framework](http://flask.pocoo.org/). +This application uses the lightweight [Flask Framework](http://flask.pocoo.org/). We need to set up your Python environment. Install `virtualenv` via `pip`: diff --git a/app.py b/app.py index a56dc8a..694b442 100644 --- a/app.py +++ b/app.py @@ -2,7 +2,12 @@ import os from flask import Flask, jsonify, request from faker import Factory from twilio.rest import Client -from twilio.jwt.access_token import AccessToken, SyncGrant, ConversationsGrant, IpMessagingGrant +from twilio.jwt.access_token import AccessToken +from twilio.jwt.access_token.grants import ( + SyncGrant, + VideoGrant, + IpMessagingGrant +) from dotenv import load_dotenv, find_dotenv from os.path import join, dirname @@ -53,7 +58,6 @@ def token(): api_key = os.environ['TWILIO_API_KEY'] api_secret = os.environ['TWILIO_API_SECRET'] sync_service_sid = os.environ['TWILIO_SYNC_SERVICE_SID'] - configuration_profile_sid = os.environ['TWILIO_CONFIGURATION_SID'] chat_service_sid = os.environ['TWILIO_CHAT_SERVICE_SID'] @@ -69,9 +73,8 @@ def token(): token.add_grant(sync_grant) # Create a Video grant and add to token - if configuration_profile_sid: - video_grant = ConversationsGrant(configuration_profile_sid=configuration_profile_sid) - token.add_grant(video_grant) + video_grant = VideoGrant(room='default_room') + token.add_grant(video_grant) # Create an Chat grant and add to token if chat_service_sid: @@ -89,24 +92,24 @@ def register(): 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() # Get a reference to the notification service - service = client.notify.v1.services(service_sid) + service = client.notify.services(service_sid) # Create the binding binding = service.bindings.create( - endpoint=content["endpoint"], - identity=content["identity"], - binding_type=content["BindingType"], + endpoint=content["endpoint"], + identity=content["identity"], + binding_type=content["BindingType"], address=content["Address"]) - print binding + print(binding) # Return success message return jsonify(message="Binding created!") @@ -119,12 +122,12 @@ def send_notification(): 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.v1.services(service_sid) - + service = client.notify.services(service_sid) + # Create a notification for a given identity identity = request.form.get('identity') notification = service.notifications.create( @@ -139,4 +142,4 @@ def static_file(path): return app.send_static_file(path) if __name__ == '__main__': - app.run(debug=True) \ No newline at end of file + app.run(debug=True) diff --git a/requirements.txt b/requirements.txt index cc4ed37..ab3457f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,4 @@ Flask==0.10.1 fake-factory==0.5.3 -twilio==6.0.0rc12 +twilio>=6.0.0rc13,<6.1.0 python-dotenv==0.6.0 From 74105f5d825f48414ac4ca24e427264a4e602d36 Mon Sep 17 00:00:00 2001 From: Jeffrey Linwood Date: Thu, 30 Mar 2017 21:11:42 -0500 Subject: [PATCH 2/2] Add named parameter for identity to access token --- app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app.py b/app.py index 694b442..ec52876 100644 --- a/app.py +++ b/app.py @@ -65,7 +65,7 @@ def token(): identity = fake.user_name() # Create access token with credentials - token = AccessToken(account_sid, api_key, api_secret, identity) + token = AccessToken(account_sid, api_key, api_secret, identity=identity) # Create a Sync grant and add to token if sync_service_sid: