This repository has been archived by the owner on Mar 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
124 lines (104 loc) · 3.17 KB
/
main.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
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
require("dotenv/config");
const { Client, IntentsBitField } = require("discord.js");
const client = new Client({
intents: [
IntentsBitField.Flags.Guilds,
IntentsBitField.Flags.GuildMessages,
IntentsBitField.Flags.MessageContent,
],
});
// Create a new conversation
async function createConversation() {
const headers = {
"Content-Type": "application/json",
Authorization: `Bearer ${process.env.CHATBOTKIT_API_KEY}`,
};
const createConversationRes = await fetch(
"https://api.chatbotkit.com/v1/conversation/create",
{
method: "POST",
headers: headers,
body: JSON.stringify({
backstory: "This is a friendly chat bot.",
// model:""
// datasetId: "",
// skillsetId:"",
}),
}
);
if (!createConversationRes.ok) {
const message = (await createConversationRes.json()).message;
throw new Error(message);
}
const conversationId = (await createConversationRes.json()).id;
const createTokenRes = await fetch(
`https://api.chatbotkit.com/v1/conversation/${conversationId}/token/create`,
{
method: "POST",
headers: headers,
body: JSON.stringify({
durationInSeconds: 3600, // in seconds
}),
}
);
if (!createTokenRes.ok) {
const message = (await createTokenRes.json()).message;
throw new Error(message);
}
const token = (await createTokenRes.json()).token;
return { conversationId, token };
}
let conversationId;
let token;
client.on("ready", async () => {
const res = await createConversation();
conversationId = res.conversationId;
token = res.token;
});
client.on("messageCreate", async (message) => {
// Check if the message was sent by the bot itself
if (message.author.bot) return;
// if (message.channel.id !== process.env.CHANNEL_ID) return; --> Uncomment if you want the chatbot to answer only to certain channels
// if (message.content.startsWith("-")) return; --> Uncomment if you want the chatbot to answer to every message apart from the ones that start with "-". You can change this to whichever value you want
await message.channel.sendTyping();
try {
const headers = {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
};
const sendMessageRes = await fetch(
`https://api.chatbotkit.com/v1/conversation/${conversationId}/send`,
{
method: "POST",
headers: headers,
body: JSON.stringify({
text: message.content,
}),
}
);
if (sendMessageRes.ok) {
const recieveMessageRes = await fetch(
`https://api.chatbotkit.com/v1/conversation/${conversationId}/receive`,
{
method: "POST",
headers: headers,
body: JSON.stringify({
parse: true,
}),
}
);
if (recieveMessageRes.ok) {
const data = await recieveMessageRes.json();
const botReply = data.text.stripped;
await message.reply(botReply);
} else {
await message.reply(
`Sorry ${message.author}, I am not quite sure at the moment!`
);
}
}
} catch (err) {
console.error(err);
}
});
client.login(process.env.BOT_TOKEN);