Skip to content

Commit b679fb4

Browse files
authored
Merge pull request #37 from wenkat/feature/ENG-130
Feature/eng 130
2 parents a0b12f6 + b550326 commit b679fb4

File tree

4 files changed

+73
-1
lines changed

4 files changed

+73
-1
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/env python
2+
import messagebird
3+
import argparse
4+
from messagebird.conversation_webhook import \
5+
CONVERSATION_WEBHOOK_EVENT_CONVERSATION_CREATED, \
6+
CONVERSATION_WEBHOOK_EVENT_CONVERSATION_UPDATED
7+
8+
9+
parser = argparse.ArgumentParser()
10+
parser.add_argument('--accessKey', help='access key for MessageBird API', type=str, required=True)
11+
parser.add_argument('--webhookId', help='webhook that you want to update', type=str, required=True)
12+
parser.add_argument('--url', help='url for the webhook', type=str)
13+
parser.add_argument('--status', help='Status of the webhook. Can be set to "enabled" or "disabled"', type=str, default='enabled')
14+
args = vars(parser.parse_args())
15+
16+
try:
17+
client = messagebird.Client(args['accessKey'])
18+
19+
update_request = {
20+
'events': [CONVERSATION_WEBHOOK_EVENT_CONVERSATION_CREATED, CONVERSATION_WEBHOOK_EVENT_CONVERSATION_UPDATED],
21+
'url': args['url'],
22+
'status': args['status']
23+
}
24+
webhook = client.conversation_update_webhook(args['webhookId'], update_request)
25+
26+
# Print the object information.
27+
print('The following information was returned as a Webhook object:')
28+
print(webhook)
29+
30+
except messagebird.client.ErrorException as e:
31+
print('An error occured while requesting a Webhook object:')
32+
33+
for error in e.errors:
34+
print(' code : %d' % error.code)
35+
print(' description : %s' % error.description)
36+
print(' parameter : %s\n' % error.parameter)

messagebird/client.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,16 @@ def conversation_create_webhook(self, webhook_create_request):
269269
return ConversationWebhook().load(
270270
self.request(CONVERSATION_WEB_HOOKS_PATH, 'POST', webhook_create_request, CONVERSATION_TYPE))
271271

272+
def conversation_update_webhook(self, id, update_request):
273+
"""
274+
Updates a webhook with the supplied parameters.
275+
276+
API Reference: https://developers.messagebird.com/api/conversations/#webhooks
277+
"""
278+
uri = CONVERSATION_WEB_HOOKS_PATH + '/' + str(id)
279+
web_hook = self.request(uri, 'PATCH', update_request, CONVERSATION_TYPE)
280+
return ConversationWebhook().load(web_hook)
281+
272282
def conversation_delete_webhook(self, id):
273283
uri = CONVERSATION_WEB_HOOKS_PATH + '/' + str(id)
274284
self.request(uri, 'DELETE', None, CONVERSATION_TYPE)

messagebird/conversation_webhook.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ def __init__(self):
1313
self.channelId = None
1414
self.url = None
1515
self.events = None
16+
self.status = None
1617
self._createdDatetime = None
1718
self._updatedDatetime = None
1819

@@ -37,11 +38,12 @@ def __str__(self):
3738
'id : %s' % self.id,
3839
'events : %s' % self.events,
3940
'channel id : %s' % self.channelId,
41+
'status : %s' % self.status,
42+
'url : %s' % self.url,
4043
'created date time : %s' % self.createdDatetime,
4144
'updated date time : %s' % self.updatedDatetime
4245
])
4346

44-
4547
class ConversationWebhookList(Base):
4648
def __init__(self):
4749
self.offset = None

tests/test_conversation_webhook.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import unittest
2+
import json
23
from datetime import datetime
34
from messagebird import Client
45
from messagebird.conversation_webhook import \
@@ -62,3 +63,26 @@ def test_conversation_webhook_read(self):
6263
self.assertEqual(datetime(2019, 4, 3, 8, 41, 37), web_hook.createdDatetime)
6364
self.assertEqual(None, web_hook.updatedDatetime)
6465
self.assertEqual(['conversation.created', 'conversation.updated'], web_hook.events)
66+
67+
def test_conversation_webhook_update(self):
68+
http_client = Mock()
69+
http_client.request.return_value = json.dumps({"id": "985ae50937a94c64b392531ea87a0263",
70+
"url": "https://example.com/webhook",
71+
"channelId": "853eeb5348e541a595da93b48c61a1ae",
72+
"events": [
73+
"message.created",
74+
"message.updated",
75+
],
76+
"status": "enabled",
77+
"createdDatetime": "2018-08-29T10:04:23Z",
78+
"updatedDatetime": "2018-08-29T10:10:23Z"
79+
})
80+
81+
webhookRequestData = {
82+
'events': [CONVERSATION_WEBHOOK_EVENT_CONVERSATION_CREATED,
83+
CONVERSATION_WEBHOOK_EVENT_CONVERSATION_UPDATED],
84+
'url': 'https://example.com/webhook',
85+
'status': 'enabled'
86+
}
87+
web_hook = Client('', http_client).conversation_update_webhook('webhook-id', webhookRequestData)
88+
http_client.request.assert_called_once_with('webhooks/webhook-id', 'PATCH', webhookRequestData)

0 commit comments

Comments
 (0)