UNPKG

2.11 kBPlain TextView Raw
1/**
2 * @module botkit
3 */
4/**
5 * Copyright (c) Microsoft Corporation. All rights reserved.
6 * Licensed under the MIT License.
7 */
8
9import { DialogContext } from 'botbuilder-dialogs';
10import { BotkitConversationStep } from './conversation';
11
12/**
13 * This class is used to provide easy access to common actions taken on active BotkitConversation instances.
14 * These objects are passed into handlers bound to BotkitConversations using .before .onChange and conditional handler functions passed to .ask and .addQuestion
15 * Grants access to convo.vars convo.gotoThread() convo.setVar() and convo.repeat().
16 */
17export class BotkitDialogWrapper {
18 private dc: DialogContext;
19 private step: BotkitConversationStep;
20 /**
21 * An object containing variables and user responses from this conversation.
22 */
23 public vars: {
24 [key: string]: any;
25 }
26
27 public constructor(dc: DialogContext, step: BotkitConversationStep) {
28 this.dc = dc;
29 this.step = step;
30 this.vars = this.step.values;
31 }
32
33 /**
34 * Jump immediately to the first message in a different thread.
35 * @param thread Name of a thread
36 */
37 public async gotoThread(thread: string): Promise<void> {
38 this.step.index = 0;
39 this.step.thread = thread;
40 }
41
42 /**
43 * Repeat the last message sent on the next turn.
44 */
45 public async repeat(): Promise<void> {
46 // move back one step next turn the bot will repeat with the last message sent.
47 this.step.index--;
48 }
49
50 /**
51 * Stop the dialog.
52 */
53 public async stop(): Promise<void> {
54 // set this to 1 bigger than the total length of the thread.
55 this.step.index = this.step.threadLength + 1;
56 }
57
58 /**
59 * Set the value of a variable that will be available to messages in the conversation.
60 * Equivalent to convo.vars.key = val;
61 * Results in {{vars.key}} being replaced with the value in val.
62 * @param key the name of the variable
63 * @param val the value for the variable
64 */
65 public setVar(key, val): void {
66 this.vars[key] = val;
67 }
68}