forked from microsoft/BotBuilder-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.js
More file actions
48 lines (43 loc) · 2.02 KB
/
Copy pathbot.js
File metadata and controls
48 lines (43 loc) · 2.02 KB
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
// bot.js is your bot's main entry point to handle incoming activities.
const { ActivityTypes } = require('botbuilder');
// Turn counter property
const TURN_COUNTER_PROPERTY = 'turnCounterProperty';
class EchoBot {
/**
*
* @param {ConversationState} conversation state object
*/
constructor(conversationState) {
// Creates a new state accessor property.
// See https://aka.ms/about-bot-state-accessors to learn more about the bot state and state accessors
this.countProperty = conversationState.createProperty(TURN_COUNTER_PROPERTY);
this.conversationState = conversationState;
}
/**
*
* Use onTurn to handle an incoming activity, received from a user, process it, and reply as needed
*
* @param {TurnContext} on turn context object.
*/
async onTurn(turnContext) {
// Handle message activity type. User's responses via text or speech or card interactions flow back to the bot as Message activity.
// Message activities may contain text, speech, interactive cards, and binary or unknown attachments.
// see https://aka.ms/about-bot-activity-message to learn more about the message and other activity types
if (turnContext.activity.type === ActivityTypes.Message) {
// read from state.
let count = await this.countProperty.get(turnContext);
count = count === undefined ? 1 : ++count;
await turnContext.sendActivity(`${ count }: You said "${ turnContext.activity.text }"`);
// increment and set turn counter.
await this.countProperty.set(turnContext, count);
} else {
// Generic handler for all other activity types.
await turnContext.sendActivity(`[${ turnContext.activity.type } event detected]`);
}
// Save state changes
await this.conversationState.saveChanges(turnContext);
}
}
exports.EchoBot = EchoBot;