The Flowroute Python Library v2 provides methods for interacting with Messages v2 of the Flowroute API.
TopicsRequirements
- Flowroute API credentials
- Python 2.x or higher
Installation
First, start a command shell session and clone the flowroute-messaging-python Github repo.
git clone https://github.com/flowroute/flowroute-messaging-python.git
Switch to the newly-created flowroute-messaging-python directory. The Messaging SDK for Python comes with a requirements file listing the required Python libraries.
pip install -r requirements.txt
Usage
In Flowroute's approach to building the Messaging (v2) API wrapper, HTTP requests are handled by the APIController, which contains the functions used to perform tasks with the Python SDK.
APIController
- create_message() - Send outbound messages from a SMS-enabled Flowroute number
- get_message_lookup() - Retrieve a message detail record (MDR) based on a specific record ID
The following shows how you can import and instantiate the APIController:
#Import the Controllers
from FlowrouteMessagingLib.Models.Message import Message
from FlowrouteMessagingLib.Controllers.APIController import APIController
Credentials
In demo_send.py file, replace API_ACCESS_KEY and API_SECRET_KEY with your API credentials from the Flowroute Manager.
#Pass your API credentials
controller = APIController(username="API_ACCESS_KEY", password="API_SECRET_KEY")
Methods
create_message()
The method accepts the to, from, and body parameters which you can learn more about in the API reference.
#Create and Send a Message
msg = Message(to="15305557784", from_="18444205700", content="Gee, nice marmot!")
response = controller.create_message(msg)
Example Response
{
"data": {
"id": "mdr1-fab29a740129404a8ca794efc1359e12'
}
}
get_message_lookup()
The method accepts the record_id parameter which you can learn more about in the API reference.
# Look up the MDR
response = controller.get_message_lookup("mdr1-fab29a740129404a8ca794efc1359e12")
Example Response
{
"data": {
"attributes": {
"body": "Gee, nice marmot!",
"direction": "outbound",
"amount_nanodollars": 4000000,
"message_encoding": 0,
"timestamp": "2016-05-03T17:41:00.478893+00:00",
"has_mms": false,
"to": "15305557784",
"amount_display": "$0.0040",
"from": "18444205700",
"callback_url": None,
"message_type": "long-code"
},
"type": "message",
"id": "mdr1-cfab29a740129404a8ca794efc1359e12"
}
}
Errors
In cases of method errors, the API Controller returns an error object with the same fields as our API's error response.
Example Error
<FlowrouteMessagingLib.Controllers.APIController.APIController object at 0x10af2fc90>
Error - 401
[{u'detail': u'Authentication failed.',
u'id': u'277d6cfc-0577-4bfb-aaee-2c819a20223c',
u'status': 401,
u'title': u'Unauthorized'}]