-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
105 lines (90 loc) · 3.42 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
const express = require("express");
const TelegramBot = require("node-telegram-bot-api");
const { sendMessage } = require("./predict");
const http = require("http");
require("dotenv").config();
const TELEGRAM_BOT_TOKEN = process.env.TELEGRAM_BOT_KEY;
const bot = new TelegramBot(TELEGRAM_BOT_TOKEN, { polling: true });
const app = express();
const PORT = process.env.PORT || 4040;
// Using a Free tier at the moment, that every 50 seconds, this manually pings the server every 30 seconds to ensure it's still up
setInterval(() => {
http
.get(`http://localhost:${PORT}`, (res) => {
console.log(`Pinged the server - Status Code: ${res.statusCode}`);
})
.on("error", (e) => {
console.error(`Error pinging the server: ${e.message}`);
});
}, 30000);
// Handle the /eligibility command
bot.onText(/\/review (.+)/, async (msg, match) => {
const chatId = msg.chat.id;
const projectDescription = match[1]; // Extract the project description from the command
try {
// Call the eligibility evaluation function
const response = await sendMessage(projectDescription);
// Send the response back to the group
bot.sendMessage(chatId, response);
} catch (error) {
console.error("Error evaluating eligibility:", error);
bot.sendMessage(chatId, "An error occurred while processing your request.");
}
});
let expectingDescription = false;
bot.onText(/\/review/, async (msg) => {
const chatId = msg.chat.id;
expectingDescription = true;
bot.sendMessage(chatId, "Please enter your project description:");
});
bot.on("message", async (msg) => {
if (expectingDescription) {
const chatId = msg.chat.id;
const projectDescription = msg.text;
// Call the review function with the project description
try {
const response = await sendMessage(projectDescription);
bot.sendMessage(chatId, response);
} catch (error) {
console.error("Error reviewing project:", error);
bot.sendMessage(
chatId,
"An error occurred while processing your request."
);
} finally {
expectingDescription = false;
}
}
});
bot.onText(/\/start/, async (msg) => {
const chatId = msg.chat.id;
const startMessage = `Welcome to AI-PGF BOT! This bot is designed to check your project Eligibility status and Provide Reasons.\n\nAvailable commands:\n/review - Followed by the description of your project to review your project`;
bot.sendMessage(chatId, startMessage);
});
bot.onText(/\/(?!review|start)\w*/, async (msg) => {
if (expectingDescription) return;
const chatId = msg.chat.id;
const unknownCommandMessage = `Sorry, I didn't understand that command. Available commands:\n/review - Review a project\n/start - Set of instructions to get started`;
bot.sendMessage(chatId, unknownCommandMessage);
});
// Express POST route to handle Telegram webhook updates
app.post("/", express.json(), async (req, res) => {
try {
const update = req.body;
// Process the update using the bot
bot.processUpdate(update);
// Respond with a 200 status to acknowledge receipt of the update
res.status(200).send("OK");
} catch (err) {
console.error("Error processing request:", err);
res.status(500).send("Internal Server Error");
}
});
// Handle errors for methods other than POST
app.all("/", (req, res) => {
res.status(405).send("Method Not Allowed");
});
// Start the Express server
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});