Skip to content

Repository files navigation

PyPI version (tropo-webapi-python)GitHub license

Tropo Python Module

tropo - The TropoPython module. This module implements a set of classes and methods for manipulating the Web API for the Tropo cloud communications service at http://www.tropo.com/

For more information about the Tropo Web API that is used with this module, please see:

http://www.tropo.com/docs/webapi/

As this python module is still in development, please report any bugs or issues by raising an issue here:

http://github.com/tropo/tropo-webapi-python/issues

File

http://github.com/tropo/tropo-webapi-python/blob/master/tropo.py

Description

Usage:

fromtropoimportTropotropo=Tropo()
tropo.say("Hello, World")
json=tropo.RenderJson() 

You can write this JSON back to standard output to get Tropo to perform the action. For example, on Google Appengine you might write something like:

handler.response.out.write(json)

Much of the time, a you will interact with Tropo by examining the Result object and communicating back to Tropo via the Tropo class methods, such as "say". In some cases, you'll want to build a class object directly such as in :

choices=tropo.Choices("[5 digits]").objtropo.ask(choices, say="Please enter your 5 digit zip code.", attempts=3, bargein=True, name="zip", timeout=5, voice="dave")
...

Note on Python versions

This module is for python 2.x and requires python 2.5 or higher.
There is a separate version available for python 3.x at:

 https://github.com/tropo/tropo-webapi-python/tree/python3

License

MIT, see LICENSE.txt

Tests

Run testsuite by issuing:

cd test
python test.py

Classes

