-
Notifications
You must be signed in to change notification settings - Fork 0
/
teamsBot.ts
128 lines (112 loc) · 4.07 KB
/
teamsBot.ts
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
import {
TeamsActivityHandler,
TurnContext,
SigninStateVerificationQuery,
BotState,
AdaptiveCardInvokeValue,
AdaptiveCardInvokeResponse,
MemoryStorage,
ConversationState,
UserState,
} from "botbuilder";
import { Utils } from "./helpers/utils";
import { SSODialog } from "./helpers/ssoDialog";
import { CommandsHelper } from "./helpers/commandHelper";
const rawWelcomeCard = require("./adaptiveCards/welcome.json");
const rawLearnCard = require("./adaptiveCards/learn.json");
export class TeamsBot extends TeamsActivityHandler {
likeCountObj: { likeCount: number };
conversationState: BotState;
userState: BotState;
dialog: SSODialog;
dialogState: any;
commandsHelper: CommandsHelper;
constructor() {
super();
// record the likeCount
this.likeCountObj = { likeCount: 0 };
// Define the state store for your bot.
// See https://aka.ms/about-bot-state to learn more about using MemoryStorage.
// A bot requires a state storage system to persist the dialog and user state between messages.
const memoryStorage = new MemoryStorage();
// Create conversation and user state with in-memory storage provider.
this.conversationState = new ConversationState(memoryStorage);
this.userState = new UserState(memoryStorage);
this.dialog = new SSODialog(new MemoryStorage());
this.dialogState = this.conversationState.createProperty("DialogState");
this.onMessage(async (context, next) => {
console.log("Running with Message Activity.");
let txt = context.activity.text;
// remove the mention of this bot
const removedMentionText = TurnContext.removeRecipientMention(
context.activity
);
if (removedMentionText) {
// Remove the line break
txt = removedMentionText.toLowerCase().replace(/\n|\r/g, "").trim();
}
// Trigger command by IM text
await CommandsHelper.triggerCommand(txt, {
context: context,
ssoDialog: this.dialog,
dialogState: this.dialogState,
likeCount: this.likeCountObj,
});
// By calling next() you ensure that the next BotHandler is run.
await next();
});
this.onMembersAdded(async (context, next) => {
const membersAdded = context.activity.membersAdded;
for (let cnt = 0; cnt < membersAdded.length; cnt++) {
if (membersAdded[cnt].id) {
const card = Utils.renderAdaptiveCard(rawWelcomeCard);
await context.sendActivity({ attachments: [card] });
break;
}
}
await next();
});
}
// Invoked when an action is taken on an Adaptive Card. The Adaptive Card sends an event to the Bot and this
// method handles that event.
async onAdaptiveCardInvoke(
context: TurnContext,
invokeValue: AdaptiveCardInvokeValue
): Promise<AdaptiveCardInvokeResponse> {
// The verb "userlike" is sent from the Adaptive Card defined in adaptiveCards/learn.json
if (invokeValue.action.verb === "userlike") {
this.likeCountObj.likeCount++;
const card = Utils.renderAdaptiveCard(rawLearnCard, this.likeCountObj);
await context.updateActivity({
type: "message",
id: context.activity.replyToId,
attachments: [card],
});
return { statusCode: 200, type: undefined, value: undefined };
}
}
async run(context: TurnContext) {
await super.run(context);
// Save any state changes. The load happened during the execution of the Dialog.
await this.conversationState.saveChanges(context, false);
await this.userState.saveChanges(context, false);
}
async handleTeamsSigninVerifyState(
context: TurnContext,
query: SigninStateVerificationQuery
) {
console.log(
"Running dialog with signin/verifystate from an Invoke Activity."
);
await this.dialog.run(context, this.dialogState);
}
async handleTeamsSigninTokenExchange(
context: TurnContext,
query: SigninStateVerificationQuery
) {
await this.dialog.run(context, this.dialogState);
}
async onSignInInvoke(context: TurnContext) {
await this.dialog.run(context, this.dialogState);
}
}