Skip to content

Update the Twilio sample to use their latest library #1289

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions appengine/flexible/twilio/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
import os

from flask import Flask, request
from twilio import twiml
from twilio.rest import TwilioRestClient
from twilio import rest
from twilio.twiml import messaging_response, voice_response


# [START configuration]
Expand All @@ -35,7 +35,7 @@
@app.route('/call/receive', methods=['POST'])
def receive_call():
"""Answers a call and replies with a simple greeting."""
response = twiml.Response()
response = voice_response.VoiceResponse()
response.say('Hello from Twilio!')
return str(response), 200, {'Content-Type': 'application/xml'}
# [END receive_call]
Expand All @@ -50,11 +50,12 @@ def send_sms():
return ('Please provide the number to message in the "to" query string'
' parameter.'), 400

client = TwilioRestClient(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN)
client = rest.Client(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN)
rv = client.messages.create(
to=to,
from_=TWILIO_NUMBER,
body='Hello from Twilio!')
body='Hello from Twilio!'
)
return str(rv)
# [END send_sms]

Expand All @@ -68,7 +69,7 @@ def receive_sms():

message = 'Hello, {}, you said: {}'.format(sender, body)

response = twiml.Response()
response = messaging_response.MessagingResponse()
response.message(message)
return str(response), 200, {'Content-Type': 'application/xml'}
# [END receive_sms]
Expand Down
48 changes: 29 additions & 19 deletions appengine/flexible/twilio/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,10 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import re

import json

from googleapiclient.http import HttpMockSequence
import httplib2
import pytest


class HttpMockSequenceWithCredentials(HttpMockSequence):
def add_credentials(self, *args):
pass
import responses


@pytest.fixture
Expand All @@ -41,17 +34,34 @@ def test_receive_call(app):
assert 'Hello from Twilio!' in r.data.decode('utf-8')


@responses.activate
def test_send_sms(app, monkeypatch):
httpmock = HttpMockSequenceWithCredentials([
({'status': '200'}, json.dumps({
'sid': 'sid123'
}))
])

def mock_http_ctor(*args, **kwargs):
return httpmock

monkeypatch.setattr(httplib2, 'Http', mock_http_ctor)
sample_response = {
"sid": "sid",
"date_created": "Wed, 20 Dec 2017 19:32:14 +0000",
"date_updated": "Wed, 20 Dec 2017 19:32:14 +0000",
"date_sent": None,
"account_sid": "account_sid",
"to": "+1234567890",
"from": "+9876543210",
"messaging_service_sid": None,
"body": "Hello from Twilio!",
"status": "queued",
"num_segments": "1",
"num_media": "0",
"direction": "outbound-api",
"api_version": "2010-04-01",
"price": None,
"price_unit": "USD",
"error_code": None,
"error_message": None,
"uri": "/2010-04-01/Accounts/sample.json",
"subresource_uris": {
"media": "/2010-04-01/Accounts/sample/Media.json"
}
}
responses.add(responses.POST, re.compile('.*'),
json=sample_response, status=200)

r = app.get('/sms/send')
assert r.status_code == 400
Expand Down
2 changes: 1 addition & 1 deletion appengine/flexible/twilio/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Flask==0.12.2
gunicorn==19.7.1
twilio<=6.0.0dev,==5.4.0
twilio==6.10.0