AskCallChoicesConferenceHangupMessageOnRecordRedirectRejectResultSaySessionStartRecordingStopRecordingTransferTropounittest.TestCase(__builtin__.object)
TestTropoPythonclassAsk|Classrepresentingthe"ask"Tropoaction. Buildsan"ask"JSONobject.
|Classconstructorarg: choices, aChoicesobject|Conveniencefunction: Tropo.ask()
|Classconstructoroptions: attempts, bargein, choices, minConfidence, name, recognizer, required, say, timeout, voice||Requestinformationfromthecallerandwaitforaresponse.
| (Seehttps://www.tropo.com/docs/webapi/ask.htm)
|| { "ask": {
|"attempts": Integer,
|"bargein": Boolean,
|"choices": Object, #Required|"minConfidence": Integer,
|"name": String,
|"recognizer": String,
|"required": Boolean,
|"say": Object,
|"timeout": Float,
|"voice": String } }
||Methodsdefinedhere:
||__init__(self, choices, **options)
classCall|Classrepresentingthe"call"Tropoaction. Buildsa"call"JSONobject.
|Classconstructorarg: to, aString|Classconstructoroptions: answerOnMedia, channel, _from, headers, name, network, recording, required, timeout|Conveniencefunction: Tropo.call()
|| (Seehttps://www.tropo.com/docs/webapi/call.htm)
|| { "call": {
|"to": StringorArray,#Required|"answerOnMedia": Boolean,
|"channel": string,
|"from": string,
|"headers": Object,
|"name": String,
|"network": String,
|"recording": ArrayorObject,
|"required": Boolean,
|"timeout": Float } }
||Methodsdefinedhere:
||__init__(self, to, **options)
classChoices|Classrepresentingchoicemadebyauser. Buildsa"choices"JSONobject.
|Classconstructoroptions: terminator, mode|| (Seehttps://www.tropo.com/docs/webapi/ask.htm)
||Methodsdefinedhere:
||__init__(self, value, **options)
classConference|Classrepresentingthe"conference"Tropoaction. Buildsa"conference"JSONobject.
|Classconstructorarg: id, aString|Conveniencefunction: Tropo.conference()
|Classconstructoroptions: mute, name, playTones, required, terminator|| (Seehttps://www.tropo.com/docs/webapi/conference.htm)
|| { "conference": {
|"id": String,#Required|"mute": Boolean,
|"name": String,
|"playTones": Boolean,
|"required": Boolean,
|"terminator": String } }
||Methodsdefinedhere:
||__init__(self, id, **options)
classHangup|Classrepresentingthe"hangup"Tropoaction. Buildsa"hangup"JSONobject.
|Classconstructorarg: |Classconstructoroptions: |Conveniencefunction: Tropo.hangup()
|| (Seehttps://www.tropo.com/docs/webapi/hangup.htm)
|| { "hangup": { } }
||Methodsdefinedhere:
||__init__(self)
classMessage|Classrepresentingthe"message"Tropoaction. Buildsa"message"JSONobject.
|Classconstructorarg: say_obj, aSayobject|Classconstructorarg: to, aString|Classconstructoroptions: answerOnMedia, channel, _from, name, network, required, timeout, voice|Conveniencefunction: Tropo.message()
|| (Seehttps://www.tropo.com/docs/webapi/message.htm)
| { "message": {
|"say": Object,#Required|"to": StringorArray,#Required|"answerOnMedia": Boolean,
|"channel": string,
|"from": Object,
|"name": String,
|"network": String,
|"required": Boolean,
|"timeout": Float,
|"voice": String } }
||Methodsdefinedhere:
||__init__(self, say_obj, to, **options)
classOn|Classrepresentingthe"on"Tropoaction. Buildsan"on"JSONobject.
|Classconstructorarg: event, aString|Classconstructoroptions: name,next,required,say|Conveniencefunction: Tropo.on()
|| (Seehttps://www.tropo.com/docs/webapi/on.htm)
|| { "on": {
|"event": String,#Required|"name": String,
|"next": String,
|"required": Boolean,
|"say": Object } }
||Methodsdefinedhere:
||__init__(self, event, **options)
classRecord|Classrepresentingthe"record"Tropoaction. Buildsa"record"JSONobject.
|Classconstructorarg: |Classconstructoroptions: attempts, bargein, beep, choices, format, maxSilence, maxTime, method, minConfidence, name, password, required, say, timeout, transcription, url, username|Conveniencefunction: Tropo.record()
|||| (Seehttps://www.tropo.com/docs/webapi/record.htm)
|| { "record": {
|"attempts": Integer,
|"bargein": Boolean,
|"beep": Boolean,
|"choices": Object,
|"format": String,
|"maxSilence": Float,
|"maxTime": Float,
|"method": String,
|"minConfidence": Integer,
|"name": String,
|"password": String,
|"required": Boolean,
|"say": Object,
|"timeout": Float,
|"transcription": ArrayorObject,
|"url": String,#Required ?????|"username": String } }
||Methodsdefinedhere:
||__init__(self, **options)
classRedirect|Classrepresentingthe"redirect"Tropoaction. Buildsa"redirect"JSONobject.
|Classconstructorarg: to, aString|Classconstructoroptions: name, required|Conveniencefunction: Tropo.redirect()
|| (Seehttps://www.tropo.com/docs/webapi/redirect.htm)
|| { "redirect": {
|"to": Object,#Required|"name": String,
|"required": Boolean } }
||Methodsdefinedhere:
||__init__(self, to, **options)
classReject|Classrepresentingthe"reject"Tropoaction. Buildsa"reject"JSONobject.
|Classconstructorarg: |Classconstructoroptions: |Conveniencefunction: Tropo.reject()
|| (Seehttps://www.tropo.com/docs/webapi/reject.htm)
|| { "reject": { } }
||Methodsdefinedhere:
||__init__(self)
classResult|ReturnedanytimearequestismadetotheTropoWebAPI. |Method: getValue| (Seehttps://www.tropo.com/docs/webapi/result.htm)
|| { "result": {
|"actions": ArrayorObject,
|"complete": Boolean,
|"error": String,
|"sequence": Integer,
|"sessionDuration": Integer,
|"sessionId": String,
|"state": String } }
||Methodsdefinedhere:
||__init__(self, result_json)
||getValue(self)
|GetthevalueofthepreviouslyPOSTedTropoaction.
classSay|Classrepresentingthe"say"Tropoaction. Buildsa"say"JSONobject.
|Classconstructorarg: message, aString, oraListofStrings|Classconstructoroptions: attempts, bargein, choices, minConfidence, name, recognizer, required, say, timeout, voice, _as|Conveniencefunction: Tropo.say()
|| (Seehttps://www.tropo.com/docs/webapi/say.htm)
|| { "say": {
|"as": String,
|"name": String,
|"required": Boolean,
|"value": String#Required| } }
||Methodsdefinedhere:
||__init__(self, message, **options)
classSession|SessionisthepayloadsentasanHTTPPOSTtoyourwebapplicationwhenanewsessionarrives. | (Seehttps://www.tropo.com/docs/webapi/session.htm)
||Because'from'isareservedwordinPython, thesessionobject's 'from' propertyiscalled|fromaddressinthePythonlibrary||Methodsdefinedhere:
||__init__(self, session_json)
classStartRecording|Classrepresentingthe"startRecording"Tropoaction. Buildsa"startRecording"JSONobject.
|Classconstructorarg: url, aString|Classconstructoroptions: format, method, username, password|Conveniencefunction: Tropo.startRecording()
|| (Seehttps://www.tropo.com/docs/webapi/startrecording.htm)
|| { "startRecording": {
|"format": String,
|"method": String,
|"url": String,#Required|"username": String,
|"password": String } }
||Methodsdefinedhere:
||__init__(self, url, **options)
classStopRecording|Classrepresentingthe"stopRecording"Tropoaction. Buildsa"stopRecording"JSONobject.
|Classconstructorarg:
|Classconstructoroptions:
|Conveniencefunction: Tropo.stopRecording()
|| (Seehttps://www.tropo.com/docs/webapi/stoprecording.htm)
| { "stopRecording": { } }
||Methodsdefinedhere:
||__init__(self)
classTestTropoPython(unittest.TestCase)
|ClassimplementingasetofunittestsforTropoPython.
||Methodresolutionorder:
|TestTropoPython|unittest.TestCase|__builtin__.object||Methodsdefinedhere:
||test_ask(self)
|Testthe"ask"Tropoclassmethod.
||test_call(self)
|Testthe"call"Tropoclassmethod.
||test_conference(self)
|Testthe"conference"Tropoclassmethod.
||test_hangup(self)
|Testthe"hangup"Tropoclassmethod.
||test_list_say(self)
|Testthe"say"Tropoclassmethod, whenalistofStringsispassedtoit.
||test_message(self)
|Testthe"message"Tropoclassmethod.
||test_on(self)
|Testthe"on"Tropoclassmethod.
||test_record(self)
|Testthe"record"Tropoclassmethod.
||test_redirect(self)
|Testthe"redirect"Tropoclassmethod.
||test_reject(self)
|Testthe"reject"Tropoclassmethod.
||test_say(self)
|Testthe"say"Tropoclassmethod.
||test_startRecording(self)
|Testthe"startRecording"Tropoclassmethod.
||test_stopRecording(self)
|Testthe"stopRecording"Tropoclassmethod.
||test_transfer(self)
|Testthe"transfer"Tropoclassmethod.
||----------------------------------------------------------------------|Dataandotherattributesdefinedhere:
||ID='foo'||MY_PHONE='6021234567'||RECORDING_URL='/receive_recording.py'||S3_URL='http://s3.amazonaws.com/xxx_s3_bucket/hello.wav'||TO='8005551212'||----------------------------------------------------------------------|Methodsinheritedfromunittest.TestCase:
||__call__(self, *args, **kwds)
||__init__(self, methodName='runTest')
|Createaninstanceoftheclassthatwillusethenamedtest|methodwhenexecuted. RaisesaValueErroriftheinstancedoes|nothaveamethodwiththespecifiedname.
||__repr__(self)
||__str__(self)
||assertAlmostEqual=failUnlessAlmostEqual(self, first, second, places=7, msg=None)
|Failifthetwoobjectsareunequalasdeterminedbytheir|differenceroundedtothegivennumberofdecimalplaces| (default7) andcomparingtozero.
||Notethatdecimalplaces (fromzero) areusuallynotthesame|assignificantdigits (measuredfromthemostsignficantdigit).
||assertAlmostEquals=failUnlessAlmostEqual(self, first, second, places=7, msg=None)
|Failifthetwoobjectsareunequalasdeterminedbytheir|differenceroundedtothegivennumberofdecimalplaces| (default7) andcomparingtozero.
||Notethatdecimalplaces (fromzero) areusuallynotthesame|assignificantdigits (measuredfromthemostsignficantdigit).
||assertEqual=failUnlessEqual(self, first, second, msg=None)
|Failifthetwoobjectsareunequalasdeterminedbythe'=='|operator.
||assertEquals=failUnlessEqual(self, first, second, msg=None)
|Failifthetwoobjectsareunequalasdeterminedbythe'=='|operator.
||assertFalse=failIf(self, expr, msg=None)
|Failthetestiftheexpressionistrue.
||assertNotAlmostEqual=failIfAlmostEqual(self, first, second, places=7, msg=None)
|Failifthetwoobjectsareequalasdeterminedbytheir|differenceroundedtothegivennumberofdecimalplaces| (default7) andcomparingtozero.
||Notethatdecimalplaces (fromzero) areusuallynotthesame|assignificantdigits (measuredfromthemostsignficantdigit).
||assertNotAlmostEquals=failIfAlmostEqual(self, first, second, places=7, msg=None)
|Failifthetwoobjectsareequalasdeterminedbytheir|differenceroundedtothegivennumberofdecimalplaces| (default7) andcomparingtozero.
||Notethatdecimalplaces (fromzero) areusuallynotthesame|assignificantdigits (measuredfromthemostsignficantdigit).
||assertNotEqual=failIfEqual(self, first, second, msg=None)
|Failifthetwoobjectsareequalasdeterminedbythe'=='|operator.
||assertNotEquals=failIfEqual(self, first, second, msg=None)
|Failifthetwoobjectsareequalasdeterminedbythe'=='|operator.
||assertRaises=failUnlessRaises(self, excClass, callableObj, *args, **kwargs)
|FailunlessanexceptionofclassexcClassisthrown|bycallableObjwheninvokedwithargumentsargsandkeyword|argumentskwargs. Ifadifferent type ofexceptionis|thrown, itwillnotbecaught, andthetestcasewillbe|deemedtohavesufferedanerror, exactlyasforan|unexpectedexception.
||assertTrue=failUnless(self, expr, msg=None)
|Failthetestunlesstheexpressionistrue.
||assert_=failUnless(self, expr, msg=None)
|Failthetestunlesstheexpressionistrue.
||countTestCases(self)
||debug(self)
|RunthetestwithoutcollectingerrorsinaTestResult||defaultTestResult(self)
||fail(self, msg=None)
|Failimmediately, withthegivenmessage.
||failIf(self, expr, msg=None)
|Failthetestiftheexpressionistrue.
||failIfAlmostEqual(self, first, second, places=7, msg=None)
|Failifthetwoobjectsareequalasdeterminedbytheir|differenceroundedtothegivennumberofdecimalplaces| (default7) andcomparingtozero.
||Notethatdecimalplaces (fromzero) areusuallynotthesame|assignificantdigits (measuredfromthemostsignficantdigit).
||failIfEqual(self, first, second, msg=None)
|Failifthetwoobjectsareequalasdeterminedbythe'=='|operator.
||failUnless(self, expr, msg=None)
|Failthetestunlesstheexpressionistrue.
||failUnlessAlmostEqual(self, first, second, places=7, msg=None)
|Failifthetwoobjectsareunequalasdeterminedbytheir|differenceroundedtothegivennumberofdecimalplaces| (default7) andcomparingtozero.
||Notethatdecimalplaces (fromzero) areusuallynotthesame|assignificantdigits (measuredfromthemostsignficantdigit).
||failUnlessEqual(self, first, second, msg=None)
|Failifthetwoobjectsareunequalasdeterminedbythe'=='|operator.
||failUnlessRaises(self, excClass, callableObj, *args, **kwargs)
|FailunlessanexceptionofclassexcClassisthrown|bycallableObjwheninvokedwithargumentsargsandkeyword|argumentskwargs. Ifadifferent type ofexceptionis|thrown, itwillnotbecaught, andthetestcasewillbe|deemedtohavesufferedanerror, exactlyasforan|unexpectedexception.
||id(self)
||run(self, result=None)
||setUp(self)
|Hookmethodforsettingupthetestfixturebeforeexercisingit.
||shortDescription(self)
|Returnsaone-linedescriptionofthetest, orNoneifno|descriptionhasbeenprovided.
||Thedefaultimplementationofthismethodreturnsthefirstlineof|thespecifiedtestmethod'sdocstring.
||tearDown(self)
|Hookmethodfordeconstructingthetestfixtureaftertestingit.
||----------------------------------------------------------------------|Datadescriptorsinheritedfromunittest.TestCase:
||__dict__|dictionaryforinstancevariables (ifdefined)
||__weakref__|listofweakreferencestotheobject (ifdefined)
||----------------------------------------------------------------------|Dataandotherattributesinheritedfromunittest.TestCase:
||failureException=<type 'exceptions.AssertionError'>|Assertionfailed.
classTransfer|Classrepresentingthe"transfer"Tropoaction. Buildsa"transfer"JSONobject.
|Classconstructorarg: to, aString, orList|Classconstructoroptions: answerOnMedia, choices, _from, name, required, terminator|Conveniencefunction: Tropo.transfer()
|| (Seehttps://www.tropo.com/docs/webapi/transfer.htm)
| { "transfer": {
|"to": StringorArray,#Required|"answerOnMedia": Boolean,
|"choices": Object,
|"from": Object,
|"name": String,
|"required": Boolean,
|"terminator": String,
|"timeout": Float } }
||Methodsdefinedhere:
||__init__(self, to, **options)
classTropo|ThisisthetoplevelclassforalltheTropowebapiactions.
|ThemethodsofthisclassimplementindividualTropoactions.
|Individualactionsareeachmethodsonthisclass.
||Eachmethodtakesoneormorerequiredarguments, followedbyoptional|argumentsexpressedaskey=valuepairs.
||Theoptionalargumentsforthesemethodsaredescribedhere:
|https://www.tropo.com/docs/webapi/||Methodsdefinedhere:
||PrettyJson(self)
|RenderaTropoobjectintoaJsonstring, pretty-printedwithindents.
||RenderJson(self)
|RenderaTropoobjectintoaJsonstring.
||__init__(self)
||ask(self, choices, **options)
|Sendsaprompttotheuserandoptionallywaitsforaresponse.
|Arguments: "choices"isaChoicesobject|Seehttps://www.tropo.com/docs/webapi/ask.htm||call(self, to, **options)
|PlacesacallorsendsanIM, Twitter, orSMSmessage. Tostartacall, usetheSessionAPItotellTropotolaunchyourcode. ||Arguments: toisaString.
|Argument: **optionsisasetofoptionalkeywordarguments. Use"_from"insteadof"from".
|Seehttps://www.tropo.com/docs/webapi/call.htm||conference(self, id, **options)
|Thisobjectallowsmultiplelinesinseparatesessionstobeconferencedtogethersothatthepartiesoneachlinecantalktoeachothersimultaneously. |Thisisavoicechannelonlyfeature. |Argument: "id"isaString|Argument: **optionsisasetofoptionalkeywordarguments.
|Seehttps://www.tropo.com/docs/webapi/conference.htm||hangup(self)
|ThismethodinstructsTropoto"hang-up"ordisconnectthesessionassociatedwiththecurrentsession.
|Seehttps://www.tropo.com/docs/webapi/hangup.htm||message(self, say_obj, to, **options)
|Ashortcutmethodtocreateasession, saysomething, andhangup, allinonestep. ThisisparticularlyusefulforsendingoutaquickSMSorIM. ||Argument: "say_obj"isaSayobject|Argument: "to"isaString|Argument: **optionsisasetofoptionalkeywordarguments. Use"_from"insteadof"from".
|Seehttps://www.tropo.com/docs/webapi/message.htm||on(self, event, **options)
|Addsaneventcallbacksothatyourapplicationmaybenotifiedwhenaparticulareventoccurs. |Possibleeventsare: "continue", "error", "incomplete"and"hangup". |Argument: eventisanevent|Argument: **optionsisasetofoptionalkeywordarguments.
|Seehttps://www.tropo.com/docs/webapi/on.htm||record(self, **options)
|Playsaprompt (audiofileortexttospeech) andoptionallywaitsforaresponsefromthecallerthatisrecorded. |Argument: **optionsisasetofoptionalkeywordarguments.
|Seehttps://www.tropo.com/docs/webapi/record.htm||redirect(self, id, **options)
|Forwardsanincomingcalltoanotherdestination/phonenumberbeforeansweringit. |Argument: idisaString|Argument: **optionsisasetofoptionalkeywordarguments.
|Seehttps://www.tropo.com/docs/webapi/redirect.htm||reject(self)
|AllowsTropoapplicationstorejectincomingsessionsbeforetheyareanswered. |Seehttps://www.tropo.com/docs/webapi/reject.htm||say(self, message, **options)
|WhenthecurrentsessionisavoicechannelthiskeywilleitherplayamessageoranaudiofilefromaURL. |InthecaseofantextchannelitwillsendthetextbacktotheuserviainstantmessagingorSMS. |Argument: messageisastring|Argument: **optionsisasetofoptionalkeywordarguments. Use"_as"insteadof"as".
|Seehttps://www.tropo.com/docs/webapi/say.htm||startRecording(self, url, **options)
|AllowsTropoapplicationstobeginrecordingthecurrentsession. |Argument: urlisastring|Argument: **optionsisasetofoptionalkeywordarguments.
|Seehttps://www.tropo.com/docs/webapi/startrecording.htm||stopRecording(self)
|Stopsapreviouslystartedrecording.
|Seehttps://www.tropo.com/docs/webapi/stoprecording.htm||transfer(self, to, **options)
|Transfersanalreadyansweredcalltoanotherdestination/phonenumber. |Argument: toisastring|Argument: **optionsisasetofoptionalkeywordarguments. Use"_from"insteadof"from".
|Seehttps://www.tropo.com/docs/webapi/transfer.htm

About

A set of Python classes for working with the Tropo WebAPI

Resources

Stars

85 stars

Watchers

15 watching

Forks

Releases

Packages

Used by

Contributors

Languages