Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,6 @@ target/

# PyCharm
.idea/

# pyvenv
venv/
62 changes: 32 additions & 30 deletions examples/call.py
Original file line number Diff line number Diff line change
@@ -1,46 +1,48 @@
#!/usr/bin/env python

import sys, os
import os
import sys

sys.path.append(os.path.join(os.path.dirname(__file__), '..'))

import messagebird

#ACCESS_KEY = ''
#CALL_ID = ''
# ACCESS_KEY = ''
# CALL_ID = ''

try:
ACCESS_KEY
ACCESS_KEY
except NameError:
print('You need to set an ACCESS_KEY constant in this file')
sys.exit(1)
print('You need to set an ACCESS_KEY constant in this file')
sys.exit(1)

try:
CALL_ID
CALL_ID
except NameError:
print('You need to set a CALL_ID constant in this file')
sys.exit(1)
print('You need to set a CALL_ID constant in this file')
sys.exit(1)

try:
# Create a MessageBird client with the specified ACCESS_KEY.
client = messagebird.Client(ACCESS_KEY)

# Fetch the Message object for the specified MESSAGE_ID.
call = client.call(CALL_ID)

# Print the object information.
print('\nThe following information was returned as a Message object:\n')
print(' id : %s' % call.data.id)
print(' status : %s' % call.data.status)
print(' source : %s' % call.data.source)
print(' destination : %s' % call.data.destination)
print(' createdAt : %s' % call.data.createdAt)
print(' updatedAt : %s' % call.data.updatedAt)
print(' endedAt : %s' % call.data.endedAt)
# Create a MessageBird client with the specified ACCESS_KEY.
client = messagebird.Client(ACCESS_KEY)

# Fetch the Call object for the specified CALL_ID.
call = client.call(CALL_ID)

# Print the object information.
print('\nThe following information was returned as a', str(call.__class__), 'object:\n')
print(' id : %s' % call.data.id)
print(' status : %s' % call.data.status)
print(' source : %s' % call.data.source)
print(' destination : %s' % call.data.destination)
print(' createdAt : %s' % call.data.createdAt)
print(' updatedAt : %s' % call.data.updatedAt)
print(' endedAt : %s' % call.data.endedAt)

except messagebird.client.ErrorException as e:
print('\nAn error occured while requesting a Message object:\n')
print('\nAn error occurred while requesting a Message object:\n')

for error in e.errors:
print(' code : %d' % error.code)
print(' description : %s' % error.description)
print(' parameter : %s\n' % error.parameter)
for error in e.errors:
print(' code : %d' % error.code)
print(' description : %s' % error.description)
print(' parameter : %s\n' % error.parameter)
77 changes: 77 additions & 0 deletions examples/call_create.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#!/usr/bin/env python
import os
import sys
import json
import argparse
import requests

sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
import messagebird

exampleCallFlow = '{"title":"Test Flow","steps":[{"action":"say","options":{"payload":"Hey, this is your first voice \
call","language":"en-GB","voice":"female"}}]}'

parser = argparse.ArgumentParser(usage='call_create.py\
--accessKey="*******" \
--destination=31612345678 \
--source=31644556677 \
--callFlow \'' + exampleCallFlow + '\'\
')
parser.add_argument('--accessKey', help='access key for MessageBird API', type=str, required=True)
parser.add_argument('--source', help='The caller ID of the call.', type=str, required=True)
parser.add_argument('--destination', help='The number/address to be called.', type=str, required=True)
parser.add_argument('--callFlow', help='The call flow object to be executed when the call is answered.', type=str, required=False, default=exampleCallFlow)
parser.add_argument('--webhook', help='The webhook object containing the url & required token.', type=str, required=False, default='{}')
args = vars(parser.parse_args())

# arguments to parse as json
jsonArgs = ['callFlow', 'webhook']

for jsonArg in jsonArgs:
try:
args[jsonArg] = json.loads(str(args[jsonArg]).strip('\''))
except json.decoder.JSONDecodeError as e:
parser.print_usage()
print('Invalid json provided for %s: %s' % (jsonArg, e))
print('Provided %s json: %s' % (jsonArg, args[jsonArg]))
exit(1)

try:
# Create a MessageBird client with the specified accessKey.
client = messagebird.Client(args['accessKey'])
del(args['accessKey'])

# Create a call for the specified callID.
call = client.call_create(**args)

# Print the object information.
print('\nThe following information was returned as a', str(call.__class__), 'object:\n')
print(' id : %s' % call.data.id)
print(' status : %s' % call.data.status)
print(' source : %s' % call.data.source)
print(' destination : %s' % call.data.destination)
print(' webhook : %s' % call.data.webhook)
print(' createdAt : %s' % call.data.createdAt)
print(' updatedAt : %s' % call.data.updatedAt)
print(' endedAt : %s' % call.data.endedAt)

except messagebird.client.ErrorException as e:
print('\nAn error occurred while creating a call:\n')

for error in e.errors:
print(' code : %d' % error.code)
print(' description : %s' % error.description)
print(' parameter : %s' % error.parameter)
print(' type : %s' % error.__class__)

except requests.exceptions.HTTPError as e:
print('\nAn http exception occurred while creating a call:')
print(' ', e)
print(' Http request body: ', e.request.body)
print(' Http response status: ', e.response.status_code)
print(' Http response body: ', e.response.content.decode())

except Exception as e:
print('\nAn ', e.__class__, ' exception occurred while creating a call:')
print(e)

