Skip to content

Commit fdd600b

Browse files
committed
Initial commit
0 parents  commit fdd600b

20 files changed

+648
-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) 2014, MessageBird
2+
All rights reserved.
3+
4+
Redistribution and use in source and binary forms, with or without
5+
modification, are permitted provided that the following conditions are met:
6+
7+
1. Redistributions of source code must retain the above copyright notice, this
8+
list of conditions and the following disclaimer.
9+
2. Redistributions in binary form must reproduce the above copyright notice,
10+
this list of conditions and the following disclaimer in the documentation
11+
and/or other materials provided with the distribution.
12+
13+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
14+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
17+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

README.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
MessageBird's REST API for Python
2+
=================================
3+
This repository contains the open source Python client for MessageBird's REST API. Documentation can be found at: https://www.messagebird.com/developers/python.
4+
5+
Requirements
6+
------------
7+
- [Sign up](https://www.messagebird.com/en/signup) for a free MessageBird account
8+
- Create a new access key in the developers sections
9+
- An application written in Python (tested with Python 2.7 and Python 3.4)
10+
11+
Installation
12+
------------
13+
The easiest way to use the MessageBird API in your Python project is to install it using the setup.py file:
14+
15+
```
16+
$ sudo python setup.py install
17+
```
18+
19+
Examples
20+
--------
21+
We have put some self-explanatory examples in the [examples](https://github.com/messagebird/python-rest-api/tree/master/examples) directory, but here is a quick example on how to get started. Assuming the installation was successful, you can import the messagebird package like this:
22+
23+
```python
24+
import messagebird
25+
```
26+
27+
Then, create an instance of **messagebird.Client**:
28+
29+
```python
30+
client = messagebird.Client('test_gshuPaZoeEG6ovbc8M79w0QyM')
31+
```
32+
33+
Now you can query the API for information or send a request. For example, if we want to request our balance information you'd do something like this:
34+
35+
```python
36+
try:
37+
# Fetch the Balance object.
38+
balance = client.balance()
39+
40+
# Print the object information.
41+
print('\nThe following information was returned as a Balance object:\n')
42+
print(' amount : %d' % balance.amount)
43+
print(' type : %s' % balance.type)
44+
print(' payment : %s\n' % balance.payment)
45+
46+
except messagebird.client.ErrorException as e:
47+
print('\nAn error occured while requesting a Balance object:\n')
48+
49+
for error in e.errors:
50+
print(' code : %d' % error.code)
51+
print(' description : %s' % error.description)
52+
print(' parameter : %s\n' % error.parameter)
53+
54+
```
55+
56+
This will give you something like:
57+
```shell
58+
$ python example.py
59+
60+
amount : 9
61+
type : credits
62+
payment : prepaid
63+
```
64+
65+
Please see the other examples for a complete overview of all the available API calls.
66+
67+
Documentation
68+
-------------
69+
Complete documentation, instructions, and examples are available at:
70+
[https://www.messagebird.com/developers/python](https://www.messagebird.com/developers/python).
71+
72+
License
73+
-------
74+
The MessageBird REST Client for Python is licensed under [The BSD 2-Clause License](http://opensource.org/licenses/BSD-2-Clause). Copyright (c) 2014, MessageBird

examples/balance.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 sys, os
4+
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
5+
6+
import messagebird
7+
8+
ACCESS_KEY = 'test_gshuPaZoeEG6ovbc8M79w0QyM'
9+
10+
try:
11+
# Create a MessageBird client with the specified ACCESS_KEY.
12+
client = messagebird.Client(ACCESS_KEY)
13+
14+
# Fetch the Balance object.
15+
balance = client.balance()
16+
17+
# Print the object information.
18+
print('\nThe following information was returned as a Balance object:\n')
19+
print(' amount : %d' % balance.amount)
20+
print(' type : %s' % balance.type)
21+
print(' payment : %s\n' % balance.payment)
22+
23+
except messagebird.client.ErrorException as e:
24+
print('\nAn error occured while requesting a Balance object:\n')
25+
26+
for error in e.errors:
27+
print(' code : %d' % error.code)
28+
print(' description : %s' % error.description)
29+
print(' parameter : %s\n' % error.parameter)

examples/hlr.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/usr/bin/env python
2+
3+
import sys, os
4+
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
5+
6+
import messagebird
7+
8+
# ACCESS_KEY = ''
9+
# HLR_ID = ''
10+
11+
try:
12+
ACCESS_KEY
13+
except NameError:
14+
print('You need to set an ACCESS_KEY constant in this file')
15+
sys.exit(1)
16+
17+
try:
18+
HLR_ID
19+
except NameError:
20+
print('You need to set an HLR_ID constant in this file')
21+
sys.exit(1)
22+
23+
try:
24+
# Create a MessageBird client with the specified ACCESS_KEY.
25+
client = messagebird.Client(ACCESS_KEY)
26+
27+
# Fetch the HLR lookup object for the specified HLR_ID.
28+
hlr = client.hlr(HLR_ID)
29+
30+
# Print the object information.
31+
print('\nThe following information was returned as an HLR object:\n')
32+
print(' id : %s' % hlr.id)
33+
print(' href : %s' % hlr.href)
34+
print(' msisdn : %s' % hlr.msisdn)
35+
print(' reference : %s' % hlr.reference)
36+
print(' status : %s' % hlr.status)
37+
print(' createdDatetime : %s' % hlr.createdDatetime)
38+
print(' statusDatetime : %s\n' % hlr.statusDatetime)
39+
40+
except messagebird.client.ErrorException as e:
41+
print('\nAn error occured while requesting an HLR object:\n')
42+
43+
for error in e.errors:
44+
print(' code : %d' % error.code)
45+
print(' description : %s' % error.description)
46+
print(' parameter : %s\n' % error.parameter)

examples/hlr_create.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env python
2+
3+
import sys, os
4+
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
5+
6+
import messagebird
7+
8+
ACCESS_KEY = 'test_gshuPaZoeEG6ovbc8M79w0QyM'
9+
10+
try:
11+
# Create a MessageBird client with the specified ACCESS_KEY.
12+
client = messagebird.Client(ACCESS_KEY)
13+
14+
# Create a new HLR lookup object.
15+
hlr = client.hlr_create('31612345678', 'MyReference')
16+
17+
# Print the object information.
18+
print('\nThe following information was returned as an HLR object:\n')
19+
print(' id : %s' % hlr.id)
20+
print(' href : %s' % hlr.href)
21+
print(' msisdn : %s' % hlr.msisdn)
22+
print(' reference : %s' % hlr.reference)
23+
print(' status : %s' % hlr.status)
24+
print(' createdDatetime : %s' % hlr.createdDatetime)
25+
print(' statusDatetime : %s\n' % hlr.statusDatetime)
26+
27+
except messagebird.client.ErrorException as e:
28+
print('\nAn error occured while requesting an HLR object:\n')
29+
30+
for error in e.errors:
31+
print(' code : %d' % error.code)
32+
print(' description : %s' % error.description)
33+
print(' parameter : %s\n' % error.parameter)

examples/message.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 sys, os
4+
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
5+
6+
import messagebird
7+
8+
# ACCESS_KEY = ''
9+
# MESSAGE_ID = ''
10+
11+
try:
12+
ACCESS_KEY
13+
except NameError:
14+
print('You need to set an ACCESS_KEY constant in this file')
15+
sys.exit(1)
16+
17+
try:
18+
MESSAGE_ID
19+
except NameError:
20+
print('You need to set a MESSAGE_ID constant in this file')
21+
sys.exit(1)
22+
23+
try:
24+
# Create a MessageBird client with the specified ACCESS_KEY.
25+
client = messagebird.Client(ACCESS_KEY)
26+
27+
# Fetch the Message object for the specified MESSAGE_ID.
28+
msg = client.message(MESSAGE_ID)
29+
30+
# Print the object information.
31+
print('\nThe following information was returned as a Message object:\n')
32+
print(' id : %s' % msg.id)
33+
print(' href : %s' % msg.href)
34+
print(' direction : %s' % msg.direction)
35+
print(' type : %s' % msg.type)
36+
print(' originator : %s' % msg.originator)
37+
print(' body : %s' % msg.body)
38+
print(' reference : %s' % msg.reference)
39+
print(' validity : %s' % msg.validity)
40+
print(' gateway : %s' % msg.gateway)
41+
print(' typeDetails : %s' % msg.typeDetails)
42+
print(' datacoding : %s' % msg.datacoding)
43+
print(' mclass : %s' % msg.mclass)
44+
print(' scheduledDatetime : %s' % msg.scheduledDatetime)
45+
print(' createdDatetime : %s' % msg.createdDatetime)
46+
print(' recipients : %s\n' % msg.recipients)
47+
48+
except messagebird.client.ErrorException as e:
49+
print('\nAn error occured while requesting a Message object:\n')
50+
51+
for error in e.errors:
52+
print(' code : %d' % error.code)
53+
print(' description : %s' % error.description)
54+
print(' parameter : %s\n' % error.parameter)

examples/message_create.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/usr/bin/env python
2+
3+
import sys, os
4+
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
5+
6+
import messagebird
7+
8+
ACCESS_KEY = 'test_gshuPaZoeEG6ovbc8M79w0QyM'
9+
10+
try:
11+
# Create a MessageBird client with the specified ACCESS_KEY.
12+
client = messagebird.Client(ACCESS_KEY)
13+
14+
# Send a new message.
15+
msg = client.message_create('FromMe', '31612345678', 'Hello World', { 'reference' : 'Foobar' })
16+
17+
# Print the object information.
18+
print('\nThe following information was returned as a Message object:\n')
19+
print(' id : %s' % msg.id)
20+
print(' href : %s' % msg.href)
21+
print(' direction : %s' % msg.direction)
22+
print(' type : %s' % msg.type)
23+
print(' originator : %s' % msg.originator)
24+
print(' body : %s' % msg.body)
25+
print(' reference : %s' % msg.reference)
26+
print(' validity : %s' % msg.validity)
27+
print(' gateway : %s' % msg.gateway)
28+
print(' typeDetails : %s' % msg.typeDetails)
29+
print(' datacoding : %s' % msg.datacoding)
30+
print(' mclass : %s' % msg.mclass)
31+
print(' scheduledDatetime : %s' % msg.scheduledDatetime)
32+
print(' createdDatetime : %s' % msg.createdDatetime)
33+
print(' recipients : %s\n' % msg.recipients)
34+
print
35+
36+
except messagebird.client.ErrorException as e:
37+
print('\nAn error occured while requesting a Message object:\n')
38+
39+
for error in e.errors:
40+
print(' code : %d' % error.code)
41+
print(' description : %s' % error.description)
42+
print(' parameter : %s\n' % error.parameter)

examples/voice_message.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/usr/bin/env python
2+
3+
import sys, os
4+
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
5+
6+
import messagebird
7+
8+
# ACCESS_KEY = ''
9+
# MESSAGE_ID = ''
10+
11+
try:
12+
ACCESS_KEY
13+
except NameError:
14+
print('You need to set an ACCESS_KEY constant in this file')
15+
sys.exit(1)
16+
17+
try:
18+
MESSAGE_ID
19+
except NameError:
20+
print('You need to set a MESSAGE_ID constant in this file')
21+
sys.exit(1)
22+
23+
try:
24+
# Create a MessageBird client with the specified ACCESS_KEY.
25+
client = messagebird.Client(ACCESS_KEY)
26+
27+
# Fetch the VoiceMessage object for the specified MESSAGE_ID.
28+
vmsg = client.voice_message(MESSAGE_ID)
29+
30+
# Print the object information.
31+
print('\nThe following information was returned as a VoiceMessage object:\n')
32+
print(' id : %s' % vmsg.id)
33+
print(' href : %s' % vmsg.href)
34+
print(' body : %s' % vmsg.body)
35+
print(' reference : %s' % vmsg.reference)
36+
print(' language : %s' % vmsg.language)
37+
print(' voice : %s' % vmsg.voice)
38+
print(' repeat : %s' % vmsg.repeat)
39+
print(' ifMachine : %s' % vmsg.ifMachine)
40+
print(' scheduledDatetime : %s' % vmsg.scheduledDatetime)
41+
print(' createdDatetime : %s' % vmsg.createdDatetime)
42+
print(' recipients : %s\n' % vmsg.recipients)
43+
44+
except messagebird.client.ErrorException as e:
45+
print('\nAn error occured while requesting a VoiceMessage object:\n')
46+
47+
for error in e.errors:
48+
print(' code : %d' % error.code)
49+
print(' description : %s' % error.description)
50+
print(' parameter : %s\n' % error.parameter)

examples/voice_message_create.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env python
2+
3+
import sys, os
4+
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
5+
6+
import messagebird
7+
8+
ACCESS_KEY = 'test_gshuPaZoeEG6ovbc8M79w0QyM'
9+
10+
try:
11+
# Create a MessageBird client with the specified ACCESS_KEY.
12+
client = messagebird.Client(ACCESS_KEY)
13+
14+
# Send a new voice message.
15+
vmsg = client.voice_message_create('31612345678', 'Hello World', { 'reference' : 'Foobar' })
16+
17+
# Print the object information.
18+
print('\nThe following information was returned as a VoiceMessage object:\n')
19+
print(' id : %s' % vmsg.id)
20+
print(' href : %s' % vmsg.href)
21+
print(' body : %s' % vmsg.body)
22+
print(' reference : %s' % vmsg.reference)
23+
print(' language : %s' % vmsg.language)
24+
print(' voice : %s' % vmsg.voice)
25+
print(' repeat : %s' % vmsg.repeat)
26+
print(' ifMachine : %s' % vmsg.ifMachine)
27+
print(' scheduledDatetime : %s' % vmsg.scheduledDatetime)
28+
print(' createdDatetime : %s' % vmsg.createdDatetime)
29+
print(' recipients : %s\n' % vmsg.recipients)
30+
31+
except messagebird.client.ErrorException as e:
32+
print('\nAn error occured while requesting a VoiceMessage object:\n')
33+
34+
for error in e.errors:
35+
print(' code : %d' % error.code)
36+
print(' description : %s' % error.description)
37+
print(' parameter : %s\n' % error.parameter)

messagebird/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from messagebird.client import Client

0 commit comments

Comments
 (0)