File size: 13,028 Bytes
0827183
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

from abc import abstractmethod
import copy
from typing import Dict, List
from botbuilder.core.turn_context import TurnContext
from botbuilder.schema import InputHints, ActivityTypes
from botbuilder.dialogs.choices import (
    Choice,
    ChoiceFactory,
    ChoiceFactoryOptions,
    ListStyle,
)
from botbuilder.schema import Activity
from .prompt_options import PromptOptions
from .prompt_validator_context import PromptValidatorContext
from ..dialog_reason import DialogReason
from ..dialog import Dialog
from ..dialog_instance import DialogInstance
from ..dialog_turn_result import DialogTurnResult
from ..dialog_context import DialogContext


class Prompt(Dialog):
    """

    Defines the core behavior of prompt dialogs. Extends the :class:`Dialog` base class.

    .. remarks::
        When the prompt ends, it returns an object that represents the value it was prompted for.
        Use :meth:`DialogSet.add()` or :meth:`ComponentDialog.add_dialog()` to add a prompt to
        a dialog set or component dialog, respectively.

        Use :meth:`DialogContext.prompt()` or :meth:`DialogContext.begin_dialog()` to start the prompt.
        If you start a prompt from a :class:`WaterfallStep` in a :class:`WaterfallDialog`, then the
        prompt result will be available in the next step of the waterfall.
    """

    ATTEMPT_COUNT_KEY = "AttemptCount"
    persisted_options = "options"
    persisted_state = "state"

    def __init__(self, dialog_id: str, validator: object = None):
        """
        Creates a new :class:`Prompt` instance.

        :param dialog_id: Unique Id of the prompt within its parent :class:`DialogSet`
        :class:`ComponentDialog`
        :type dialog_id: str
        :param validator: Optionally provide additional validation and re-prompting logic
        :type validator: Object
        """
        super(Prompt, self).__init__(dialog_id)

        self._validator = validator

    async def begin_dialog(
        self, dialog_context: DialogContext, options: object = None
    ) -> DialogTurnResult:
        """
        Starts a prompt dialog. Called when a prompt dialog is pushed onto the dialog stack and is being activated.

        :param dialog_context: The dialog context for the current turn of the conversation
        :type dialog_context:  :class:`DialogContext`
        :param options: Optional, additional information to pass to the prompt being started
        :type options: Object
        :return: The dialog turn result
        :rtype: :class:`DialogTurnResult`

        .. note::
            The result indicates whether the prompt is still active after the turn has been processed.
        """
        if not dialog_context:
            raise TypeError("Prompt(): dc cannot be None.")
        if not isinstance(options, PromptOptions):
            raise TypeError("Prompt(): Prompt options are required for Prompt dialogs.")
        # Ensure prompts have input hint set
        if options.prompt is not None and not options.prompt.input_hint:
            options.prompt.input_hint = InputHints.expecting_input

        if options.retry_prompt is not None and not options.retry_prompt.input_hint:
            options.retry_prompt.input_hint = InputHints.expecting_input

        # Initialize prompt state
        state = dialog_context.active_dialog.state
        state[self.persisted_options] = options
        state[self.persisted_state] = {}

        # Send initial prompt
        await self.on_prompt(
            dialog_context.context,
            state[self.persisted_state],
            state[self.persisted_options],
            False,
        )

        return Dialog.end_of_turn

    async def continue_dialog(self, dialog_context: DialogContext):
        """
        Continues a dialog.

        :param dialog_context: The dialog context for the current turn of the conversation
        :type dialog_context:  :class:`DialogContext`
        :return: The dialog turn result
        :rtype: :class:`DialogTurnResult`

        .. remarks::
            Called when a prompt dialog is the active dialog and the user replied with a new activity.

            If the task is successful, the result indicates whether the dialog is still active after
            the turn has been processed by the dialog.

            The prompt generally continues to receive the user's replies until it accepts the
            user's reply as valid input for the prompt.
        """
        if not dialog_context:
            raise TypeError("Prompt(): dc cannot be None.")

        # Don't do anything for non-message activities
        if dialog_context.context.activity.type != ActivityTypes.message:
            return Dialog.end_of_turn

        # Perform base recognition
        instance = dialog_context.active_dialog
        state = instance.state[self.persisted_state]
        options = instance.state[self.persisted_options]
        recognized = await self.on_recognize(dialog_context.context, state, options)

        # Validate the return value
        is_valid = False
        if self._validator is not None:
            prompt_context = PromptValidatorContext(
                dialog_context.context, recognized, state, options
            )
            is_valid = await self._validator(prompt_context)
            if options is None:
                options = PromptOptions()
            options.number_of_attempts += 1
        else:
            if recognized.succeeded:
                is_valid = True
        # Return recognized value or re-prompt
        if is_valid:
            return await dialog_context.end_dialog(recognized.value)

        if not dialog_context.context.responded:
            await self.on_prompt(dialog_context.context, state, options, True)
        return Dialog.end_of_turn

    async def resume_dialog(
        self, dialog_context: DialogContext, reason: DialogReason, result: object
    ) -> DialogTurnResult:
        """
        Resumes a dialog.

        :param dialog_context: The dialog context for the current turn of the conversation.
        :type dialog_context:  :class:`DialogContext`
        :param reason: An enum indicating why the dialog resumed.
        :type reason:  :class:`DialogReason`
        :param result: Optional, value returned from the previous dialog on the stack.
        :type result:  object
        :return: The dialog turn result
        :rtype: :class:`DialogTurnResult`

        .. remarks::
            Called when a prompt dialog resumes being the active dialog on the dialog stack,
            such as when the previous active dialog on the stack completes.

            If the task is successful, the result indicates whether the dialog is still
            active after the turn has been processed by the dialog.

            Prompts are typically leaf nodes on the stack but the dev is free to push other dialogs
            on top of the stack which will result in the prompt receiving an unexpected call to
            :meth:resume_dialog() when the pushed on dialog ends.

            Simply re-prompt the user to avoid that the prompt ends prematurely.

        """
        await self.reprompt_dialog(dialog_context.context, dialog_context.active_dialog)
        return Dialog.end_of_turn

    async def reprompt_dialog(self, context: TurnContext, instance: DialogInstance):
        """
        Reprompts user for input.

        :param context: Context for the current turn of conversation with the user
        :type context:  :class:`botbuilder.core.TurnContext`
        :param instance: The instance of the dialog on the stack
        :type instance:  :class:`DialogInstance`
        :return: A task representing the asynchronous operation

        """
        state = instance.state[self.persisted_state]
        options = instance.state[self.persisted_options]
        await self.on_prompt(context, state, options, False)

    @abstractmethod
    async def on_prompt(
        self,
        turn_context: TurnContext,
        state: Dict[str, object],
        options: PromptOptions,
        is_retry: bool,
    ):
        """
        Prompts user for input. When overridden in a derived class, prompts the user for input.

        :param turn_context: Context for the current turn of conversation with the user
        :type turn_context:  :class:`botbuilder.core.TurnContext`
        :param state: Contains state for the current instance of the prompt on the dialog stack
        :type state:  :class:`Dict`
        :param options: A prompt options object constructed from:meth:`DialogContext.prompt()`
        :type options:  :class:`PromptOptions`
        :param is_retry: Determines whether `prompt` or `retry_prompt` should be used
        :type is_retry:  bool

        :return: A task representing the asynchronous operation.

        """

    @abstractmethod
    async def on_recognize(
        self,
        turn_context: TurnContext,
        state: Dict[str, object],
        options: PromptOptions,
    ):
        """
        Recognizes the user's input.

        :param turn_context: Context for the current turn of conversation with the user
        :type turn_context:  :class:`botbuilder.core.TurnContext`
        :param state: Contains state for the current instance of the prompt on the dialog stack
        :type state:  :class:`Dict`
        :param options: A prompt options object constructed from :meth:`DialogContext.prompt()`
        :type options:  :class:`PromptOptions`

        :return: A task representing the asynchronous operation.

        .. note::
            When overridden in a derived class, attempts to recognize the user's input.
        """

    def append_choices(
        self,
        prompt: Activity,
        channel_id: str,
        choices: List[Choice],
        style: ListStyle,
        options: ChoiceFactoryOptions = None,
    ) -> Activity:
        """
        Composes an output activity containing a set of choices.

        :param prompt: The prompt to append the user's choice to
        :type prompt:
        :param channel_id: Id of the channel the prompt is being sent to
        :type channel_id: str
        :param: choices: List of choices to append
        :type choices:  :class:`List`
        :param: style: Configured style for the list of choices
        :type style:  :class:`ListStyle`
        :param: options: Optional formatting options to use when presenting the choices
        :type style: :class:`ChoiceFactoryOptions`

        :return: A task representing the asynchronous operation

        .. remarks::
            If the task is successful, the result contains the updated activity.
            When overridden in a derived class, appends choices to the activity when the user
            is prompted for input. This is an helper function to compose an output activity
            containing a set of choices.

        """
        # Get base prompt text (if any)
        text = prompt.text if prompt is not None and prompt.text else ""

        # Create temporary msg
        # TODO: fix once ChoiceFactory complete
        def inline() -> Activity:
            return ChoiceFactory.inline(choices, text, None, options)

        def list_style() -> Activity:
            return ChoiceFactory.list_style(choices, text, None, options)

        def suggested_action() -> Activity:
            return ChoiceFactory.suggested_action(choices, text)

        def hero_card() -> Activity:
            return ChoiceFactory.hero_card(choices, text)

        def list_style_none() -> Activity:
            activity = Activity(type=ActivityTypes.message)
            activity.text = text
            return activity

        def default() -> Activity:
            return ChoiceFactory.for_channel(channel_id, choices, text, None, options)

        # Maps to values in ListStyle Enum
        switcher = {
            0: list_style_none,
            1: default,
            2: inline,
            3: list_style,
            4: suggested_action,
            5: hero_card,
        }

        msg = switcher.get(int(style.value), default)()

        # Update prompt with text, actions and attachments
        if prompt:
            # clone the prompt the set in the options (note ActivityEx has Properties so this is the safest mechanism)
            prompt = copy.copy(prompt)

            prompt.text = msg.text

            if (
                msg.suggested_actions is not None
                and msg.suggested_actions.actions is not None
                and msg.suggested_actions.actions
            ):
                prompt.suggested_actions = msg.suggested_actions

            if msg.attachments:
                if prompt.attachments:
                    prompt.attachments.extend(msg.attachments)
                else:
                    prompt.attachments = msg.attachments

            return prompt

        # TODO: Update to InputHints.ExpectingInput;
        msg.input_hint = None
        return msg