def send_messagebird_sms(settings, mobile_number, message): """ Sends an SMS to the mobile number given for mobile number reset confirmation. :param object settings: :param string mobile_number: :param string message: Message. :return bool: True on success and False on failure. """ sms_client = MessagebirdClient(settings.message_bird_access_key) language = api.portal.get_default_language() user = api.user.get_current() if user.getProperty('language'): language = user.getProperty('language') message = translate(message, target_language=language) try: sms_client.message_create( settings.message_bird_sender, mobile_number, message.encode('UTF-8') ) return True except (MessagebirdException, RequestException) as e: # Log in the error_log logger.exception(e) return False
def send_messagebird_sms(settings, mobile_number, message): """ Sends an SMS to the mobile number given for mobile number reset confirmation. :param object settings: :param string mobile_number: :param string message: Message. :return bool: True on success and False on failure. """ sms_client = MessagebirdClient(settings.message_bird_access_key) language = api.portal.get_default_language() user = api.user.get_current() if user.getProperty('language'): language = user.getProperty('language') message = translate(message, target_language=language) try: sms_client.message_create(settings.message_bird_sender, mobile_number, message.encode('UTF-8')) return True except (MessagebirdException, RequestException) as e: # Log in the error_log logger.exception(e) return False
def test_create_mms(self): http_client = Mock() http_client.request.return_value = '{"originator": "test-org", "body": "Rich test message", "direction": "mt", "recipients": {"totalCount": 1, "totalSentCount": 1, "totalDeliveredCount": 0, "totalDeliveryFailedCount": 0, "items": [{"status": "sent", "statusDatetime": "2019-06-04T13:54:48+00:00", "recipient": 4915238456487}]}, "reference": null, "createdDatetime": "2019-06-04T13:54:48+00:00", "href": "https://rest.messagebird.com/mms/0a75f8f82b5d4377bd8fb5b22ac1e8ac", "mediaUrls": ["https://www.messagebird.com/assets/images/og/messagebird.gif"], "scheduledDatetime": null, "id": "0a75f8f82b5d4377bd8fb5b22ac1e8ac", "subject": null}' params = { "originator": "test-org", "body": "Rich test message", "recipients": "+4915238456487", "mediaUrls": "https://www.messagebird.com/assets/images/og/messagebird.gif" } mms = Client('', http_client).mms_create(**params) params["mediaUrls"] = [params["mediaUrls"]] params.update({ 'subject': None, 'reference': None, 'scheduledDatetime': None }) http_client.request.assert_called_once_with('mms', 'POST', params) self.assertEqual(params["originator"], mms.originator) self.assertEqual(params["recipients"].strip("+"), str(mms.recipients["items"][0].recipient)) self.assertEqual(1, len(mms.recipients["items"]))
def test_create_message(self): http_client = Mock() http_client.request.return_value = """ { "id": "id", "conversationId": "conversation-id", "channelId": "channel-id", "type": "text", "content": { "text": "Example Text Message" }, "direction": "sent", "status": "pending", "createdDatetime": "2019-04-02T11:57:52.142641447Z", "updatedDatetime": "2019-04-02T11:57:53.142641447Z" }""" data = { 'channelId': 1234, 'type': 'text', 'content': { 'text': 'this is a message' }, } msg = Client('', http_client).conversation_create_message('conversation-id', data) self.assertEqual(datetime(2019, 4, 2, 11, 57, 53, tzinfo=tzutc()), msg.updatedDatetime) self.assertEqual(datetime(2019, 4, 2, 11, 57, 52, tzinfo=tzutc()), msg.createdDatetime) http_client.request.assert_called_once_with('conversations/conversation-id/messages', 'POST', data)
def test_conversation_read_message(self): http_client = Mock() http_client.request.return_value = '{}' Client('', http_client).conversation_read_message('message-id') http_client.request.assert_called_once_with('messages/message-id', 'GET', None)
def test_contact(self): http_client = Mock() http_client.request.return_value = """ { "id": "contact-id", "href": "https://rest.messagebird.com/contacts/contact-id", "msisdn": 31612345678, "firstName": "Foo", "lastName": "Bar", "customDetails": { "custom1": "First", "custom2": "Second", "custom3": "Third", "custom4": "Fourth" }, "groups": { "totalCount": 3, "href": "https://rest.messagebird.com/contacts/contact-id/groups" }, "messages": { "totalCount": 5, "href": "https://rest.messagebird.com/contacts/contact-id/messages" }, "createdDatetime": "2018-07-13T10:34:08+00:00", "updatedDatetime": "2018-07-13T10:44:08+00:00" }""" contact = Client('', http_client).contact('contact-id') http_client.request.assert_called_once_with('contacts/contact-id', 'GET', None) self.assertEqual(31612345678, contact.msisdn) self.assertEqual('First', contact.customDetails.custom1) self.assertEqual(3, contact.groups.totalCount) self.assertEqual('https://rest.messagebird.com/contacts/contact-id/messages', contact.messages.href)
def test_voice_create_webhook(self): http_client = Mock() http_client.request.return_value = '''{ "data": [ { "id": "534e1848-235f-482d-983d-e3e11a04f58a", "url": "https://example.com/", "token": "foobar", "createdAt": "2017-03-15T14:10:07Z", "updatedAt": "2017-03-15T14:10:07Z" } ], "_links": { "self": "/webhooks/534e1848-235f-482d-983d-e3e11a04f58a" } }''' create_webhook_request = VoiceCreateWebhookRequest( url="https://example.com/", title="FooBar", token="foobar") created_webhook = Client( '', http_client).voice_create_webhook(create_webhook_request) http_client.request.assert_called_once_with( VOICE_API_ROOT + '/' + VOICE_WEB_HOOKS_PATH, 'POST', create_webhook_request.__dict__()) self.assertEqual(create_webhook_request.url, created_webhook.url) self.assertEqual(create_webhook_request.token, created_webhook.token)
def test_call(self): http_client = Mock() http_client.request.return_value = """ { "data":[ { "id":"call-id", "status":"ended", "source":"16479311111", "destination":"1416555555", "createdAt":"2019-08-06T13:17:06Z", "updatedAt":"2019-08-06T13:17:39Z", "endedAt":"2019-08-06T13:17:39Z" } ], "_links":{ "legs":"/calls/66bd9f08-a8af-40fe-a830-652d8dabc057/legs", "self":"/calls/66bd9f08-a8af-40fe-a830-652d8bca357" }, "pagination":{ "totalCount":0, "pageCount":0, "currentPage":0, "perPage":0 } } """ call = Client('', http_client).call('call-id') http_client.request.assert_called_once_with('calls/call-id', 'GET', None) self.assertEqual('ended', call.data.status)
def test_voice_update_webhook(self): http_client = Mock() http_client.request.return_value = '''{ "data": [ { "id": "534e1848-235f-482d-983d-e3e11a04f58a", "url": "https://example.com/baz", "token": "foobar", "createdAt": "2017-03-15T13:27:02Z", "updatedAt": "2017-03-15T13:28:01Z" } ], "_links": { "self": "/webhooks/534e1848-235f-482d-983d-e3e11a04f58a" } }''' webhook_id = '534e1848-235f-482d-983d-e3e11a04f58a' update_webhook_request = VoiceUpdateWebhookRequest(title="FooBar", token="foobar") updated_webhook = Client('', http_client).voice_update_webhook( webhook_id, update_webhook_request) http_client.request.assert_called_once_with( VOICE_API_ROOT + '/' + VOICE_WEB_HOOKS_PATH + '/' + webhook_id, 'PUT', update_webhook_request.__dict__()) self.assertEqual(update_webhook_request.token, updated_webhook.token)
def test_get_flow(self): http_client = Mock() http_client.request.return_value = '''{ "data": [ { "id": "de3ed163-d5fc-45f4-b8c4-7eea7458c635", "title": "Updated call flow", "record": false, "steps": [ { "id": "3538a6b8-5a2e-4537-8745-f72def6bd393", "action": "transfer", "options": { "destination": "31611223344" } } ], "createdAt": "2017-03-06T13:34:14Z", "updatedAt": "2017-03-06T15:02:38Z" } ], "_links": { "self": "/call-flows/de3ed163-d5fc-45f4-b8c4-7eea7458c635" } } ''' call_flow = Client('', http_client).call_flow('de3ed163-d5fc-45f4-b8c4-7eea7458c635') http_client.request.assert_called_once_with('call-flows/de3ed163-d5fc-45f4-b8c4-7eea7458c635', 'GET', None) self.assertEqual('Updated call flow', call_flow.title) self.assertIsNotNone(call_flow.steps)
def test_numbers_list(self): http_client = Mock() http_client.request.return_value = '''{ "data": [ { "id": "13f38f34-7ff4-45b3-8783-8d5b1143f22b", "number": "31611111111", "callFlowId": "de3ed163-d5fc-45f4-b8c4-7eea7458c635", "createdAt": "2017-03-16T13:49:24Z", "updatedAt": "2017-09-12T08:59:50Z", "_links": { "self": "/numbers/13f38f34-7ff4-45b3-8783-8d5b1143f22b" } } ], "_links": { "self": "/call-flows/de3ed163-d5fc-45f4-b8c4-7eea7458c635/numbers?page=1" }, "pagination": { "totalCount": 1, "pageCount": 1, "currentPage": 1, "perPage": 10 } } ''' number_list = Client('', http_client).call_flow_numbers_list('de3ed163-d5fc-45f4-b8c4-7eea7458c635') http_client.request.assert_called_once_with('call-flows/de3ed163-d5fc-45f4-b8c4-7eea7458c635/numbers', 'GET', None) self.assertEqual('31611111111', number_list.data[0].number) self.assertEqual(1, number_list.pagination['totalCount'])
def test_conversation_start(self): http_client = Mock() http_client.request.return_value = TEST_CONVERSATION_START_RESPONSE data = { 'channelId': '1234', 'to': '+99999999999', 'type': "text", 'content': { 'text': 'Message Example' }, } msg = Client('', http_client).conversation_start(data) http_client.request.assert_called_once_with('conversations/start', 'POST', data) self.assertEqual('1234', msg.id) self.assertEqual(99999999999, msg.contact.msisdn) self.assertEqual(datetime(2019, 4, 2, 8, 19, 37, tzinfo=tzutc()), msg.contact.createdDatetime) self.assertEqual(datetime(2019, 4, 2, 8, 19, 38, tzinfo=tzutc()), msg.contact.updatedDatetime) self.assertEqual('channel-name', msg.channels[0].name)
def test_call_delete(self): http_client = Mock() http_client.request.return_value = '' call_id_to_delete = '21025ed1-cc1d-4554-ac05-043fa6c84e00' Client('', http_client).call_delete(call_id_to_delete) http_client.request.assert_called_once_with('calls/%s' % call_id_to_delete, 'DELETE', None)
def test_create_mms(self): http_client = Mock() http_client.request.return_value = TEST_CREATE_MMS_RESPONSE params = { "originator": "test-org", "body": "Rich test message", "recipients": "+4915238456487", "mediaUrls": "https://www.messagebird.com/assets/images/og/messagebird.gif" } mms = Client('', http_client).mms_create(**params) params["mediaUrls"] = [params["mediaUrls"]] params.update({ 'subject': None, 'reference': None, 'scheduledDatetime': None }) http_client.request.assert_called_once_with('mms', 'POST', params) self.assertEqual(params["originator"], mms.originator) self.assertEqual(params["recipients"].strip("+"), str(mms.recipients["items"][0].recipient)) self.assertEqual(1, len(mms.recipients["items"]))
def test_hlr_create(self): http_client = Mock() http_client.request.return_value = '{}' Client('', http_client).hlr_create(31612345678, 'MyReference') http_client.request.assert_called_once_with('hlr', 'POST', {'msisdn': 31612345678, 'reference': 'MyReference'})
def test_voice_read_webhook(self): http_client = Mock() http_client.request.return_value = '''{ "data": [ { "id": "534e1848-235f-482d-983d-e3e11a04f58a", "url": "https://example.com/", "token": "foobar", "createdAt": "2017-03-15T13:28:32Z", "updatedAt": "2017-03-15T13:28:32Z" } ], "_links": { "self": "/webhooks/534e1848-235f-482d-983d-e3e11a04f58a" } }''' webhook_id = '534e1848-235f-482d-983d-e3e11a04f58a' webhook_token = 'foobar' voice_webhook = Client('', http_client).voice_read_webhook(webhook_id) http_client.request.assert_called_once_with( VOICE_API_ROOT + '/' + VOICE_WEB_HOOKS_PATH + '/' + webhook_id, 'GET', None) self.assertEqual(webhook_id, voice_webhook.id) self.assertEqual(webhook_token, voice_webhook.token)
def test_conversation_webhook_delete(self): http_client = Mock() http_client.request.return_value = '' Client('', http_client).conversation_delete_webhook('webhook-id') http_client.request.assert_called_once_with('webhooks/webhook-id', 'DELETE', None)
def test_conversation_webhook_list_pagination(self): http_client = Mock() http_client.request.return_value = '{"offset":0,"limit":10,"count":2,"totalCount":2,"items":[{"id":"57b96dbe0fda40f0a814f5e3268c30a9","contactId":"8846d44229094c20813cf9eea596e680","contact":{"id":"8846d44229094c20813cf9eea596e680","href":"https://contacts.messagebird.com/v2/contacts/8846d44229094c20813cf9eea596e680","msisdn":31617110163,"displayName":"31617110163","firstName":"","lastName":"","customDetails":{},"attributes":{},"createdDatetime":"2019-04-02T08:54:39Z","updatedDatetime":"2019-04-02T08:54:40Z"},"channels":[{"id":"c0dae31e440145e094c4708b7d908842","name":"test","platformId":"sms","status":"active","createdDatetime":"2019-04-01T15:25:12Z","updatedDatetime":"0001-01-01T00:00:00Z"}],"status":"active","createdDatetime":"2019-04-02T08:54:38Z","updatedDatetime":"2019-04-02T14:24:09.192202886Z","lastReceivedDatetime":"2019-04-02T14:24:09.14826339Z","lastUsedChannelId":"c0dae31e440145e094c4708b7d908842","messages":{"totalCount":2,"href":"https://conversations.messagebird.com/v1/conversations/57b96dbe0fda40f0a814f5e3268c30a9/messages"}},{"id":"07e823fdb36a462fb5e187d6d7b96493","contactId":"459a35432b0c4195abbdae353eb19359","contact":{"id":"459a35432b0c4195abbdae353eb19359","href":"https://contacts.messagebird.com/v2/contacts/459a35432b0c4195abbdae353eb19359","msisdn":31615164888,"displayName":"31615164888","firstName":"","lastName":"","customDetails":{},"attributes":{},"createdDatetime":"2019-04-02T08:19:37Z","updatedDatetime":"2019-04-02T08:19:38Z"},"channels":[{"id":"c0dae31e440145e094c4708b7d908842","name":"test","platformId":"sms","status":"active","createdDatetime":"2019-04-01T15:25:12Z","updatedDatetime":"0001-01-01T00:00:00Z"}],"status":"active","createdDatetime":"2019-04-02T08:19:37Z","updatedDatetime":"2019-04-03T07:35:47.35395356Z","lastReceivedDatetime":"2019-04-02T12:02:22.707634424Z","lastUsedChannelId":"c0dae31e440145e094c4708b7d908842","messages":{"totalCount":16,"href":"https://conversations.messagebird.com/v1/conversations/07e823fdb36a462fb5e187d6d7b96493/messages"}}]}' Client('', http_client).conversation_list_webhooks(2, 1) http_client.request.assert_called_once_with( 'webhooks?limit=2&offset=1', 'GET', None)
def test_conversation_webhook_list_pagination(self): http_client = Mock() http_client.request.return_value = TEST_CONVERSATION_WEBHOOK_LIST_PAGINATION_RESPONSE Client('', http_client).conversation_list_webhooks(2, 1) http_client.request.assert_called_once_with( 'webhooks?limit=2&offset=1', 'GET', None)
def test_voicemessages_list(self): http_client = Mock() http_client.request.return_value = '{ "offset": 0, "limit": 10, "count": 2, "totalCount": 2, "links": { "first": "https://rest.messagebird.com/voicemessages/?offset=0&limit=30", "previous": null, "next": null, "last": "https://rest.messagebird.com/voicemessages/?offset=0&limit=30" }, "items": [ { "id": "12345678-9012-3456-7890-123456789012", "href": "https://rest.messagebird.com/voicemessages/12345678-9012-3456-7890-123456789012", "originator": null, "body": "This is a test message.", "reference": null, "language": "en-gb", "voice": "male", "repeat": 1, "ifMachine": "continue", "machineTimeout": 7000, "scheduledDatetime": null, "createdDatetime": "2020-02-04T15:15:30+00:00", "recipients": { "totalCount": 1, "totalSentCount": 1, "totalDeliveredCount": 1, "totalDeliveryFailedCount": 0, "items": [ { "recipient": 31612345678, "originator": null, "status": "answered", "statusDatetime": "2020-02-04T15:15:57+00:00" } ] } }, { "id": "12345678-9012-3456-7890-123456789013", "href": "https://rest.messagebird.com/voicemessages/12345678-9012-3456-7890-123456789013", "originator": null, "body": "The voice message to be sent", "reference": null, "language": "en-gb", "voice": "female", "repeat": 1, "ifMachine": "delay", "machineTimeout": 7000, "scheduledDatetime": null, "createdDatetime": "2020-02-04T12:26:44+00:00", "recipients": { "totalCount": 1, "totalSentCount": 1, "totalDeliveredCount": 1, "totalDeliveryFailedCount": 0, "items": [ { "recipient": 31612345678, "originator": null, "status": "answered", "statusDatetime": "2020-02-04T12:27:32+00:00" } ] } } ] }' voice_messages = Client('', http_client).voice_message_list() http_client.request.assert_called_once_with( 'voicemessages?limit=10&offset=0', 'GET', None) voice_messages_check = { '12345678-9012-3456-7890-123456789012': { "id": '12345678-9012-3456-7890-123456789012', "href": "https://rest.messagebird.com/voicemessages/12345678-9012-3456-7890-123456789012" }, '12345678-9012-3456-7890-123456789013': { "id": '12345678-9012-3456-7890-123456789013', "href": "https://rest.messagebird.com/voicemessages/12345678-9012-3456-7890-123456789013" } } for item in voice_messages.items: message_specific = voice_messages_check.get(item.id) self.assertEqual(message_specific['id'], item.id) self.assertEqual(message_specific['href'], item.href) self.assertIsInstance(str(voice_messages), str)
def test_conversation_start(self): http_client = Mock() http_client.request.return_value = '{"id":"1234","contactId":"1234","contact":{"id":"1234","href":"https://contacts.messagebird.com/v2/contacts/1234","msisdn":99999999999,"displayName":"99999999999","firstName":"","lastName":"","customDetails":{},"attributes":{},"createdDatetime":"2019-04-02T08:19:37Z","updatedDatetime":"2019-04-02T08:19:38Z"},"channels":[{"id":"1234","name":"channel-name","platformId":"sms","status":"active","createdDatetime":"2019-04-01T15:25:12Z","updatedDatetime":"0001-01-01T00:00:00Z"}],"status":"active","createdDatetime":"2019-04-02T08:19:37Z","updatedDatetime":"2019-04-02T08:54:42.497114599Z","lastReceivedDatetime":"2019-04-02T08:54:42.464955904Z","lastUsedChannelId":"1234","messages":{"totalCount":1,"href":"https://conversations.messagebird.com/v1/conversations/1234/messages"}}' data = { 'channelId': '1234', 'to': '+99999999999', 'type': "text", 'content': { 'text': 'Message Example' }, } msg = Client('', http_client).conversation_start(data) http_client.request.assert_called_once_with('conversations/start', 'POST', data) self.assertEqual('1234', msg.id) self.assertEqual(99999999999, msg.contact.msisdn) self.assertEqual(datetime(2019, 4, 2, 8, 19, 37, tzinfo=tzutc()), msg.contact.createdDatetime) self.assertEqual(datetime(2019, 4, 2, 8, 19, 38, tzinfo=tzutc()), msg.contact.updatedDatetime) self.assertEqual('channel-name', msg.channels[0].name)
def test_contact_delete(self): http_client = Mock() http_client.request.return_value = '' Client('', http_client).contact_delete('contact-id') http_client.request.assert_called_once_with('contacts/contact-id', 'DELETE', None)
def test_voicemessages_list(self): http_client = Mock() http_client.request.return_value = TEST_VOICE_MESSAGE_LIST_RESPONSE voice_messages = Client('', http_client).voice_message_list() http_client.request.assert_called_once_with( 'voicemessages?limit=10&offset=0', 'GET', None) voice_messages_check = { '12345678-9012-3456-7890-123456789012': { "id": '12345678-9012-3456-7890-123456789012', "href": "https://rest.messagebird.com/voicemessages/12345678-9012-3456-7890-123456789012" }, '12345678-9012-3456-7890-123456789013': { "id": '12345678-9012-3456-7890-123456789013', "href": "https://rest.messagebird.com/voicemessages/12345678-9012-3456-7890-123456789013" } } for item in voice_messages.items: message_specific = voice_messages_check.get(item.id) self.assertEqual(message_specific['id'], item.id) self.assertEqual(message_specific['href'], item.href) self.assertIsInstance(str(voice_messages), str)
def test_conversation_list(self): http_client = Mock() http_client.request.return_value = '{}' Client('', http_client).conversation_list() http_client.request.assert_called_once_with( 'conversations?limit=10&offset=0', 'GET', None)
def test_verify_delete(self): http_client = Mock() http_client.request.return_value = '' Client('', http_client).verify_delete('31612345678') http_client.request.assert_called_once_with('verify/31612345678', 'DELETE', None)
def test_verify_create(self): http_client = Mock() http_client.request.return_value = '{}' Client('', http_client).verify_create('31612345678', {}) http_client.request.assert_called_once_with( 'verify', 'POST', {'recipient': '31612345678'})
def test_contact_delete_invalid(self): http_client = Mock() http_client.request.return_value = '{"errors": [{"code": 20,"description": "contact not found","parameter": null}]}' with self.assertRaises(ErrorException): Client('', http_client).contact_delete('non-existent-contact-id') http_client.request.assert_called_once_with('contacts/non-existent-contact-id', 'DELETE', None)
def test_contact_create(self): http_client = Mock() http_client.request.return_value = '{}' Client('', http_client).contact_create(31612345678, {'firstName': 'Foo', 'custom3': 'Third'}) http_client.request.assert_called_once_with( 'contacts', 'POST', {'msisdn': 31612345678, 'firstName': 'Foo', 'custom3': 'Third'})
def test_scheduled_message_list(self): http_client = Mock() http_client.request.return_value = '{"offset": 0,"limit": 20,"count": 2,"totalCount": 2,"links": {"first": "https://rest.messagebird.com/messages/?offset=0","previous": null,"next": null,"last": "https://rest.messagebird.com/messages/?offset=0"},"items": []}' message_list = Client('', http_client).message_list(20, 0, "scheduled") http_client.request.assert_called_once_with( 'messages?limit=20&offset=0&status=scheduled', 'GET', None)
def test_message_delete(self): http_client = Mock() http_client.request.return_value = '{}' Client('', http_client).message_delete('message-id') http_client.request.assert_called_once_with('messages/message-id', 'DELETE', None)
def test_voice_recording_download(self): http_client = Mock() http_client.request.return_value = TEST_VOICE_RECORDING_DOWNLOAD_RESPONSE with self.assertRaises(ErrorException): voice_recording = Client('', http_client).voice_recording_download( '12348765-4321-0987-6543-210987654321', '87654321-0987-6543-2109-876543210987', '12345678-9012-3456-7890-123456789012') http_client.request.return_value = TEST_VOICE_RECORDING_DOWNLOAD_RESPONSE with self.assertRaises(ErrorException): voice_recording = Client('', http_client).voice_recording_download( '12348765-4321-0987-6543-210987654321', '87654321-0987-6543-2109-876543210987', '12345678-9012-3456-7890-123456789012')