diff --git a/examples/conversation_update_webhook.py b/examples/conversation_update_webhook.py new file mode 100644 index 0000000..08084c8 --- /dev/null +++ b/examples/conversation_update_webhook.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python +import messagebird +import argparse +from messagebird.conversation_webhook import \ + CONVERSATION_WEBHOOK_EVENT_CONVERSATION_CREATED, \ + CONVERSATION_WEBHOOK_EVENT_CONVERSATION_UPDATED + + +parser = argparse.ArgumentParser() +parser.add_argument('--accessKey', help='access key for MessageBird API', type=str, required=True) +parser.add_argument('--webhookId', help='webhook that you want to update', type=str, required=True) +parser.add_argument('--url', help='url for the webhook', type=str) +parser.add_argument('--status', help='Status of the webhook. Can be set to "enabled" or "disabled"', type=str, default='enabled') +args = vars(parser.parse_args()) + +try: + client = messagebird.Client(args['accessKey']) + + update_request = { + 'events': [CONVERSATION_WEBHOOK_EVENT_CONVERSATION_CREATED, CONVERSATION_WEBHOOK_EVENT_CONVERSATION_UPDATED], + 'url': args['url'], + 'status': args['status'] + } + webhook = client.conversation_update_webhook(args['webhookId'], update_request) + + # Print the object information. + print('The following information was returned as a Webhook object:') + print(webhook) + +except messagebird.client.ErrorException as e: + print('An error occured while requesting a Webhook object:') + + for error in e.errors: + print(' code : %d' % error.code) + print(' description : %s' % error.description) + print(' parameter : %s\n' % error.parameter) diff --git a/messagebird/client.py b/messagebird/client.py index a935189..fdd51f6 100644 --- a/messagebird/client.py +++ b/messagebird/client.py @@ -269,6 +269,16 @@ def conversation_create_webhook(self, webhook_create_request): return ConversationWebhook().load( self.request(CONVERSATION_WEB_HOOKS_PATH, 'POST', webhook_create_request, CONVERSATION_TYPE)) + def conversation_update_webhook(self, id, update_request): + """ + Updates a webhook with the supplied parameters. + + API Reference: https://developers.messagebird.com/api/conversations/#webhooks + """ + uri = CONVERSATION_WEB_HOOKS_PATH + '/' + str(id) + web_hook = self.request(uri, 'PATCH', update_request, CONVERSATION_TYPE) + return ConversationWebhook().load(web_hook) + def conversation_delete_webhook(self, id): uri = CONVERSATION_WEB_HOOKS_PATH + '/' + str(id) self.request(uri, 'DELETE', None, CONVERSATION_TYPE) diff --git a/messagebird/conversation_webhook.py b/messagebird/conversation_webhook.py index 9729743..68e924d 100644 --- a/messagebird/conversation_webhook.py +++ b/messagebird/conversation_webhook.py @@ -13,6 +13,7 @@ def __init__(self): self.channelId = None self.url = None self.events = None + self.status = None self._createdDatetime = None self._updatedDatetime = None @@ -37,11 +38,12 @@ def __str__(self): 'id : %s' % self.id, 'events : %s' % self.events, 'channel id : %s' % self.channelId, + 'status : %s' % self.status, + 'url : %s' % self.url, 'created date time : %s' % self.createdDatetime, 'updated date time : %s' % self.updatedDatetime ]) - class ConversationWebhookList(Base): def __init__(self): self.offset = None diff --git a/tests/test_conversation_webhook.py b/tests/test_conversation_webhook.py index ec009e8..d320c69 100644 --- a/tests/test_conversation_webhook.py +++ b/tests/test_conversation_webhook.py @@ -1,4 +1,5 @@ import unittest +import json from datetime import datetime from messagebird import Client from messagebird.conversation_webhook import \ @@ -62,3 +63,26 @@ def test_conversation_webhook_read(self): self.assertEqual(datetime(2019, 4, 3, 8, 41, 37), web_hook.createdDatetime) self.assertEqual(None, web_hook.updatedDatetime) self.assertEqual(['conversation.created', 'conversation.updated'], web_hook.events) + + def test_conversation_webhook_update(self): + http_client = Mock() + http_client.request.return_value = json.dumps({"id": "985ae50937a94c64b392531ea87a0263", + "url": "https://example.com/webhook", + "channelId": "853eeb5348e541a595da93b48c61a1ae", + "events": [ + "message.created", + "message.updated", + ], + "status": "enabled", + "createdDatetime": "2018-08-29T10:04:23Z", + "updatedDatetime": "2018-08-29T10:10:23Z" + }) + + webhookRequestData = { + 'events': [CONVERSATION_WEBHOOK_EVENT_CONVERSATION_CREATED, + CONVERSATION_WEBHOOK_EVENT_CONVERSATION_UPDATED], + 'url': 'https://example.com/webhook', + 'status': 'enabled' + } + web_hook = Client('', http_client).conversation_update_webhook('webhook-id', webhookRequestData) + http_client.request.assert_called_once_with('webhooks/webhook-id', 'PATCH', webhookRequestData) \ No newline at end of file