forked from microsoft/BotBuilder-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_dialog.py
342 lines (280 loc) · 12.9 KB
/
main_dialog.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
from typing import List
from botbuilder.dialogs import (
ComponentDialog,
DialogContext,
WaterfallDialog,
WaterfallStepContext,
DialogTurnResult,
)
from botbuilder.dialogs.choices import Choice, FoundChoice
from botbuilder.dialogs.prompts import (
PromptOptions,
ChoicePrompt,
PromptValidatorContext,
)
from botbuilder.dialogs.skills import (
SkillDialogOptions,
SkillDialog,
BeginSkillDialogOptions,
)
from botbuilder.core import ConversationState, MessageFactory, TurnContext
from botbuilder.core.skills import BotFrameworkSkill, ConversationIdFactoryBase
from botbuilder.schema import Activity, ActivityTypes, InputHints
from botbuilder.integration.aiohttp.skills import SkillHttpClient
from config import SkillConfiguration, DefaultConfig
class MainDialog(ComponentDialog):
"""
The main dialog for this bot. It uses a SkillDialog to call skills.
"""
ACTIVE_SKILL_PROPERTY_NAME = f"MainDialog.ActiveSkillProperty"
def __init__(
self,
conversation_state: ConversationState,
conversation_id_factory: ConversationIdFactoryBase,
skill_client: SkillHttpClient,
skills_config: SkillConfiguration,
configuration: DefaultConfig,
):
super(MainDialog, self).__init__(MainDialog.__name__)
# Constants used for selecting actions on the skill.
self._skill_action_book_flight = "BookFlight"
self._skill_action_book_flight_with_input_parameters = (
"BookFlight with input parameters"
)
self._skill_action_get_weather = "GetWeather"
self._skill_action_message = "Message"
self._selected_skill_key = (
f"{MainDialog.__module__}.{MainDialog.__name__}.SelectedSkillKey"
)
bot_id = configuration.APP_ID
self._skills_config = skills_config
if not self._skills_config:
raise TypeError("Skills configuration cannot be None")
if not skill_client:
raise TypeError("skill_client cannot be None")
if not conversation_state:
raise TypeError("conversation_state cannot be None")
# Use helper method to add SkillDialog instances for the configured skills.
self._add_skill_dialogs(
conversation_state,
conversation_id_factory,
skill_client,
skills_config,
bot_id,
)
# Add ChoicePrompt to render available skills.
self.add_dialog(ChoicePrompt("SkillPrompt"))
# Add ChoicePrompt to render skill actions.
self.add_dialog(
ChoicePrompt("SkillActionPrompt", self._skill_action_prompt_validator)
)
# Add main waterfall dialog for this bot.
self.add_dialog(
WaterfallDialog(
WaterfallDialog.__name__,
[
self._select_skill_step,
self._select_skill_action_step,
self._call_skill_action_step,
self._final_step,
],
)
)
# Create state property to track the active skill.
self._active_skill_property = conversation_state.create_property(
MainDialog.ACTIVE_SKILL_PROPERTY_NAME
)
# The initial child Dialog to run.
self.initial_dialog_id = WaterfallDialog.__name__
async def on_continue_dialog(self, inner_dc: DialogContext) -> DialogTurnResult:
# This is an example on how to cancel a SkillDialog that is currently in progress from the parent bot.
active_skill = await self._active_skill_property.get(inner_dc.context)
activity = inner_dc.context.activity
if (
active_skill
and activity.type == ActivityTypes.message
and "abort" in activity.text
):
# Cancel all dialogs when the user says abort.
# The SkillDialog automatically sends an EndOfConversation message to the skill to let the
# skill know that it needs to end its current dialogs, too.
await inner_dc.cancel_all_dialogs()
return await inner_dc.replace_dialog(self.initial_dialog_id)
return await super().on_continue_dialog(inner_dc)
async def _select_skill_step(
self, step_context: WaterfallStepContext
) -> DialogTurnResult:
"""
Render a prompt to select the skill to call.
"""
# Create the PromptOptions from the skill configuration which contain the list of configured skills.
message_text = (
str(step_context.options)
if step_context.options
else "What skill would you like to call?"
)
reprompt_text = "That was not a valid choice, please select a valid skill."
options = PromptOptions(
prompt=MessageFactory.text(message_text),
retry_prompt=MessageFactory.text(reprompt_text),
choices=[
Choice(value=skill.id)
for _, skill in self._skills_config.SKILLS.items()
],
)
# Prompt the user to select a skill.
return await step_context.prompt("SkillPrompt", options)
async def _select_skill_action_step(
self, step_context: WaterfallStepContext
) -> DialogTurnResult:
"""
Render a prompt to select the action for the skill.
"""
# Get the skill info based on the selected skill.
selected_skill_id = step_context.result.value
selected_skill = self._skills_config.SKILLS.get(selected_skill_id)
# Remember the skill selected by the user.
step_context.values[self._selected_skill_key] = selected_skill
# Create the PromptOptions with the actions supported by the selected skill.
message_text = (
f"Select an action # to send to **{selected_skill.id}** or just type in a message "
f"and it will be forwarded to the skill"
)
options = PromptOptions(
prompt=MessageFactory.text(
message_text, message_text, InputHints.expecting_input
),
choices=self._get_skill_actions(selected_skill),
)
# Prompt the user to select a skill action.
return await step_context.prompt("SkillActionPrompt", options)
async def _call_skill_action_step(
self, step_context: WaterfallStepContext
) -> DialogTurnResult:
"""
Starts the SkillDialog based on the user's selections.
"""
selected_skill: BotFrameworkSkill = step_context.values[
self._selected_skill_key
]
if selected_skill.id == "DialogSkillBot":
skill_activity = self._create_dialog_skill_bot_activity(
step_context.result.value, step_context.context
)
else:
raise Exception(f"Unknown target skill id: {selected_skill.id}.")
# Create the BeginSkillDialogOptions and assign the activity to send.
skill_dialog_args = BeginSkillDialogOptions(skill_activity)
# Save active skill in state.
await self._active_skill_property.set(step_context.context, selected_skill)
# Start the skillDialog instance with the arguments.
return await step_context.begin_dialog(selected_skill.id, skill_dialog_args)
async def _final_step(self, step_context: WaterfallStepContext) -> DialogTurnResult:
"""
The SkillDialog has ended, render the results (if any) and restart MainDialog.
"""
active_skill = await self._active_skill_property.get(step_context.context)
if step_context.result:
message = f"Skill {active_skill.id} invocation complete."
message += f" Result: {step_context.result}"
await step_context.context.send_activity(
MessageFactory.text(message, input_hint=InputHints.ignoring_input)
)
# Clear the skill selected by the user.
step_context.values[self._selected_skill_key] = None
# Clear active skill in state.
await self._active_skill_property.delete(step_context.context)
# Restart the main dialog with a different message the second time around
return await step_context.replace_dialog(
self.initial_dialog_id,
f'Done with "{active_skill.id}". \n\n What skill would you like to call?',
)
def _add_skill_dialogs(
self,
conversation_state: ConversationState,
conversation_id_factory: ConversationIdFactoryBase,
skill_client: SkillHttpClient,
skills_config: SkillConfiguration,
bot_id: str,
):
"""
Helper method that creates and adds SkillDialog instances for the configured skills.
"""
for _, skill_info in skills_config.SKILLS.items():
# Create the dialog options.
skill_dialog_options = SkillDialogOptions(
bot_id=bot_id,
conversation_id_factory=conversation_id_factory,
skill_client=skill_client,
skill_host_endpoint=skills_config.SKILL_HOST_ENDPOINT,
conversation_state=conversation_state,
skill=skill_info,
)
# Add a SkillDialog for the selected skill.
self.add_dialog(SkillDialog(skill_dialog_options, skill_info.id))
async def _skill_action_prompt_validator(
self, prompt_context: PromptValidatorContext
) -> bool:
"""
This validator defaults to Message if the user doesn't select an existing option.
"""
if not prompt_context.recognized.succeeded:
# Assume the user wants to send a message if an item in the list is not selected.
prompt_context.recognized.value = FoundChoice(
self._skill_action_message, None, None
)
return True
def _get_skill_actions(self, skill: BotFrameworkSkill) -> List[Choice]:
"""
Helper method to create Choice elements for the actions supported by the skill.
"""
# Note: the bot would probably render this by reading the skill manifest.
# We are just using hardcoded skill actions here for simplicity.
choices = []
if skill.id == "DialogSkillBot":
choices.append(Choice(self._skill_action_book_flight))
choices.append(Choice(self._skill_action_book_flight_with_input_parameters))
choices.append(Choice(self._skill_action_get_weather))
return choices
def _create_dialog_skill_bot_activity(
self, selected_option: str, turn_context: TurnContext
) -> Activity:
"""
Helper method to create the activity to be sent to the DialogSkillBot using selected type and values.
"""
selected_option = selected_option.lower()
# Note: in a real bot, the dialogArgs will be created dynamically based on the conversation
# and what each action requires; here we hardcode the values to make things simpler.
# Just forward the message activity to the skill with whatever the user said.
if selected_option == self._skill_action_message.lower():
# Note message activities also support input parameters but we are not using them in this example.
return turn_context.activity
activity = None
# Send an event activity to the skill with "BookFlight" in the name.
if selected_option == self._skill_action_book_flight.lower():
activity = Activity(type=ActivityTypes.event)
activity.name = self._skill_action_book_flight
# Send an event activity to the skill with "BookFlight" in the name and some testing values.
if (
selected_option
== self._skill_action_book_flight_with_input_parameters.lower()
):
activity = Activity(type=ActivityTypes.event)
activity.name = self._skill_action_book_flight
activity.value = {"origin": "New York", "destination": "Seattle"}
# Send an event activity to the skill with "GetWeather" in the name and some testing values.
if selected_option == self._skill_action_get_weather.lower():
activity = Activity(type=ActivityTypes.event)
activity.name = self._skill_action_get_weather
activity.value = {"latitude": 47.614891, "longitude": -122.195801}
return activity
if not activity:
raise Exception(f"Unable to create dialogArgs for {selected_option}.")
# We are manually creating the activity to send to the skill; ensure we add the ChannelData and Properties
# from the original activity so the skill gets them.
# Note: this is not necessary if we are just forwarding the current activity from context.
activity.channel_data = turn_context.activity.channel_data
activity.additional_properties = turn_context.activity.additional_properties
return activity