55import sys
66from copy import deepcopy , copy
77from uuid import uuid4
8- from typing import List , Callable , Iterable , Tuple
8+ from typing import List , Callable , Iterable , Union
99from 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)
0 commit comments