|
| 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 | + |
0 commit comments