Skip to content

Commit 6894f95

Browse files
authored
Merge pull request #43 from stefan-messagebird/DEVP-12_add_create_voice_call
DEVP-12 :: Add method for listing, creating, and updating calls
2 parents 4be2dd3 + 540e549 commit 6894f95

12 files changed

+510
-36
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,6 @@ target/
5959

6060
# PyCharm
6161
.idea/
62+
63+
# pyvenv
64+
venv/

examples/call.py

+32-30
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,48 @@
11
#!/usr/bin/env python
22

3-
import sys, os
3+
import os
4+
import sys
5+
46
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
57

68
import messagebird
79

8-
#ACCESS_KEY = ''
9-
#CALL_ID = ''
10+
# ACCESS_KEY = ''
11+
# CALL_ID = ''
1012

1113
try:
12-
ACCESS_KEY
14+
ACCESS_KEY
1315
except NameError:
14-
print('You need to set an ACCESS_KEY constant in this file')
15-
sys.exit(1)
16+
print('You need to set an ACCESS_KEY constant in this file')
17+
sys.exit(1)
1618

1719
try:
18-
CALL_ID
20+
CALL_ID
1921
except NameError:
20-
print('You need to set a CALL_ID constant in this file')
21-
sys.exit(1)
22+
print('You need to set a CALL_ID constant in this file')
23+
sys.exit(1)
2224

2325
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-
call = client.call(CALL_ID)
29-
30-
# Print the object information.
31-
print('\nThe following information was returned as a Message object:\n')
32-
print(' id : %s' % call.data.id)
33-
print(' status : %s' % call.data.status)
34-
print(' source : %s' % call.data.source)
35-
print(' destination : %s' % call.data.destination)
36-
print(' createdAt : %s' % call.data.createdAt)
37-
print(' updatedAt : %s' % call.data.updatedAt)
38-
print(' endedAt : %s' % call.data.endedAt)
39-
26+
# Create a MessageBird client with the specified ACCESS_KEY.
27+
client = messagebird.Client(ACCESS_KEY)
28+
29+
# Fetch the Call object for the specified CALL_ID.
30+
call = client.call(CALL_ID)
31+
32+
# Print the object information.
33+
print('\nThe following information was returned as a', str(call.__class__), 'object:\n')
34+
print(' id : %s' % call.data.id)
35+
print(' status : %s' % call.data.status)
36+
print(' source : %s' % call.data.source)
37+
print(' destination : %s' % call.data.destination)
38+
print(' createdAt : %s' % call.data.createdAt)
39+
print(' updatedAt : %s' % call.data.updatedAt)
40+
print(' endedAt : %s' % call.data.endedAt)
41+
4042
except messagebird.client.ErrorException as e:
41-
print('\nAn error occured while requesting a Message object:\n')
43+
print('\nAn error occurred while requesting a Message object:\n')
4244

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)
45+
for error in e.errors:
46+
print(' code : %d' % error.code)
47+
print(' description : %s' % error.description)
48+
print(' parameter : %s\n' % error.parameter)

