-
Notifications
You must be signed in to change notification settings - Fork 780
/
teamsBot.js
85 lines (72 loc) · 2.37 KB
/
teamsBot.js
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
const { TeamsActivityHandler } = require("botbuilder");
const { Configuration, OpenAIApi } = require("openai");
var textToAnalyze = "";
var sentimentResponse = "";
class TeamsBot extends TeamsActivityHandler {
constructor() {
super();
this.baseUrl = process.env.BaseUrl;
}
// fetch and handle message extension task module
async handleTeamsMessagingExtensionFetchTask(context, action) {
textToAnalyze = action.messagePayload.body.content;
textToAnalyze = textToAnalyze.replace(/(<([^>]+)>)/ig, '');
// Configure your apikey
const configuration = new Configuration({
apiKey: process.env.SECRET_OPENAI_API_KEY
});
// Updaitng configuration with OpenAI API
const openai = new OpenAIApi(configuration);
const analyzeSentiment = async (tweet) => {
try {
const messages = [
{
role: 'system',
content: `You will be provided with a tweet, and your task is to classify its sentiment as positive, neutral, or negative`,
},
{
role: 'user',
content: `${tweet}`
}
]
// CreateChatCompletion api call
const response = await openai.createChatCompletion({
model: process.env.CHAT_COMPLETION_MODEL_NAME,
messages: messages,
temperature: 0,
max_tokens: 256,
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0,
});
// Extract the AI's response from the API call
const aiResponse = response.data.choices[0].message.content.trim();
return aiResponse;
}
catch (error) {
console.error('Error:', error);
throw error;
}
};
// get sentiment response for selected teams message
await analyzeSentiment(textToAnalyze)
.then(sentiment =>
sentimentResponse = sentiment
).catch(err => console.error('Error:', err));
// return sentiment analysis result to task module
return {
task: {
type: 'continue',
value: {
width: 600,
height: 400,
title: 'Sentiment Analysis',
url: this.baseUrl + "/sentimentModule?title=" + textToAnalyze + "&result=" + sentimentResponse
}
}
};
}
}
module.exports.TeamsBot = TeamsBot;