8000 add handler-registering methods to BotContext · rsliang/botbuilder-python@c3f3588 · GitHub
[go: up one dir, main page]

Skip to content

Commit c3f3588

Browse files
committed
add handler-registering methods to BotContext
1 parent 708fea0 commit c3f3588

File tree

2 files changed

+63
-15
lines changed

2 files changed

+63
-15
lines changed

libraries/botbuilder-core/botbuilder/core/bot_context.py

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,39 @@
1010

1111

1212
class BotContext(object):
13-
def __init__(self, adapter, request: Activity):
14-
self.adapter = adapter
15-
self.activity = request
16-
self.responses: List[Activity] = []
17-
self._services: dict = {}
18-
self._on_send_activities: Callable[[]] = []
19-
self._on_update_activity: Callable[[]] = []
20-
self._on_delete_activity: Callable[[]] = []
21-
self.__responded: bool = False
13+
def __init__(self, adapter_or_context, request: Activity=None):
14+
if isinstance(adapter_or_context, BotContext):
15+
adapter_or_context.copy_to(self)
16+
else:
17+
self.adapter = adapter_or_context
18+
self.activity = request
19+
self.responses: List[Activity] = []
20+
self._services: dict = {}
21+
self._on_send_activities: Callable[[]] = []
22+
self._on_update_activity: Callable[[]] = []
23+
self._on_delete_activity: Callable[[]] = []
24+
self._responded = {'responded': False}
2225

2326
if self.adapter is None:
2427
raise TypeError('BotContext must be instantiated with an adapter.')
2528
if self.activity is None:
2629
raise TypeError('BotContext must be instantiated with a request parameter of type Activity.')
2730

2831
def copy_to(self, context: 'BotContext') -> None:
29-
for attribute in ['adapter', 'activity', 'responded', '_services',
32+
for attribute in ['adapter', 'activity', '_responded', '_services',
3033
'_on_send_activities', '_on_update_activity', '_on_delete_activity']:
3134
setattr(context, attribute, getattr(self, attribute))
3235

3336
@property
3437
def responded(self):
35-
return self.__responded
38+
return self._responded['responded']
3639

3740
@responded.setter
3841
def responded(self, value):
39-
if type(value) != bool or value is False:
42+
if not value:
4043
raise ValueError('BotContext.responded(): cannot set BotContext.responded to False.')
4144
else:
42-
self.__responded = value
45+
self._responded['responded'] = True
4346

4447
def get(self, key: str) -> object:
4548
if not key or not isinstance(key, str):
@@ -92,6 +95,33 @@ async def update_activity(self, activity: Activity):
9295
async def delete_activity(self, reference: ConversationReference):
9396
return await self._emit(self._on_delete_activity, reference, self.adapter.delete_activity(self, reference))
9497

98+
def on_send_activities(self, handler) -> 'BotContext':
99+
"""
100+
Registers a handler to be notified of and potentially intercept the sending of activities.
101+
:param handler:
102+
:return:
103+
"""
104+
self._on_send_activities.append(handler)
105+
return self
106+
107+
def on_update_activity(self, handler) -> 'BotContext':
108+
"""
109+
Registers a handler to be notified of and potentially intercept an activity being updated.
110+
:param handler:
111+
:return:
112+
"""
113+
self._on_update_activity.append(handler)
114+
return self
115+
116+
def on_delete_activity(self, handler) -> 'BotContext':
117+
"""
118+
Registers a handler to be notified of and potentially intercept an activity being deleted.
119+
:param handler:
120+
:return:
121+
"""
122+
self._on_delete_activity.append(handler)
123+
return self
124+
95125
@staticmethod
96126
async def _emit(plugins, arg, logic):
97127
handlers = copy(plugins)

libraries/botbuilder-core/tests/test_bot_context.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,41 @@ def test_should_not_create_context_without_adapter(self):
3030
except Exception as e:
3131
raise e
3232

33-
def test_copy_to_should_copy_references(self):
33+
def test_should_create_context_with_older_context(self):
34+
context = BotContext(ADAPTER, ACTIVITY)
35+
new_context = BotContext(context)
36+
37+
def test_copy_to_should_copy_all_references(self):
3438
old_adapter = TestAdapter(None)
3539
old_activity = Activity(id='2', type='message', text='test copy')
3640
old_context = BotContext(old_adapter, old_activity)
3741
old_context.responded = True
3842

43+
async def send_activities_handler(context, next_handler):
44+
pass
45+
46+
old_context.on_send_activities(send_activities_handler)
47+
3948
adapter = TestAdapter(None)
4049
new_context = BotContext(adapter, ACTIVITY)
4150
old_context.copy_to(new_context)
4251

4352
assert new_context.adapter == old_adapter
4453
assert new_context.activity == old_activity
4554
assert new_context.responded is True
55+
assert len(new_context._on_send_activities) == 1
56+
assert len(new_context._on_update_activity) == 0
57+
assert len(new_context._on_delete_activity) == 0
58+
59+
def test_responded_should_be_automatically_set_to_False(self):
60+
context = BotContext(ADAPTER, ACTIVITY)
61+
assert context.responded is False
4662

47-
def test_should_not_be_able_to_set_responded_to_True(self):
63+
def test_should_be_able_to_set_responded_to_True(self):
4864
context = BotContext(ADAPTER, ACTIVITY)
65+
assert context.responded is False
4966
context.responded = True
67+
assert context.responded
5068

5169
def test_should_not_be_able_to_set_responded_to_False(self):
5270
context = BotContext(ADAPTER, ACTIVITY)

0 commit comments

Comments
 (0)
0