5
5
import sys
6
6
from copy import deepcopy , copy
7
7
from uuid import uuid4
8
- from typing import List , Callable , Iterable , Tuple
8
+ from typing import List , Callable , Iterable , Union
9
9
from botbuilder .schema import Activity , ActivityTypes , ConversationReference , ResourceResponse
10
10
11
11
@@ -15,7 +15,7 @@ def __init__(self, adapter_or_context, request: Activity=None):
15
15
adapter_or_context .copy_to (self )
16
16
else :
17
17
self .adapter = adapter_or_context
18
- self .activity = request
18
+ self ._activity = request
19
19
self .responses : List [Activity ] = []
20
20
self ._services : dict = {}
21
21
self ._on_send_activities : Callable [[]] = []
@@ -33,14 +33,25 @@ def copy_to(self, context: 'BotContext') -> None:
33
33
'_on_send_activities' , '_on_update_activity' , '_on_delete_activity' ]:
34
34
setattr (context , attribute , getattr (self , attribute ))
35
35
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
+
36
47
@property
37
48
def responded (self ):
38
49
return self ._responded ['responded' ]
39
50
40
51
@responded .setter
41
52
def responded (self , value ):
42
53
if not value :
43
- raise ValueError ('BotContext.responded() : cannot set BotContext.responded to False.' )
54
+ raise ValueError ('BotContext: cannot set BotContext.responded to False.' )
44
55
else :
45
56
self ._responded ['responded' ] = True
46
57
@@ -74,7 +85,7 @@ def set(self, key: str, value: object) -> None:
74
85
75
86
self ._services [key ] = value
76
87
77
- async def send_activity (self , * activity_or_text : Tuple [Activity , str ]):
88
+ async def send_activity (self , * activity_or_text : Union [Activity , str ]):
78
89
reference = BotContext .get_conversation_reference (self .activity )
79
90
output = [BotContext .apply_conversation_reference (
80
91
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]):
83
94
activity .input_hint = 'acceptingInput'
84
95
85
96
async def callback (context : 'BotContext' , output ):
86
- responses = await context .adapter .send_activity (context , output )
97
+ responses = await context .adapter .send_activities (context , output )
87
98
context ._responded = True
88
99
return responses
89
100
@@ -92,7 +103,7 @@ async def callback(context: 'BotContext', output):
92
103
async def update_activity (self , activity : Activity ):
93
104
return await self ._emit (self ._on_update_activity , activity , self .adapter .update_activity (self , activity ))
94
105
95
- async def delete_activity (self , reference : ConversationReference ):
106
+ async def delete_activity (self , reference : Union [ str , ConversationReference ] ):
96
107
return await self ._emit (self ._on_delete_activity , reference , self .adapter .delete_activity (self , reference ))
97
108
98
109
def on_send_activities (self , handler ) -> 'BotContext' :
@@ -122,14 +133,14 @@ def on_delete_activity(self, handler) -> 'BotContext':
122
133
self ._on_delete_activity .append (handler )
123
134
return self
124
135
125
- @staticmethod
126
- async def _emit (plugins , arg , logic ):
136
+ async def _emit (self , plugins , arg , logic ):
127
137
handlers = copy (plugins )
128
138
129
139
async def emit_next (i : int ):
140
+ context = self
130
141
try :
131
142
if i < len (handlers ):
132
- await handlers [i ](arg , emit_next (i + 1 ))
143
+ await handlers [i ](context , arg , emit_next (i + 1 ))
133
144
asyncio .ensure_future (logic )
134
145
except Exception as e :
135
146
raise e
@@ -139,7 +150,7 @@ async def emit_next(i: int):
139
150
def get_conversation_reference (activity : Activity ) -> ConversationReference :
140
151
"""
141
152
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.
143
154
144
155
Usage Example:
145
156
reference = BotContext.get_conversation_reference(context.request)
0 commit comments