8000 Initial Commit · kevindias/twilio-python@2ce43ff · GitHub
[go: up one dir, main page]

Skip to content

Commit 2ce43ff

Browse files
author
Kyle Conroy
committed
Initial Commit
0 parents  commit 2ce43ff

File tree

6 files changed

+625
-0
lines changed

6 files changed

+625
-0
lines changed

LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Copyright (c) 2009 Twilio, Inc.
2+
3+
Permission is hereby granted, free of charge, to any person
4+
obtaining a copy of this software and associated documentation
5+
files (the "Software"), to deal in the Software without
6+
restriction, including without limitation the rights to use,
7+
copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the
9+
Software is furnished to do so, subject to the following
10+
conditions:
11+
12+
The above copyright notice and this permission notice shall be
13+
included in all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22+
OTHER DEALINGS IN THE SOFTWARE.

README.markdown

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
## Python Twilio Helper Library
2+
3+
### DESCRIPTION
4+
The Twilio REST SDK simplifies the process of makes calls to the Twilio REST.
5+
The Twilio REST API lets to you initiate outgoing calls, list previous call,
6+
and much more. See http://www.twilio.com/docs for more information.
7+
8+
### USAGE
9+
To use the Twilio library, just 'import
10+
twilio' in the your current py file. As shown in example-rest.py, you will need to specify the ACCOUNT_ID and ACCOUNT_TOKEN given to you by Twilio before you can make REST requests. In
11+
addition, you will need to choose a 'Caller' and 'Called' before making
12+
outgoing calls. See http://www.twilio.com/docs for more information.
13+
14+
### FILES
15+
* **twilio.py**: include this library in your code
16+
* **example-rest.py**: example usage of REST
17+
* **example-twiml.py**: example usage of the TwiML generator
18+
* **example-utils.py**: example usage of utilities
19+
20+
### LICENSE
21+
The Twilio Python Helper Library is distributed under the MIT License

example-rest.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/env python
2+
3+
import twilio
4+
5+
# Twilio REST API version
6+
API_VERSION = '2008-08-01'
7+
8+
# Twilio AccountSid and AuthToken
9+
ACCOUNT_SID = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
10+
ACCOUNT_TOKEN = 'YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY'
11+
12+
# Outgoing Caller ID previously validated with Twilio
13+
CALLER_ID = 'NNNNNNNNNN';
14+
15+
# Create a Twilio REST account object using your Twilio account ID and token
16+
account = twilio.Account(ACCOUNT_SID, ACCOUNT_TOKEN)
17+
18+
# ===========================================================================
19+
# 1. Initiate a new outbound call to 415-555-1212
20+
# uses a HTTP POST
21+
d = {
22+
'Caller' : CALLER_ID,
23+
'Called' : '415-555-1212',
24+
'Url' : 'http://demo.twilio.com/welcome',
25+
}
26+
print account.request('/%s/Accounts/%s/Calls' % \
27+
(API_VERSION, ACCOUNT_SID), 'POST', d)
28+
29+
# ===========================================================================
30+
# 2. Get a list of recent completed calls (i.e. Status = 2)
31+
# uses a HTTP GET
32+
d = { 'Status':2, }
33+
print account.request('/%s/Accounts/%s/Calls' % \
34+
(API_VERSION, ACCOUNT_SID), 'GET', d)
35+
36+
# ===========================================================================
37+
# 3. Get a list of recent notification log entries
38+
# uses a HTTP GET
39+
print account.request('/%s/Accounts/%s/Notifications' % \
40+
(API_VERSION, ACCOUNT_SID), 'GET')
41+
42+
# ===========================================================================
43+
# 4. Get a list of audio recordings for a certain call
44+
# uses a HTTP GET
45+
d = { 'CallSid':'CA0c7001f3f3f5063b7f7d96def0f1ed00', }
46+
print account.request('/%s/Accounts/%s/Recordings' % \
47+
(API_VERSION, ACCOUNT_SID), 'GET', d)
48+
49+
# ===========================================================================
50+
# 5. Delete a specific recording
51+
# uses a HTTP DELETE, no response is returned when using DELETE
52+
account.request( \
53+
'/%s/Accounts/%s/Recordings/RE4e75a0b62a5c52e5cb96dc25fb4101d9' % \
54+
(API_VERSION, ACCOUNT_SID), 'DELETE')

