/
teamsBot.ts
57 lines (49 loc) · 1.69 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
import {
TeamsActivityHandler,
CardFactory,
TurnContext,
AdaptiveCardInvokeValue,
AdaptiveCardInvokeResponse,
} from "botbuilder";
import rawWelcomeCard from "./adaptiveCards/welcome.json";
import { AdaptiveCards } from "@microsoft/adaptivecards-tools";
import { Configuration, OpenAIApi } from "openai";
import config from "./config";
export class TeamsBot extends TeamsActivityHandler {
constructor() {
super();
const configuration = new Configuration({
apiKey: config.openaiApiKey,
});
const openai = new OpenAIApi(configuration);
this.onMessage(async (context, next) => {
console.log("Running with Message Activity.");
let txt = context.activity.text;
const removedMentionText = TurnContext.removeRecipientMention(context.activity);
if (removedMentionText) {
// Remove the line break
txt = removedMentionText.toLowerCase().replace(/\n|\r/g, "").trim();
}
const response = await openai.createCompletion({
model: "text-davinci-003",
prompt: txt,
temperature: 0,
max_tokens: 2048,
});
await context.sendActivity(response.data.choices[0].text);
// 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 = AdaptiveCards.declareWithoutData(rawWelcomeCard).render();
await context.sendActivity({ attachments: [CardFactory.adaptiveCard(card)] });
break;
}
}
await next();
});
}
}