-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot.js
202 lines (173 loc) · 5.14 KB
/
bot.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import "dotenv/config";
import { Bot, webhookCallback, GrammyError, HttpError } from "grammy";
import { ChatGPTAPIBrowser } from "chatgpt";
// Bot
const bot = new Bot(process.env.BOT_TOKEN);
// Auth
const api = new ChatGPTAPIBrowser({
email: process.env.email,
password: process.env.password,
isGoogleLogin: true,
});
api.initSession();
// Admin
const authorizedUsers = process.env.BOT_DEVELOPER?.split(",").map(Number) || [];
bot.use(async (ctx, next) => {
ctx.config = {
botDevelopers: authorizedUsers,
isDeveloper: authorizedUsers.includes(ctx.chat?.id),
};
await next();
});
// Response
async function responseTime(ctx, next) {
const before = Date.now();
await next();
const after = Date.now();
console.log(`Response time: ${after - before} ms`);
}
bot.use(responseTime);
// Commands
bot.command("start", async (ctx) => {
if (!ctx.chat.type == "private") {
await bot.api.sendMessage(
ctx.chat.id,
"*Channels and groups are not supported presently.*",
{ parse_mode: "Markdown" }
);
return;
}
await ctx
.reply(
"*Welcome!* ✨\n_This is a private ChatGPT instance.\nIf you want to request access, please get in touch!_",
{
parse_mode: "Markdown",
}
)
.then(console.log("New user added:\n", ctx.from));
});
bot.command("help", async (ctx) => {
await ctx
.reply(
"*@anzubo Project.*\n\n_This is a utility bot to query ChatGPT.\nUnauthorized use is not permitted._",
{ parse_mode: "Markdown" }
)
.then(console.log("Help command sent to", ctx.chat.id));
});
// Messages
bot.on("message", async (ctx) => {
// Logging
const from = ctx.from;
const name =
from.last_name === undefined
? from.first_name
: `${from.first_name} ${from.last_name}`;
console.log(
`From: ${name} (@${from.username}) ID: ${from.id}\nMessage: ${ctx.message.text}`
);
// Logic
if (!ctx.config.isDeveloper) {
await bot.api.sendMessage(
process.env.BOT_DEVELOPER,
`*From: ${name} (@${from.username}) ID: ${from.id}\nMessage: ${ctx.message.text}*`,
{ parse_mode: "Markdown" }
);
}
try {
const statusMessage = await ctx.reply(`*Processing*`, {
parse_mode: "Markdown",
});
async function deleteMessageWithDelay(fromId, messageId, delayMs) {
return new Promise((resolve, reject) => {
setTimeout(() => {
bot.api
.deleteMessage(fromId, messageId)
.then(() => resolve())
.catch((error) => reject(error));
}, delayMs);
});
}
await deleteMessageWithDelay(ctx.chat.id, statusMessage.message_id, 3000);
// GPT
async function sendMessageWithTimeout(ctx) {
try {
const resultPromise = api.sendMessage(ctx.msg.text);
const result = await Promise.race([
resultPromise,
new Promise((_, reject) => {
setTimeout(() => {
reject("Function timeout");
}, 60000);
}),
]);
console.log(result.detail.usage);
await ctx.reply(`${result.text}`, {
reply_to_message_id: ctx.message.message_id,
parse_mode: "Markdown",
});
console.log(`Function executed successfully from ${ctx.chat.id}`);
} catch (error) {
if (error === "Function timeout") {
await ctx.reply("*Query timed out.*", {
parse_mode: "Markdown",
reply_to_message_id: ctx.message.message_id,
});
} else {
throw error;
}
}
}
await sendMessageWithTimeout(ctx);
} catch (error) {
if (error instanceof GrammyError) {
if (error.message.includes("Forbidden: bot was blocked by the user")) {
console.log("Bot was blocked by the user");
} else if (error.message.includes("Call to 'sendMessage' failed!")) {
console.log("Error sending message: ", error);
await ctx.reply(`*Error contacting Telegram.*`, {
parse_mode: "Markdown",
reply_to_message_id: ctx.message.message_id,
});
} else {
await ctx.reply(`*An error occurred: ${error.message}*`, {
parse_mode: "Markdown",
reply_to_message_id: ctx.message.message_id,
});
}
console.log(`Error sending message: ${error.message}`);
return;
} else {
console.log(`An error occured:`, error);
await ctx.reply(`*An error occurred.*\n_Error: ${error.message}_`, {
parse_mode: "Markdown",
reply_to_message_id: ctx.message.message_id,
});
return;
}
}
});
// Error
bot.catch((err) => {
const ctx = err.ctx;
console.error(
"Error while handling update",
ctx.update.update_id,
"\nQuery:",
ctx.msg.text
);
const e = err.error;
if (e instanceof GrammyError) {
console.error("Error in request:", e.description);
if (e.description === "Forbidden: bot was blocked by the user") {
console.log("Bot was blocked by the user");
} else {
ctx.reply("An error occurred");
}
} else if (e instanceof HttpError) {
console.error("Could not contact Telegram:", e);
} else {
console.error("Unknown error:", e);
}
});
// Run
bot.start();