48 changes: 48 additions & 0 deletions examples/call_delete.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env python
import os
import sys
import json
import argparse
import requests

sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
import messagebird

parser = argparse.ArgumentParser(usage='call_create.py\
--accessKey="*******" \
--callId=dda20377-72da-4846-9b2c-0fea3ad4bcb6 \
')
parser.add_argument('--accessKey', help='Access key for MessageBird API.', type=str, required=True)
parser.add_argument('--callId', help='The ID of the MessageBird call to delete.', type=str, required=True)
args = vars(parser.parse_args())

try:
# Create a MessageBird client with the specified accessKey.
client = messagebird.Client(args['accessKey'])

# Create a call for the specified callID.
call = client.call_delete(args['callId'])

# If no error is thrown, means delete was successful.
print('\nDeleted call with id `%s` successfully!' % args['callId'])

except messagebird.client.ErrorException as e:
print('\nAn error occurred while creating a call:\n')

for error in e.errors:
print(' code : %d' % error.code)
print(' description : %s' % error.description)
print(' parameter : %s' % error.parameter)
print(' type : %s' % error.__class__)

except requests.exceptions.HTTPError as e:
print('\nAn http exception occurred while deleting a call:')
print(' ', e)
print(' Http request body: ', e.request.body)
print(' Http response status: ', e.response.status_code)
print(' Http response body: ', e.response.content.decode())

except Exception as e:
print('\nAn ', e.__class__, ' exception occurred while deleting a call:')
print(e)

64 changes: 64 additions & 0 deletions examples/call_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#!/usr/bin/env python
import os
import sys
import argparse
import requests

sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
import messagebird


parser = argparse.ArgumentParser(usage='call_create.py\
--accessKey="*******" \
--page=1 \
')
parser.add_argument('--accessKey', help='Access key for MessageBird API.', type=str, required=True)
parser.add_argument('--page', help='The page you wish to view.', type=str, required=False, default=1)
args = vars(parser.parse_args())

try:
# Create a MessageBird client with the specified accessKey.
client = messagebird.Client(args['accessKey'])
del(args['accessKey'])

# Create a call for the specified callID.
callList = client.call_list(**args)

# Print the object information.
print('\nThe following information was returned as a %s object:\n' % callList.__class__)
if callList.items is not None:
print(' Containing the the following items:')
for item in callList.items:
print(' {')
print(' id : %s' % item.id)
print(' status : %s' % item.status)
print(' source : %s' % item.source)
print(' destination : %s' % item.destination)
print(' createdAt : %s' % item.createdAt)
print(' updatedAt : %s' % item.updatedAt)
print(' endedAt : %s' % item.endedAt)
print(' },')
else:
print(' With an empty response.')

except messagebird.client.ErrorException as e:
print('\nAn error occurred while listing calls:\n')

for error in e.errors:
print(' code : %d' % error.code)
print(' description : %s' % error.description)
print(' parameter : %s' % error.parameter)
print(' type : %s' % error.__class__)

except requests.exceptions.HTTPError as e:
print('\nAn HTTP exception occurred while listing calls:')
print(' ', e)
print(' Http request body: ', e.request.body)
print(' Http response status: ', e.response.status_code)
print(' Http response body: ', e.response.content.decode())

except Exception as e:
print('\nAn ', e.__class__, ' exception occurred while creating a call:')
print(e)


4 changes: 4 additions & 0 deletions messagebird/base_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,7 @@ def items(self, value):
items.append(self.itemType().load(item))

self._items = items

def __str__(self):
items_count = 0 if self.items is None else len(self.items)
return "%s with %d items.\n" % (str(self.__class__), items_count)
6 changes: 6 additions & 0 deletions messagebird/call.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,9 @@ def data(self):
@data.setter
def data(self, value):
self._data = CallData().load(value[0])

def __str__(self):
return "\n".join([
'id : %s' % self.id,
'data.'+'data.'.join(str(self._data).splitlines(True)),
])
12 changes: 12 additions & 0 deletions messagebird/call_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,15 @@ def webhook(self):
@webhook.setter
def webhook(self, value):
self._webhook = Webhook.load(value)

def __str__(self):
return "\n".join([
'id : %s' % self.id,
'status : %s' % self.status,
'source : %s' % self.source,
'destination : %s' % self.destination,
'webhook : %s' % self.webhook,
'updatedAt : %s' % self.updatedAt,
'createdAt : %s' % self.createdAt,
'endedAt : %s' % self.endedAt,
])
42 changes: 42 additions & 0 deletions messagebird/call_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from messagebird.base_list import BaseList
from messagebird.call_data import CallData


class CallList(BaseList):
def __init__(self):
# We're expecting items of type CallData
super(CallList, self).__init__(CallData)
self.perPage = None
self.currentPage = None
self.pageCount = None
self._pagination = None

@property
def data(self):
return self.items

@property
def pagination(self):
return {
"totalCount": self.totalCount,
"pageCount": self.pageCount,
"currentPage": self.currentPage,
"perPage": self.perPage
}

@pagination.setter
def pagination(self, value):
if isinstance(value, dict):
self.totalCount = value['totalCount']
self.pageCount = value['pageCount']
self.currentPage = value['currentPage']
self.perPage = value['perPage']
self.limit = self.perPage * self.currentPage
self.offset = self.perPage * (self.currentPage - 1)

@data.setter
def data(self, value):
if isinstance(value, list):
self.count = len(value)
self.items = value

Loading