From ec8bae371ef8642dc3290913e68b72abd613e2f7 Mon Sep 17 00:00:00 2001 From: Jeffrey Linwood Date: Fri, 2 Dec 2016 12:42:38 -0600 Subject: [PATCH] Migrate from IP Messaging to Chat --- .env.example | 4 ++-- README.md | 4 ++-- app.py | 18 +++++++++--------- static/{ipmessaging => chat}/index.css | 0 static/{ipmessaging => chat}/index.html | 7 +++---- static/{ipmessaging => chat}/index.js | 22 +++++++++++----------- static/config-check.js | 10 +++++----- static/index.html | 8 ++++---- 8 files changed, 36 insertions(+), 37 deletions(-) rename static/{ipmessaging => chat}/index.css (100%) rename static/{ipmessaging => chat}/index.html (68%) rename static/{ipmessaging => chat}/index.js (87%) diff --git a/.env.example b/.env.example index b0ad489..ae09d3a 100644 --- a/.env.example +++ b/.env.example @@ -6,8 +6,8 @@ TWILIO_API_SECRET= # Required for Video TWILIO_CONFIGURATION_SID= -# Required for IP Messaging -TWILIO_IPM_SERVICE_SID= +# Required for Chat +TWILIO_CHAT_SERVICE_SID= # Required for Notify TWILIO_NOTIFICATION_SERVICE_SID= diff --git a/README.md b/README.md index ef46ef1..0abc82e 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ 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 [IP Messaging](https://www.twilio.com/ip-messaging), +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! @@ -41,7 +41,7 @@ Depending on which demos you'd like to run, you'll need to configure a few more | Config Value | Product Demo | Description | | :------------- |:------------- |:------------- | -`TWILIO_IPM_SERVICE_SID` | IP Messaging | Like a database for your IP Messaging data - [generate one in the console here](https://www.twilio.com/console/ip-messaging/services) +`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) diff --git a/app.py b/app.py index d55056c..9dcfe4b 100644 --- a/app.py +++ b/app.py @@ -28,9 +28,9 @@ def sync(): def notify(): return app.send_static_file('notify/index.html') -@app.route('/ipmessaging/') -def ipmessaging(): - return app.send_static_file('ipmessaging/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 @@ -43,7 +43,7 @@ def config(): TWILIO_GCM_CREDENTIAL_SID=os.environ['TWILIO_GCM_CREDENTIAL_SID'], TWILIO_API_KEY=os.environ['TWILIO_API_KEY'], TWILIO_API_SECRET=bool(os.environ['TWILIO_API_SECRET']), - TWILIO_IPM_SERVICE_SID=os.environ['TWILIO_IPM_SERVICE_SID'], + TWILIO_CHAT_SERVICE_SID=os.environ['TWILIO_CHAT_SERVICE_SID'], TWILIO_SYNC_SERVICE_SID=os.environ['TWILIO_SYNC_SERVICE_SID'], TWILIO_CONFIGURATION_SID=os.environ['TWILIO_CONFIGURATION_SID'] ) @@ -56,7 +56,7 @@ def token(): api_secret = os.environ['TWILIO_API_SECRET'] sync_service_sid = os.environ['TWILIO_SYNC_SERVICE_SID'] configuration_profile_sid = os.environ['TWILIO_CONFIGURATION_SID'] - ipm_service_sid = os.environ['TWILIO_IPM_SERVICE_SID'] + chat_service_sid = os.environ['TWILIO_CHAT_SERVICE_SID'] # create a randomly generated username for the client @@ -79,10 +79,10 @@ def token(): video_grant = ConversationsGrant(configuration_profile_sid=configuration_profile_sid) token.add_grant(video_grant) - # Create an IP Messaging grant and add to token - if ipm_service_sid: - ipm_grant = IpMessagingGrant(endpoint_id=endpoint, service_sid=ipm_service_sid) - token.add_grant(ipm_grant) + # Create an Chat grant and add to token + if chat_service_sid: + chat_grant = IpMessagingGrant(endpoint_id=endpoint, service_sid=chat_service_sid) + token.add_grant(chat_grant) # Return token info as JSON return jsonify(identity=identity, token=token.to_jwt()) diff --git a/static/ipmessaging/index.css b/static/chat/index.css similarity index 100% rename from static/ipmessaging/index.css rename to static/chat/index.css diff --git a/static/ipmessaging/index.html b/static/chat/index.html similarity index 68% rename from static/ipmessaging/index.html rename to static/chat/index.html index 5e93564..c5d76a0 100755 --- a/static/ipmessaging/index.html +++ b/static/chat/index.html @@ -1,14 +1,14 @@ - Twilio IP Messaging Quickstart + Twilio Chat Quickstart
- Read the getting started guide @@ -19,8 +19,7 @@ - - + diff --git a/static/ipmessaging/index.js b/static/chat/index.js similarity index 87% rename from static/ipmessaging/index.js rename to static/chat/index.js index 5a9d38f..6cea9bd 100755 --- a/static/ipmessaging/index.js +++ b/static/chat/index.js @@ -2,11 +2,8 @@ $(function() { // Get handle to the chat div var $chatWindow = $('#messages'); - // Manages the state of our access token we got from the server - var accessManager; - - // Our interface to the IP Messaging service - var messagingClient; + // Our interface to the Chat service + var chatClient; // A handle to the "general" chat channel - the one and only channel we // will have in this sample app @@ -54,19 +51,22 @@ $(function() { print('You have been assigned a random username of: ' + '' + username + '', true); - // Initialize the IP messaging client - accessManager = new Twilio.AccessManager(data.token); - messagingClient = new Twilio.IPMessaging.Client(accessManager); + // Initialize the Chat client + chatClient = new Twilio.Chat.Client(data.token); + chatClient.getUserChannels().then(createOrJoinGeneralChannel); + }); + function createOrJoinGeneralChannel() { // Get the general chat channel, which is where all the messages are // sent in this simple application print('Attempting to join "general" chat channel...'); - var promise = messagingClient.getChannelByUniqueName('general'); + var promise = chatClient.getChannelByUniqueName('general'); promise.then(function(channel) { generalChannel = channel; if (!generalChannel) { // If it doesn't exist, let's create it - messagingClient.createChannel({ + console.log('Creating general channel'); + chatClient.createChannel({ uniqueName: 'general', friendlyName: 'General Chat Channel' }).then(function(channel) { @@ -81,7 +81,7 @@ $(function() { setupChannel(); } }); - }); + } // Set up channel after it has been found function setupChannel() { diff --git a/static/config-check.js b/static/config-check.js index c47f778..e0da1e2 100644 --- a/static/config-check.js +++ b/static/config-check.js @@ -7,7 +7,7 @@ $(function() { configureField(response, 'TWILIO_NOTIFICATION_SERVICE_SID','twilioNotificationServiceSID',false); configureField(response, 'TWILIO_APN_CREDENTIAL_SID','twilioAPNCredentialSID',false); configureField(response, 'TWILIO_GCM_CREDENTIAL_SID','twilioGCMCredentialSID',false); - configureField(response, 'TWILIO_IPM_SERVICE_SID','twilioIPMServiceSID',false); + configureField(response, 'TWILIO_CHAT_SERVICE_SID','twilioChatServiceSID',false); configureField(response, 'TWILIO_SYNC_SERVICE_SID','twilioSyncServiceSID',false); //configure individual product buttons @@ -20,10 +20,10 @@ $(function() { $('#videoDemoButton').addClass('btn-danger'); } - if (response.TWILIO_IPM_SERVICE_SID && response.TWILIO_IPM_SERVICE_SID != '') { - $('#ipmDemoButton').addClass('btn-success'); + if (response.TWILIO_CHAT_SERVICE_SID && response.TWILIO_CHAT_SERVICE_SID != '') { + $('#chatDemoButton').addClass('btn-success'); } else { - $('#ipmDemoButton').addClass('btn-danger'); + $('#chatDemoButton').addClass('btn-danger'); } if (response.TWILIO_SYNC_SERVICE_SID && response.TWILIO_SYNC_SERVICE_SID != '') { @@ -40,7 +40,7 @@ $(function() { } else { $('#videoDemoButton').addClass('btn-danger'); - $('#ipmDemoButton').addClass('btn-danger'); + $('#chatDemoButton').addClass('btn-danger'); $('#syncDemoButton').addClass('btn-danger'); $('#notifyDemoButton').addClass('btn-danger'); } diff --git a/static/index.html b/static/index.html index ed02de4..3d6e53d 100644 --- a/static/index.html +++ b/static/index.html @@ -49,9 +49,9 @@ - IP Messaging - TWILIO_IPM_SERVICE_SID - + Chat + TWILIO_CHAT_SERVICE_SID + Sync @@ -64,7 +64,7 @@ Video Sync Notify - IP Messaging + Chat