Last active
February 5, 2023 15:04
-
-
Save rueian/6c68f90d445c3274264d4a977d7b60e9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
TeamsActivityHandler, | |
CardFactory, | |
TurnContext, | |
} from "botbuilder"; | |
import rawWelcomeCard from "./adaptiveCards/welcome.json"; | |
import rawReplyCard from "./adaptiveCards/reply.json"; | |
import { AdaptiveCards } from "@microsoft/adaptivecards-tools"; | |
import { Configuration, OpenAIApi } from "openai"; | |
export class TeamsBot extends TeamsActivityHandler { | |
apiKeys: {[name:string]: string} = {}; | |
history: {[name:string]: string} = {}; | |
constructor() { | |
super(); | |
this.onMessage(async (context, next) => { | |
if (context.activity.value?.openaikey) { | |
this.apiKeys[context.activity.channelId] = context.activity.value?.openaikey; | |
await context.sendActivity({ text: 'OpenAPI key updated, you can start chatting with me.' }); | |
await next(); | |
return; | |
} | |
if (context.activity.value?.restart) { | |
delete this.apiKeys[context.activity.channelId]; | |
await context.sendActivity({ text: 'Conversation restarted.' }); | |
await next(); | |
return; | |
} | |
let txt = context.activity.text; | |
const removedMentionText = TurnContext.removeRecipientMention(context.activity); | |
if (removedMentionText) { | |
txt = removedMentionText.toLowerCase().replace(/\n|\r/g, "").trim(); | |
} | |
const apiKey = this.apiKeys[context.activity.channelId]; | |
if (txt === 'changekey' || !apiKey) { | |
const card = AdaptiveCards.declareWithoutData(rawWelcomeCard).render(); | |
await context.sendActivity({ attachments: [CardFactory.adaptiveCard(card)] }); | |
} else { | |
let prompt; | |
if (this.history[context.activity.channelId]) { | |
prompt = this.history[context.activity.channelId] + `\nQ: ${txt}\nA: `; | |
} else { | |
prompt = `Q: ${txt}\nA: ` | |
} | |
const openai = new OpenAIApi(new Configuration({ apiKey })); | |
const completion = await openai.createCompletion({ | |
model: "text-davinci-003", | |
prompt: prompt, | |
max_tokens: 2048, | |
}); | |
const reply = completion.data.choices[0].text; | |
const restart = AdaptiveCards.declare(rawReplyCard).render({reply}); | |
await context.sendActivity({ attachments: [CardFactory.adaptiveCard(restart)] }); | |
this.history[context.activity.channelId] = prompt + reply | |
console.log(JSON.stringify({"time": new Date(), "user": context.activity.from, "result": this.history[context.activity.channelId]})); | |
} | |
// By calling next() you ensure that the next BotHandler is run. | |
await next(); | |
}); | |
this.onMembersAdded(async (context, next) => { | |
if (!this.apiKeys[context.activity.channelId]) { | |
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(); | |
}); | |
} | |
} | |
// { | |
// "type": "AdaptiveCard", | |
// "body": [ | |
// { | |
// "type": "TextBlock", | |
// "size": "Medium", | |
// "weight": "Bolder", | |
// "text": "Welcome to Chatbot" | |
// }, | |
// { | |
// "type": "Input.Text", | |
// "placeholder": "sk-", | |
// "id": "openaikey", | |
// "label": "Enter OpenAI key here", | |
// "isRequired": true, | |
// "errorMessage": "key is required", | |
// "inlineAction": { | |
// "type": "Action.Submit", | |
// "title": "Save", | |
// "id": "savekey" | |
// } | |
// } | |
// ], | |
// "$schema": "http://adaptivecards.io/schemas/adaptive-card.json", | |
// "version": "1.3" | |
// } | |
// { | |
// "type": "AdaptiveCard", | |
// "actions": [ | |
// { | |
// "type": "Action.Submit", | |
// "title": "Restart Conversation", | |
// "id": "restartsubmit" | |
// } | |
// ], | |
// "$schema": "http://adaptivecards.io/schemas/adaptive-card.json", | |
// "version": "1.3", | |
// "body": [ | |
// { | |
// "type": "TextBlock", | |
// "text": "${$root.reply}", | |
// "wrap": true | |
// }, | |
// { | |
// "type": "Input.Text", | |
// "placeholder": "Placeholder text", | |
// "isVisible": false, | |
// "id": "restart", | |
// "label": "restart", | |
// "value": "restart" | |
// } | |
// ] | |
// } | |
// FROM --platform=$TARGETPLATFORM node:19-alpine | |
// COPY ./bot/package.json /bot/package.json | |
// COPY ./bot/package-lock.json /bot/package-lock.json | |
// WORKDIR /bot | |
// COPY ./bot /bot | |
// RUN npm install && find /bot/node_modules/ ! -user root | xargs chown root:root | |
// RUN npm run build | |
// CMD npm run start | |
// server.listen(process.env.port || process.env.PORT || process.env.WEBSITES_PORT || 3978, () => { |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment