8000 add more BotContext tests, update BotAdapter.send_activity to BotAdap… · umarfarook882/botbuilder-python@95b5c03 · GitHub
[go: up one dir, main page]

Skip to content

Commit 95b5c03

Browse files
committed
add more BotContext tests, update BotAdapter.send_activity to BotAdapter.send_activities
1 parent c3f3588 commit 95b5c03

File tree

5 files changed

+217
-44
lines changed

5 files changed

+217
-44
lines changed

libraries/botbuilder-core/botbuilder/core/bot_adapter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def __init__(self):
1414
self._middleware = MiddlewareSet()
1515

1616
@abstractmethod
17-
async def send_activity(self, context: BotContext, activities: List[Activity]):
17+
async def send_activities(self, context: BotContext, activities: List[Activity]):
1818
"""
1919
Sends a set of activities to the user. An array of responses from the server will be returned.
2020
:param activities:

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

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import sys
66
from copy import deepcopy, copy
77
from uuid import uuid4
8-
from typing import List, Callable, Iterable, Tuple
8+
from typing import List, Callable, Iterable, Union
99
from botbuilder.schema import Activity, ActivityTypes, ConversationReference, ResourceResponse
1010

1111

@@ -15,7 +15,7 @@ def __init__(self, adapter_or_context, request: Activity=None):
1515
adapter_or_context.copy_to(self)
1616
else:
1717
self.adapter = adapter_or_context
18-
self.activity = request
18+
self._activity = request
1919
self.responses: List[Activity] = []
2020
self._services: dict = {}
2121
self._on_send_activities: Callable[[]] = []
@@ -33,14 +33,25 @@ def copy_to(self, context: 'BotContext') -> None:
3333
'_on_send_activities', '_on_update_activity', '_on_delete_activity']:
3434
setattr(context, attribute, getattr(self, attribute))
3535

36+
@property
37+
def activity(self):
38+
return self._activity
39+
40+
@activity.setter
41+
def activity(self, value):
42+
if not isinstance(value, Activity):
43+
raise TypeError('BotContext: cannot set `activity` to a type other than Activity.')
44+
else:
45+
self._activity = value
46+
3647
@property
3748
def responded(self):
3849
return self._responded['responded']
3950

4051
@responded.setter
4152
def responded(self, value):
4253
if not value:
43-
raise ValueError('BotContext.responded(): cannot set BotContext.responded to False.')
54+
raise ValueError('BotContext: cannot set BotContext.responded to False.')
4455
else:
4556
self._responded['responded'] = True
4657

@@ -74,7 +85,7 @@ def set(self, key: str, value: object) -> None:
7485

7586
self._services[key] = value
7687

77-
async def send_activity(self, *activity_or_text: Tuple[Activity, str]):
88+
async def send_activity(self, *activity_or_text: Union[Activity, str]):
7889
reference = BotContext.get_conversation_reference(self.activity)
7990
output = [BotContext.apply_conversation_reference(
8091
Activity(text=a, type='message') if isinstance(a, str) else a, reference)
@@ -83,7 +94,7 @@ async def send_activity(self, *activity_or_text: Tuple[Activity, str]):
8394
activity.input_hint = 'acceptingInput'
8495

8596
async def callback(context: 'BotContext', output):
86-
responses = await context.adapter.send_activity(context, output)
97+
responses = await context.adapter.send_activities(context, output)
8798
context._responded = True
8899
return responses
89100

@@ -92,7 +103,7 @@ async def callback(context: 'BotContext', output):
92103
async def update_activity(self, activity: Activity):
93104
return await self._emit(self._on_update_activity, activity, self.adapter.update_activity(self, activity))
94105

95-
async def delete_activity(self, reference: ConversationReference):
106+
async def delete_activity(self, reference: Union[str, ConversationReference]):
96107
return await self._emit(self._on_delete_activity, reference, self.adapter.delete_activity(self, reference))
97108

98109
def on_send_activities(self, handler) -> 'BotContext':
@@ -122,14 +133,14 @@ def on_delete_activity(self, handler) -> 'BotContext':
122133
self._on_delete_activity.append(handler)
123134
return self
124135

125-
@staticmethod
126-
async def _emit(plugins, arg, logic):
136+
async def _emit(self, plugins, arg, logic):
127137
handlers = copy(plugins)
128138

129139
async def emit_next(i: int):
140+
context = self
130141
try:
131142
if i < len(handlers):
132-
await handlers[i](arg, emit_next(i + 1))
143+
await handlers[i](context, arg, emit_next(i + 1))
133144
asyncio.ensure_future(logic)
134145
except Exception as e:
135146
raise e
@@ -139,7 +150,7 @@ async def emit_next(i: int):
139150
def get_conversation_reference(activity: Activity) -> ConversationReference:
140151
"""
141152
Returns the conversation reference for an activity. This can be saved as a plain old JSON
142-
bject and then later used to message the user proactively.
153+
object and then later used to message the user proactively.
143154
144155
Usage Example:
145156
reference = BotContext.get_conversation_reference(context.request)