examples/call_create.py

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#!/usr/bin/env python
2+
import os
3+
import sys
4+
import json
5+
import argparse
6+
import requests
7+
8+
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
9+
import messagebird
10+
11+
exampleCallFlow = '{"title":"Test Flow","steps":[{"action":"say","options":{"payload":"Hey, this is your first voice \
12+
call","language":"en-GB","voice":"female"}}]}'
13+
14+
parser = argparse.ArgumentParser(usage='call_create.py\
15+
--accessKey="*******" \
16+
--destination=31612345678 \
17+
--source=31644556677 \
18+
--callFlow \'' + exampleCallFlow + '\'\
19+
')
20+
parser.add_argument('--accessKey', help='access key for MessageBird API', type=str, required=True)
21+
parser.add_argument('--source', help='The caller ID of the call.', type=str, required=True)
22+
parser.add_argument('--destination', help='The number/address to be called.', type=str, required=True)
23+
parser.add_argument('--callFlow', help='The call flow object to be executed when the call is answered.', type=str, required=False, default=exampleCallFlow)
24+
parser.add_argument('--webhook', help='The webhook object containing the url & required token.', type=str, required=False, default='{}')
25+
args = vars(parser.parse_args())
26+
27+
# arguments to parse as json
28+
jsonArgs = ['callFlow', 'webhook']
29+
30+
for jsonArg in jsonArgs:
31+
try:
32+
args[jsonArg] = json.loads(str(args[jsonArg]).strip('\''))
33+
except json.decoder.JSONDecodeError as e:
34+
parser.print_usage()
35+
print('Invalid json provided for %s: %s' % (jsonArg, e))
36+
print('Provided %s json: %s' % (jsonArg, args[jsonArg]))
37+
exit(1)
38+
39+
try:
40+
# Create a MessageBird client with the specified accessKey.
41+
client = messagebird.Client(args['accessKey'])
42+
del(args['accessKey'])
43+
44+
# Create a call for the specified callID.
45+
call = client.call_create(**args)
46+
47+
# Print the object information.
48+
print('\nThe following information was returned as a', str(call.__class__), 'object:\n')
49+
print(' id : %s' % call.data.id)
50+
print(' status : %s' % call.data.status)
51+
print(' source : %s' % call.data.source)
52+
print(' destination : %s' % call.data.destination)
53+
print(' webhook : %s' % call.data.webhook)
54+
print(' createdAt : %s' % call.data.createdAt)
55+
print(' updatedAt : %s' % call.data.updatedAt)
56+
print(' endedAt : %s' % call.data.endedAt)
57+
58+
except messagebird.client.ErrorException as e:
59+
print('\nAn error occurred while creating a call:\n')
60+
61+
for error in e.errors:
62+
print(' code : %d' % error.code)
63+
print(' description : %s' % error.description)
64+
print(' parameter : %s' % error.parameter)
65+
print(' type : %s' % error.__class__)
66+
67+
except requests.exceptions.HTTPError as e:
68+
print('\nAn http exception occurred while creating a call:')
69+
print(' ', e)
70+
print(' Http request body: ', e.request.body)
71+
print(' Http response status: ', e.response.status_code)
72+
print(' Http response body: ', e.response.content.decode())
73+
74+
except Exception as e:
75+
print('\nAn ', e.__class__, ' exception occurred while creating a call:')
76+
print(e)
77+

examples/call_delete.py

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/usr/bin/env python
2+
import os
3+
import sys
4+
import json
5+
import argparse
6+
import requests
7+
8+
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
9+
import messagebird
10+
11+
parser = argparse.ArgumentParser(usage='call_create.py\
12+
--accessKey="*******" \
13+
--callId=dda20377-72da-4846-9b2c-0fea3ad4bcb6 \
14+
')
15+
parser.add_argument('--accessKey', help='Access key for MessageBird API.', type=str, required=True)
16+
parser.add_argument('--callId', help='The ID of the MessageBird call to delete.', type=str, required=True)
17+
args = vars(parser.parse_args())
18+
19+
try:
20+
# Create a MessageBird client with the specified accessKey.
21+
client = messagebird.Client(args['accessKey'])
22+
23+
# Create a call for the specified callID.
24+
call = client.call_delete(args['callId'])
25+
26+
# If no error is thrown, means delete was successful.
27+
print('\nDeleted call with id `%s` successfully!' % args['callId'])
28+
29+
except messagebird.client.ErrorException as e:
30+
print('\nAn error occurred while creating a call:\n')
31+
32+
for error in e.errors:
33+
print(' code : %d' % error.code)
34+
print(' description : %s' % error.description)
35+
print(' parameter : %s' % error.parameter)
36+
print(' type : %s' % error.__class__)
37+
38+
except requests.exceptions.HTTPError as e:
39+
print('\nAn http exception occurred while deleting a call:')
40+
print(' ', e)
41+
print(' Http request body: ', e.request.body)
42+
print(' Http response status: ', e.response.status_code)
43+
print(' Http response body: ', e.response.content.decode())
44+
45+
except Exception as e:
46+
print('\nAn ', e.__class__, ' exception occurred while deleting a call:')
47+
print(e)
48+

examples/call_list.py

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/usr/bin/env python
2+
import os
3+
import sys
4+
import argparse
5+
import requests
6+
7+
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
8+
import messagebird
9+
10+
11+
parser = argparse.ArgumentParser(usage='call_create.py\
12+
--accessKey="*******" \
13+
--page=1 \
14+
')
15+
parser.add_argument('--accessKey', help='Access key for MessageBird API.', type=str, required=True)
16+
parser.add_argument('--page', help='The page you wish to view.', type=str, required=False, default=1)
17+
args = vars(parser.parse_args())
18+
19+
try:
20+
# Create a MessageBird client with the specified accessKey.
21+
client = messagebird.Client(args['accessKey'])
22+
del(args['accessKey'])
23+
24+
# Create a call for the specified callID.
25+
callList = client.call_list(**args)
26+
27+
# Print the object information.
28+
print('\nThe following information was returned as a %s object:\n' % callList.__class__)
29+
if callList.items is not None:
30+
print(' Containing the the following items:')
31+
for item in callList.items:
32+
print(' {')
33+
print(' id : %s' % item.id)
34+
print(' status : %s' % item.status)
35+
print(' source : %s' % item.source)
36+
print(' destination : %s' % item.destination)
37+
print(' createdAt : %s' % item.createdAt)
38+
print(' updatedAt : %s' % item.updatedAt)
39+
print(' endedAt : %s' % item.endedAt)
40+
print(' },')
41+
else:
42+
print(' With an empty response.')
43+
44+
except messagebird.client.ErrorException as e:
45+
print('\nAn error occurred while listing calls:\n')
46+
47+
for error in e.errors:
48+
print(' code : %d' % error.code)
49+
print(' description : %s' % error.description)
50+
print(' parameter : %s' % error.parameter)
51+
print(' type : %s' % error.__class__)
52+
53+
except requests.exceptions.HTTPError as e:
54+
print('\nAn HTTP exception occurred while listing calls:')
55+
print(' ', e)
56+
print(' Http request body: ', e.request.body)
57+
print(' Http response status: ', e.response.status_code)
58+
print(' Http response body: ', e.response.content.decode())
59+
60+
except Exception as e:
61+
print('\nAn ', e.__class__, ' exception occurred while creating a call:')
62+
print(e)
63+
64+

messagebird/base_list.py

+4
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,7 @@ def items(self, value):
4343
items.append(self.itemType().load(item))
4444

4545
self._items = items
46+
47+
def __str__(self):
48+
items_count = 0 if self.items is None else len(self.items)
49+
return "%s with %d items.\n" % (str(self.__class__), items_count)

messagebird/call.py

+6
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,9 @@ def data(self):
1919
@data.setter
2020
def data(self, value):
2121
self._data = CallData().load(value[0])
22+
23+
def __str__(self):
24+
return "\n".join([
25+
'id : %s' % self.id,
26+
'data.'+'data.'.join(str(self._data).splitlines(True)),
27+
])

messagebird/call_data.py

+12
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,15 @@ def webhook(self):
4545
@webhook.setter
4646
def webhook(self, value):
4747
self._webhook = Webhook.load(value)
48+
49+
def __str__(self):
50+
return "\n".join([
51+
'id : %s' % self.id,
52+
'status : %s' % self.status,
53+
'source : %s' % self.source,
54+
'destination : %s' % self.destination,
55+
'webhook : %s' % self.webhook,
56+
'updatedAt : %s' % self.updatedAt,
57+
'createdAt : %s' % self.createdAt,
58+
'endedAt : %s' % self.endedAt,
59+
])

messagebird/call_list.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from messagebird.base_list import BaseList
2+
from messagebird.call_data import CallData
3+
4+
5+
class CallList(BaseList):
6+
def __init__(self):
7+
# We're expecting items of type CallData
8+
super(CallList, self).__init__(CallData)
9+
self.perPage = None
10+
self.currentPage = None
11+
self.pageCount = None
12+
self._pagination = None
13+
14+
@property
15+
def data(self):
16+
return self.items
17+
18+
@property
19+
def pagination(self):
20+
return {
21+
"totalCount": self.totalCount,
22+
"pageCount": self.pageCount,
23+
"currentPage": self.currentPage,
24+
"perPage": self.perPage
25+
}
26+
27+
@pagination.setter
28+
def pagination(self, value):
29+
if isinstance(value, dict):
30+
self.totalCount = value['totalCount']
31+
self.pageCount = value['pageCount']
32+
self.currentPage = value['currentPage']
33+
self.perPage = value['perPage']
34+
self.limit = self.perPage * self.currentPage
35+
self.offset = self.perPage * (self.currentPage - 1)
36+
37+
@data.setter
38+
def data(self, value):
39+
if isinstance(value, list):
40+
self.count = len(value)
41+
self.items = value
42+

0 commit comments

Comments
 (0)