Skip to content

Commit 267afb8

Browse files
authored
Merge pull request #619 from microsoft/mm-api-ref-docs-activity-handler
mm-api-ref-docs-activity-handler
2 parents 5d9441e + 5e36083 commit 267afb8

File tree

1 file changed

+204
-1
lines changed

1 file changed

+204
-1
lines changed

libraries/botbuilder-core/botbuilder/core/activity_handler.py

Lines changed: 204 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,24 @@
88

99
class ActivityHandler:
1010
async def on_turn(self, turn_context: TurnContext):
11+
"""
12+
Called by the adapter (for example, :class:`BotFrameworkAdapter`) at runtime
13+
in order to process an inbound :class:`botbuilder.schema.Activity`.
14+
15+
:param turn_context: The context object for this turn
16+
:type turn_context: :class:`TurnContext`
17+
18+
:returns: A task that represents the work queued to execute
19+
20+
.. remarks::
21+
It calls other methods in this class based on the type of the activity to
22+
process, which allows a derived class to provide type-specific logic in a controlled way.
23+
In a derived class, override this method to add logic that applies to all activity types.
24+
25+
.. note::
26+
- Add logic to apply before the type-specific logic and before the call to the :meth:`ActivityHandler.on_turn()` method.
27+
- Add logic to apply after the type-specific logic after the call to the :meth:`ActivityHandler.on_turn()` method.
28+
"""
1129
if turn_context is None:
1230
raise TypeError("ActivityHandler.on_turn(): turn_context cannot be None.")
1331

@@ -40,9 +58,36 @@ async def on_turn(self, turn_context: TurnContext):
4058
async def on_message_activity( # pylint: disable=unused-argument
4159
self, turn_context: TurnContext
4260
):
61+
"""
62+
Override this method in a derived class to provide logic specific to activities,
63+
such as the conversational logic.
64+
65+
:param turn_context: The context object for this turn
66+
:type turn_context: :class:`TurnContext`
67+
68+
:returns: A task that represents the work queued to execute
69+
70+
"""
4371
return
4472

4573
async def on_conversation_update_activity(self, turn_context: TurnContext):
74+
"""
75+
Invoked when a conversation update activity is received from the channel when the base behavior of :meth:`ActivityHandler.on_turn()` is used.
76+
77+
:param turn_context: The context object for this turn
78+
:type turn_context: :class:`TurnContext`
79+
80+
:returns: A task that represents the work queued to execute
81+
82+
.. note::
83+
When the :meth:'ActivityHandler.on_turn()` method receives a conversation update activity, it calls this method.
84+
If the conversation update activity indicates that members other than the bot joined the conversation,
85+
it calls the :meth:`ActivityHandler.on_members_added_activity()` method.
86+
If the conversation update activity indicates that members other than the bot left the conversation,
87+
it calls the :meth:`ActivityHandler.on_members_removed_activity()` method.
88+
In a derived class, override this method to add logic that applies to all conversation update activities.
89+
Add logic to apply before the member added or removed logic before the call to this base class method.
90+
"""
4691
if (
4792
turn_context.activity.members_added is not None
4893
and turn_context.activity.members_added
@@ -61,15 +106,67 @@ async def on_conversation_update_activity(self, turn_context: TurnContext):
61106

62107
async def on_members_added_activity(
63108
self, members_added: List[ChannelAccount], turn_context: TurnContext
64-
): # pylint: disable=unused-argument
109+
): # pylint: disable=unused-argument
110+
"""
111+
Override this method in a derived class to provide logic for when members other than the bot join the conversation.
112+
You can add your bot's welcome logic.
113+
114+
:param members_added: A list of all the members added to the conversation, as described by the conversation update activity
115+
:type members_added: :class:`typing.List`
116+
:param turn_context: The context object for this turn
117+
:type turn_context: :class:`TurnContext`
118+
119+
:returns: A task that represents the work queued to execute
120+
121+
.. note::
122+
When the :meth:'ActivityHandler.on_conversation_update_activity()` method receives a conversation update activity that indicates
123+
one or more users other than the bot are joining the conversation, it calls this method.
124+
"""
65125
return
66126

