This repository has been archived by the owner on Nov 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.js
92 lines (73 loc) · 2.46 KB
/
index.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
import {ChatGPTAPIBrowser} from 'chatgpt'
import * as dotenv from 'dotenv'
import TelegramBot from 'node-telegram-bot-api'
import {sleep, convertExponentialToSymbol} from './app/utils.js'
import {oraPromise} from "ora";
import Queue from 'bee-queue';
const queue = new Queue('chatgpt');
import JSONdb from 'simple-json-db';
const db = new JSONdb('./app/storage.json');
import fs from "fs";
dotenv.config()
const bot = new TelegramBot(process.env.TELEGRAM_TOKEN, {polling: true});
const api = new ChatGPTAPIBrowser({
email: process.env.OPENAI_EMAIL,
password: process.env.OPENAI_PASSWORD,
isGoogleLogin: true,
})
await api.initSession()
let commands = (await fs.promises.readdir("./commands"))
.map((f) => {
return {
name: f.replace(".js", ""),
module: import(`./commands/${f}`)
}
})
bot.on('message', async (msg) => {
const chatId = msg.chat.id;
const chatText = msg.text;
if (chatText.startsWith("/")) {
let splitText = chatText.split(" ", 1)
let cmd = commands.find((c) => c.name === splitText[0].replace("/", ""))
if (cmd) return (await cmd.module).run(bot, msg, splitText[1] ?? null, {
db
})
}
const thingMsgId = (await bot.sendMessage(chatId, '🤔', {
reply_to_message_id: msg.message_id
})).message_id;
const job = await queue.createJob({
chatId,
thingMsgId,
text: chatText
}).save()
job.on('failed', async () => {
await job.retries(1).save();
})
});
queue.process(async function (job, done) {
const {text, chatId, thingMsgId} = job.data
try {
const chatInfo = db.has(chatId) ? db.get(chatId) : {}
const chatSettings = {
conversationId: chatInfo.conversationId ?? undefined,
parentMessageId: chatInfo.lastMessage ?? undefined
}
let result = await oraPromise(api.sendMessage(text, chatSettings), {
text: "Processing Message: " + text
})
let resp = result.response
resp = convertExponentialToSymbol(resp)
db.set(chatId, {conversationId: result.conversationId, lastMessage: result.messageId})
await bot.editMessageText(resp, {
chat_id: chatId,
message_id: thingMsgId,
parse_mode: 'Markdown'
})
return done()
} catch (e) {
console.log("Sleeping for 5 secs..")
await sleep(5000)
throw new Error("Error")
}
});