Skip to content

Commit a70f50b

Browse files
michaelawyuJon Wayne Parrott
michaelawyu
authored and
Jon Wayne Parrott
committed
Update the Twilio sample to use their latest library (#1289)
* Update the Twilio sample to use their latest library * Fixed lint error * Update main.py * Update main.py
1 parent b55b71c commit a70f50b

File tree

3 files changed

+37
-26
lines changed

3 files changed

+37
-26
lines changed

appengine/flexible/twilio/main.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
import os
1818

1919
from flask import Flask, request
20-
from twilio import twiml
21-
from twilio.rest import TwilioRestClient
20+
from twilio import rest
21+
from twilio.twiml import messaging_response, voice_response
2222

2323

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

53-
client = TwilioRestClient(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN)
53+
client = rest.Client(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN)
5454
rv = client.messages.create(
5555
to=to,
5656
from_=TWILIO_NUMBER,
57-
body='Hello from Twilio!')
57+
body='Hello from Twilio!'
58+
)
5859
return str(rv)
5960
# [END send_sms]
6061

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

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

71-
response = twiml.Response()
72+
response = messaging_response.MessagingResponse()
7273
response.message(message)
7374
return str(response), 200, {'Content-Type': 'application/xml'}
7475
# [END receive_sms]

appengine/flexible/twilio/main_test.py

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,10 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
import re
1415

15-
import json
16-
17-
from googleapiclient.http import HttpMockSequence
18-
import httplib2
1916
import pytest
20-
21-
22-
class HttpMockSequenceWithCredentials(HttpMockSequence):
23-
def add_credentials(self, *args):
24-
pass
17+
import responses
2518

2619

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

4336

37+
@responses.activate
4438
def test_send_sms(app, monkeypatch):
45-
httpmock = HttpMockSequenceWithCredentials([
46-
({'status': '200'}, json.dumps({
47-
'sid': 'sid123'
48-
}))
49-
])
50-
51-
def mock_http_ctor(*args, **kwargs):
52-
return httpmock
53-
54-
monkeypatch.setattr(httplib2, 'Http', mock_http_ctor)
39+
sample_response = {
40+
"sid": "sid",
41+
"date_created": "Wed, 20 Dec 2017 19:32:14 +0000",
42+
"date_updated": "Wed, 20 Dec 2017 19:32:14 +0000",
43+
"date_sent": None,
44+
"account_sid": "account_sid",
45+
"to": "+1234567890",
46+
"from": "+9876543210",
47+
"messaging_service_sid": None,
48+
"body": "Hello from Twilio!",
49+
"status": "queued",
50+
"num_segments": "1",
51+
"num_media": "0",
52+
"direction": "outbound-api",
53+
"api_version": "2010-04-01",
54+
"price": None,
55+
"price_unit": "USD",
56+
"error_code": None,
57+
"error_message": None,
58+
"uri": "/2010-04-01/Accounts/sample.json",
59+
"subresource_uris": {
60+
"media": "/2010-04-01/Accounts/sample/Media.json"
61+
}
62+
}
63+
responses.add(responses.POST, re.compile('.*'),
64+
json=sample_response, status=200)
5565

5666
r = app.get('/sms/send')
5767
assert r.status_code == 400
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
Flask==0.12.2
22
gunicorn==19.7.1
3-
twilio<=6.0.0dev,==5.4.0
3+
twilio==6.10.0

0 commit comments

Comments
 (0)