libraries/botbuilder-core/botbuilder/core/bot_framework_adapter.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ async def delete_activity(self, context: BotContext, conversation_reference: Con
162162
except Exception as e:
163163
raise e
164164

165-
async def send_activity(self, context: BotContext, activities: List[Activity]):
165+
async def send_activities(self, context: BotContext, activities: List[Activity]):
166166
try:
167167
for activity in activities:
168168
if activity.type == 'delay':
@@ -188,13 +188,13 @@ async def delete_conversation_member(self, context: BotContext, member_id: str)
188188
:return:
189189
"""
190190
try:
191-
if not context.request.service_url:
191+
if not context.activity.service_url:
192192
raise TypeError('BotFrameworkAdapter.delete_conversation_member(): missing service_url')
193-
if not context.request.conversation or not context.request.conversation.id:
193+
if not context.activity.conversation or not context.activity.conversation.id:
194194
raise TypeError('BotFrameworkAdapter.delete_conversation_member(): missing conversation or '
195195
'conversation.id')
196-
service_url = context.request.service_url
197-
conversation_id = context.request.conversation.id
196+
service_url = context.activity.service_url
197+
conversation_id = context.activity.conversation.id
198198
client = self.create_connector_client(service_url)
199199
return await client.conversations.delete_conversation_member_async(conversation_id, member_id)
200200
except AttributeError as attr_e:
@@ -211,16 +211,16 @@ async def get_activity_members(self, context: BotContext, activity_id: str):
211211
"""
212212
try:
213213
if not activity_id:
214-
activity_id = context.request.id
215-
if not context.request.service_url:
214+
activity_id = context.activity.id
215+
if not context.activity.service_url:
216216
raise TypeError('BotFrameworkAdapter.get_activity_member(): missing service_url')
217-
if not context.request.conversation or not context.request.conversation.id:
217+
if not context.activity.conversation or not context.activity.conversation.id:
218218
raise TypeError('BotFrameworkAdapter.get_activity_member(): missing conversation or conversation.id')
219219
if not activity_id:
220220
raise TypeError('BotFrameworkAdapter.get_activity_member(): missing both activity_id and '
221221
'context.activity.id')
222-
service_url = context.request.service_url
223-
conversation_id = context.request.conversation.id
222+
service_url = context.activity.service_url
223+
conversation_id = context.activity.conversation.id
224224
client = self.create_connector_client(service_url)
225225
return await client.conversations.get_activity_members_async(conversation_id, activity_id)
226226
except Exception as e:
@@ -233,13 +233,13 @@ async def get_conversation_members(self, context: BotContext):
233233
:return:
234234
"""
235235
try:
236-
if not context.request.service_url:
236+
if not context.activity.service_url:
237237
raise TypeError('BotFrameworkAdapter.get_conversation_members(): missing service_url')
238-
if not context.request.conversation or not context.request.conversation.id:
238+
if not context.activity.conversation or not context.activity.conversation.id:
239239
raise TypeError('BotFrameworkAdapter.get_conversation_members(): missing conversation or '
240240
'conversation.id')
241-
service_url = context.request.service_url
242-
conversation_id = context.request.conversation.id
241+
service_url = context.activity.service_url
242+
conversation_id = context.activity.conversation.id
243243
client = self.create_connector_client(service_url)
244244
return await client.conversations.get_conversation_members_async(conversation_id)
245245
except Exception as e:

libraries/botbuilder-core/botbuilder/core/test_adapter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def __init__(self, logic: Coroutine, template: ConversationReference=None):
3737
self.template.conversation = template.conversation
3838
self.template.channel_id = template.channel_id
3939

40-
async def send_activity(self, context, activities: List[Activity]):
40+
async def send_activities(self, context, activities: List[Activity]):
4141
"""
4242
INTERNAL: called by the logic under test to send a set of activities. These will be buffered
4343
to the current `TestFlow` instance for comparison against the expected results.

0 commit comments

Comments
 (0)
0