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
36 changes: 36 additions & 0 deletions examples/conversation_update_webhook.py
Original file line number Diff line number Diff line change
@@ -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)
10 changes: 10 additions & 0 deletions messagebird/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion messagebird/conversation_webhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
24 changes: 24 additions & 0 deletions tests/test_conversation_webhook.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import unittest
import json
from datetime import datetime
from messagebird import Client
from messagebird.conversation_webhook import \
Expand Down Expand Up @@ -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)