67127
async def on_members_removed_activity(
68128
self, members_removed: List[ChannelAccount], turn_context: TurnContext
69129
): # pylint: disable=unused-argument
130+
"""
131+
Override this method in a derived class to provide logic for when members other than the bot leave the conversation.
132+
You can add your bot's good-bye logic.
133+
134+
:param members_added: A list of all the members removed from the conversation, as described by the conversation update activity
135+
:type members_added: :class:`typing.List`
136+
:param turn_context: The context object for this turn
137+
:type turn_context: :class:`TurnContext`
138+
139+
:returns: A task that represents the work queued to execute
140+
141+
.. note::
142+
When the :meth:'ActivityHandler.on_conversation_update_activity()` method receives a conversation update activity that indicates
143+
one or more users other than the bot are leaving the conversation, it calls this method.
144+
"""
145+
70146
return
71147

72148
async def on_message_reaction_activity(self, turn_context: TurnContext):
149+
"""
150+
Invoked when an event activity is received from the connector when the base behavior of :meth:'ActivityHandler.on_turn()` is used.
151+
152+
153+
:param turn_context: The context object for this turn
154+
:type turn_context: :class:`TurnContext`
155+
156+
:returns: A task that represents the work queued to execute
157+
158+
.. note::
159+
Message reactions correspond to the user adding a 'like' or 'sad' etc. (often an emoji) to a previously sent activity.
160+
Message reactions are only supported by a few channels. The activity that the message reaction corresponds to is indicated in the
161+
reply to Id property. The value of this property is the activity id of a previously sent activity given back to the bot as the response
162+
from a send call.
163+
When the :meth:'ActivityHandler.on_turn()` method receives a message reaction activity, it calls this method.
164+
If the message reaction indicates that reactions were added to a message, it calls :meth:'ActivityHandler.on_reaction_added().
165+
If the message reaction indicates that reactions were removed from a message, it calls :meth:'ActivityHandler.on_reaction_removed().
166+
In a derived class, override this method to add logic that applies to all message reaction activities.
167+
Add logic to apply before the reactions added or removed logic before the call to the this base class method.
168+
Add logic to apply after the reactions added or removed logic after the call to the this base class method.
169+
"""
73170
if turn_context.activity.reactions_added is not None:
74171
await self.on_reactions_added(
75172
turn_context.activity.reactions_added, turn_context
@@ -83,14 +180,71 @@ async def on_message_reaction_activity(self, turn_context: TurnContext):
83180
async def on_reactions_added( # pylint: disable=unused-argument
84181
self, message_reactions: List[MessageReaction], turn_context: TurnContext
85182
):
183+
"""
184+
Override this method in a derived class to provide logic for when reactions to a previous activity
185+
are added to the conversation.
186+
187+
:param message_reactions: The list of reactions added
188+
:type message_reactions: :class:`typing.List`
189+
:param turn_context: The context object for this turn
190+
:type turn_context: :class:`TurnContext`
191+
192+
:returns: A task that represents the work queued to execute
193+
194+
.. note::
195+
Message reactions correspond to the user adding a 'like' or 'sad' etc. (often an emoji)
196+
to a previously sent message on the conversation. Message reactions are supported by only a few channels.
197+
The activity that the message is in reaction to is identified by the activity's reply to Id property.
198+
The value of this property is the activity ID of a previously sent activity. When the bot sends an activity,
199+
the channel assigns an ID to it, which is available in the resource response Id of the result.
200+
"""
86201
return
87202

88203
async def on_reactions_removed( # pylint: disable=unused-argument
89204
self, message_reactions: List[MessageReaction], turn_context: TurnContext
90205
):
206+
"""
207+
Override this method in a derived class to provide logic for when reactions to a previous activity
208+
are removed from the conversation.
209+
210+
:param message_reactions: The list of reactions removed
211+
:type message_reactions: :class:`typing.List`
212+
:param turn_context: The context object for this turn
213+
:type turn_context: :class:`TurnContext`
214+
215+
:returns: A task that represents the work queued to execute
216+
217+
.. note::
218+
Message reactions correspond to the user adding a 'like' or 'sad' etc. (often an emoji)
219+
to a previously sent message on the conversation. Message reactions are supported by only a few channels.
220+
The activity that the message is in reaction to is identified by the activity's reply to Id property.
221+
The value of this property is the activity ID of a previously sent activity. When the bot sends an activity,
222+
the channel assigns an ID to it, which is available in the resource response Id of the result.
223+
"""
91224
return
92225

93226
async def on_event_activity(self, turn_context: TurnContext):
227+
"""
228+
Invoked when an event activity is received from the connector when the base behavior of :meth:'ActivityHandler.on_turn()` is used.
229+
230+
:param turn_context: The context object for this turn
231+
:type turn_context: :class:`TurnContext`
232+
233+
:returns: A task that represents the work queued to execute
234+
235+
.. note::
236+
When the :meth:'ActivityHandler.on_turn()` method receives an event activity, it calls this method.
237+
If the activity name is `tokens/response`, it calls :meth:'ActivityHandler.on_token_response_event()`;
238+
otherwise, it calls :meth:'ActivityHandler.on_event()`.
239+
240+
In a derived class, override this method to add logic that applies to all event activities.
241+
Add logic to apply before the specific event-handling logic before the call to this base class method.
242+
Add logic to apply after the specific event-handling logic after the call to this base class method.
243+
244+
Event activities communicate programmatic information from a client or channel to a bot.
245+
The meaning of an event activity is defined by the event activity name property, which is meaningful within
246+
the scope of a channel.
247+
"""
94248
if turn_context.activity.name == "tokens/response":
95249
return await self.on_token_response_event(turn_context)
96250

@@ -99,19 +253,68 @@ async def on_event_activity(self, turn_context: TurnContext):
99253
async def on_token_response_event( # pylint: disable=unused-argument
100254
self, turn_context: TurnContext
101255
):
256+
"""
257+
Invoked when a `tokens/response` event is received when the base behavior of :meth:'ActivityHandler.on_event_activity()` is used.
258+
If using an `oauth_prompt`, override this method to forward this activity to the current dialog.
259+
260+
:param turn_context: The context object for this turn
261+
:type turn_context: :class:`TurnContext`
262+
263+
:returns: A task that represents the work queued to execute
264+
265+
.. note::
266+
When the :meth:'ActivityHandler.on_event()` method receives an event with an activity name of `tokens/response`,
267+
it calls this method. If your bot uses an `oauth_prompt`, forward the incoming activity to the current dialog.
268+
"""
102269
return
103270

104271
async def on_event( # pylint: disable=unused-argument
105272
self, turn_context: TurnContext
106273
):
274+
"""
275+
Invoked when an event other than `tokens/response` is received when the base behavior of
276+
:meth:'ActivityHandler.on_event_activity()` is used.
277+
278+
279+
:param turn_context: The context object for this turn
280+
:type turn_context: :class:`TurnContext`
281+
282+
:returns: A task that represents the work queued to execute
283+
284+
.. note::
285+
When the :meth:'ActivityHandler.on_event_activity()` is used method receives an event with an
286+
activity name other than `tokens/response`, it calls this method.
287+
This method could optionally be overridden if the bot is meant to handle miscellaneous events.
288+
"""
107289
return
108290

109291
async def on_end_of_conversation_activity( # pylint: disable=unused-argument
110292
self, turn_context: TurnContext
111293
):
294+
"""
295+
Invoked when a conversation end activity is received from the channel.
296+
297+
:param turn_context: The context object for this turn
298+
:type turn_context: :class:`TurnContext`
299+
:returns: A task that represents the work queued to execute
300+
"""
112301
return
113302

114303
async def on_unrecognized_activity_type( # pylint: disable=unused-argument
115304
self, turn_context: TurnContext
116305
):
306+
"""
307+
Invoked when an activity other than a message, conversation update, or event is received when the base behavior of
308+
:meth:`ActivityHandler.on_turn()` is used.
309+
If overridden, this method could potentially respond to any of the other activity types.
310+
311+
:param turn_context: The context object for this turn
312+
:type turn_context: :class:`TurnContext`
313+
314+
:returns: A task that represents the work queued to execute
315+
316+
.. note::
317+
When the :meth:`ActivityHandler.on_turn()` method receives an activity that is not a message, conversation update, message reaction,
318+
or event activity, it calls this method.
319+
"""
117320
return

0 commit comments

Comments
 (0)