Merge pull request #9 from TwilioDevEd/upgrade-twilio-python-to-6.0.0

Upgraded twilio-python to 6.0.0
This commit is contained in:
Jeff Linwood
2017-04-03 21:13:27 -05:00
committed by GitHub
4 changed files with 30 additions and 26 deletions

View File

@@ -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=

View File

@@ -1,8 +1,12 @@
<a href="https://www.twilio.com">
<img src="https://static0.twilio.com/marketing/bundles/marketing/img/logos/wordmark-red.svg" alt="Twilio" width="250" />
</a>
# 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`:

37
app.py
View File

@@ -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']
@@ -61,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:
@@ -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)
app.run(debug=True)

View File

@@ -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