example-twiml.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#!/usr/bin/env python
2+
"""
3+
The TwiML Python Response Library makes it easy to write TwiML without having
4+
to touch XML. Error checking is built in to help preventing invalid markup.
5+
6+
USAGE:
7+
To create TwiML, you will make new TwiML verbs and nest them inside another
8+
TwiML verb. Convenience methods are provided to simplify TwiML creation.
9+
10+
SUPPORTED VERBS:
11+
Response
12+
Say
13+
Play
14+
Dial
15+
Gather
16+
Hangup
17+
Redirect
18+
Record
19+
Pause
20+
Number
21+
"""
22+
23+
import twilio
24+
25+
# ===========================================================================
26+
# Using Say, Dial, and Play
27+
r = twilio.Response()
28+
r.append(twilio.Say("Hello World", voice=twilio.Say.MAN,
29+
language=twilio.Say.FRENCH, loop=10))
30+
r.append(twilio.Dial("4155551212", timeLimit=45))
31+
r.append(twilio.Play("http://www.mp3.com"))
32+
print r
33+
34+
""" outputs:
35+
<Response>
36+
<Say voice="man" language="fr" loop="10">Hello World</Say>
37+
<Dial timeLimit="45">4155551212</Dial>
38+
<Play>http://www.mp3.com</Play>
39+
</Response>
40+
"""
41+
42+
# The same XML can be created above using the convenience methods
43+
r = twilio.Response()
44+
r.addSay("Hello World", voice=twilio.Say.MAN, language=twilio.Say.FRENCH,
45+
loop=10)
46+
r.addDial("4155551212", timeLimit=45)
47+
r.addPlay("http://www.mp3.com")
48+
print r
49+
50+
51+
# ===========================================================================
52+
# Using Gather, Redirect
53+
r = twilio.Response()
54+
g = r.append(twilio.Gather(numDigits=1))
55+
g.append(twilio.Say("Press 1"))
56+
r.append(twilio.Redirect())
57+
print r
58+
59+
""" outputs:
60+
<Response>
61+
<Gather numDigits="1">
62+
<Say>Press 1</Say>
63+
</Gather>
64+
<Redirect/>
65+
</Response>
66+
"""
67+
68+
# ===========================================================================
69+
# Adding a Say verb multiple times
70+
r = twilio.Response()
71+
s = twilio.Say("Press 1")
72+
r.append(s)
73+
r.append(s)
74+
print r
75+
76+
""" outputs:
77+
<Response>
78+
<Say>Press 1</Say>
79+
<Say>Press 1</Say>
80+
</Response>
81+
"""
82+
83+
# ===========================================================================
84+
# You may want to add an attribute to a verb that the library doesn't support.
85+
# To set arbitrary attribute / value pairs, just include the new attribute
86+
# as a named parameter
87+
r = twilio.Response()
88+
r.append(twilio.Redirect(crazy="delicious"))
89+
print r
90+
91+
""" outputs:
92+
<Response>
93+
<Redirect crazy="delicious"/>
94+
</Response>
95+
"""

example-utils.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env python
2+
3+
import twilio
4+
5+
# Twilio AccountSid and AuthToken
6+
ACCOUNT_SID = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
7+
ACCOUNT_TOKEN = 'YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY'
8+
9+
# Create a Twilio utility object using your Twilio account ID and token
10+
utils = twilio.Utils(ACCOUNT_SID, ACCOUNT_TOKEN)
11+
12+
# ===========================================================================
13+
# 1. Validate a Twilio request
14+
15+
# the URL and POST parameters would typically be provided by the web
16+
# framework that is recieving the request from Twilio (e.g. Django)
17+
url = "http://UUUUUUUUUUUUUUUUUU"
18+
postvars = {}
19+
20+
# the request from Twilio also includes the HTTP header: X-Twilio-Signature
21+
# containing the expected signature
22+
signature = "SSSSSSSSSSSSSSSSSSSSSSSSSSSS"
23+
24+
print "The request from Twilio to %s with the POST parameters %s " % \
25+
(url, postvars)
26+
if utils.validateRequest(url, postVar, signature):
27+
print "was confirmed to have come from Twilio."
28+
else:
29+
print "was NOT VALID. It might have been spoofed!"

0 commit comments

Comments
 (0)
0