-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
294 lines (262 loc) · 9.34 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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const mysql = require('mysql');
require('dotenv').config()
//const { MongoClient, ServerApiVersion } = require('mongodb');
const TelegramBot = require('node-telegram-bot-api');
const token = process.env.TOKEN;
const uri =process.env.URI;
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
//
app.set('view engine', 'ejs');
app.set('views', __dirname + '/views');
app.use(express.static( __dirname + '/public'));
const bot = new TelegramBot(token, {polling: true});
const connection = mysql.createConnection({
host: 'localhost', // Replace with your MySQL host
user: 'root', // Replace with your username
password: '', // Replace with your password
database: 'test' // Replace with your database name
});
connection.connect((err) => {
if (err) {
console.error('Error connecting to MySQL:', err);
return;
}else{
console.log('Connected to MySQL database!');
connection.query('CREATE TABLE IF NOT EXISTS users (SL INT AUTO_INCREMENT PRIMARY KEY, UID INT)', (err, result) => {
if (err) throw err;
console.log('Table created (if it did not exist)!');
});
}
});
function checkAndAddUser(uid) {
connection.query('SELECT UID FROM `users` WHERE UID = ?', [uid], (err, rows) => {
if (err) {
console.error('Error checking user:', err);
// Handle the error appropriately (e.g., return an error code)
} else if (rows.length === 0) {
console.log("user doesnt exist in database");
// User doesn't exist, add them
addUser(uid);
} else {
console.log(`User with ID ${uid} already exists.`);
}
});
}
function addUser(uid) {
const newUser = { uid: uid };
connection.query('INSERT INTO users SET ?', newUser, (err, result) => {
if (err) {
console.error('Error adding user:', err);
// Handle the error appropriately (e.g., return an error code)
} else {
console.log('User added:', result);
}
});
}
// Route to display admin panel
app.get('/admin', (req, res) => {
connection.query('SELECT UID FROM `users`', (err, rows) => {
if (err) {
console.error('Error checking user:', err);
// Handle the error appropriately (e.g., return an error code)
} else {
res.render('admin-panel',{
users:rows
}); // Render the EJS template
users:rows
console.log(rows);
console.log(`Users fetched `);
}
});
});
//notificaion
app.post('/send-notification', async (req, res) => {
const { uid, message } = req.body;
console.log(uid, message);
try {
if (uid === "all") {0
connection.query('SELECT UID FROM `users`', (err, rows) => {
if (err) {
console.error('Error fetching user IDs:', err);
// Handle error (e.g., send error response to client)
return;
}
// Iterate through retrieved UIDs and send message to each user
rows.forEach((row) => {
const userUid = row.UID;
bot.sendMessage(userUid, message); // Assuming message is in req.body
});
res.send('Notifications sent successfully to all users'); // Send success response
});
} else {
const { uid, message } = req.body;
console.log(uid, message);
bot.sendMessage(uid, message);
}
} catch (err) {
console.error('Error sending notification:', err);
// Handle error (e.g., send error response to client)
}
});
app.listen(80, () => {
console.log("Running!")
})
const waitingUsers = []; // Queue for users waiting for a chat room
const activeChats = {}; // Object to store active chat rooms (key: chat ID, value: array of user IDs)
// Function to create a new chat room
function createChatRoom(user1, user2) {
const chatId = Math.random().toString(36).substring(2, 15); // Generate unique chat ID
activeChats[chatId] = [user1, user2];
// Remove users from waiting queue
waitingUsers.splice(waitingUsers.indexOf(user1), 1);
waitingUsers.splice(waitingUsers.indexOf(user2), 1);
console.log("------------------------------");
console.log("waitingUsers:",waitingUsers);
console.log("activeChats",activeChats);
console.log("------------------------------");
statusMessage(user1, `You've been paired with another user! Chat ID: ${chatId}`);
statusMessage(user2, `You've been paired with another user! Chat ID: ${chatId}`);
// Implement logic to handle messages within the chat room (using chatId)
handleChatMessages(chatId);
}
// Function to send message to a specific user
function sendMessage(userId, message) {
// bot.sendMessage(userId, message);
bot.sendMessage(userId,message, {
"reply_markup": {
"keyboard": [["/stop"], ["I'm robot"]]
}
});
}
function statusMessage(userId, message) {
bot.sendMessage(userId, message);
}
// Function to handle messages within a chat room
function handleChatMessages(chatId) {
bot.on('message', (msg) => {
const userId = msg.chat.id;
const messageText = msg.text;
// Check if user belongs to the chat room
if (activeChats[chatId] && activeChats[chatId].includes(userId)) {
const partnerId = activeChats[chatId].filter(id => id !== userId)[0];
// Error handling: check if partner found
if (partnerId) {
sendMessage(partnerId, `${userId}: ${messageText}`); // Send message to partner
} else {
console.log("------------------------------");
console.log("activeChats",activeChats);
// console.error(`Error: Partner not found in chat ${chatId} for user ${userId}`);
console.log("------------------------------");
}
} else {
console.log("------------------------------");
console.log("activeChats",activeChats);
//console.error(`Error: User ${userId} not found in active chat list for chat ${chatId}`);
console.log("------------------------------");
}
});
}
// Handle incoming messages
bot.onText(/\/start/, async (msg) => {
try {
checkAndAddUser(msg.chat.id);
//addUser(msg.chat.id)
//await log(msg.chat.id); // Wait for log to finish
} catch (error) {
console.error(error);
}
bot.sendMessage(msg.chat.id, "Welcome \n /search");
});
// /Stop
bot.onText(/\/stopsearching/, (msg) => {
const userId = msg.chat.id;
const userIndex = waitingUsers.indexOf(userId);
if (userIndex > -1) {
waitingUsers.splice(userIndex, 1);
}
bot.sendMessage(userId, "Search stopped!", {
"reply_markup": {
"keyboard": [["/search"]]
}
});
})
bot.onText(/\/search/, (msg) => {
const userId = msg.chat.id;
const username = msg.chat.username;
try{
if (!Object.values(activeChats).flat().includes(userId)) {
// User not in a chat room, follow pairing logic
if (waitingUsers.length === 1 && waitingUsers[0] !== userId) {
const partnerId = waitingUsers.shift();
createChatRoom(userId, partnerId);
}else if(waitingUsers[0] == userId){
// statusMessage(userId, `You are already on queue`);
bot.sendMessage(userId, `You are already on queue`, {
"reply_markup": {
"keyboard": [["/stopsearching"]]
}
});
} else {
waitingUsers.push(userId);
// statusMessage(userId, `Hi ${username}! You've joined the queue. You'll be paired with another user soon.`);
bot.sendMessage(userId, `Hi ${username}! You've joined the queue. You'll be paired with another user soon.`, {
"reply_markup": {
"keyboard": [["/stopsearching"]]
}
});
}
}
}catch(err){
console.log(err);
}
});
// Assuming you know the usernames (user1 and user2)
function getChatId(user1) {
// Loop through all active chats
for (const chatId in activeChats) {
const participants = activeChats[chatId];
// Check if the participants match the provided users
if (participants.includes(user1)) {
console.log(`Found the chat ID ${user1}`);
return chatId; // Found the chat ID, return it
}
}
console.log("Chat ID not found");
return null; // Chat ID not found
}
function deleteChatRoom(chatId) {
if (activeChats.hasOwnProperty(chatId)) {
console.log("activeChats",activeChats);
console.log(activeChats[chatId]);
console.log(activeChats[chatId][0]);
console.log(activeChats[chatId][1]);
delete activeChats[chatId];
// Additional steps might be needed, like notifying users and removing messages (implementation not shown)
console.log(`Chat room with ID ${chatId} deleted.`);
console.log("activeChats",activeChats);
} else {
console.log(`Chat room with ID ${chatId} not found.`);
}
}
bot.onText(/\/stop/, (msg) => {
const chatId = getChatId(msg.chat.id);
if (chatId) {
if (activeChats[chatId] && activeChats[chatId].includes(msg.chat.id)) {
const partnerId = activeChats[chatId].filter(id => id !== msg.chat.id)[0];
bot.sendMessage(partnerId, "Your partner left the chat");
}
deleteChatRoom(chatId);
bot.sendMessage(msg.chat.id, "You've left the chat.");
} else {
console.log("User not in a chat room.");
}
// Inform user regardless of chat room existence
});
bot.on('message', (msg) => {
// Optional: Logging code for debugging
});
bot.on("polling_error", console.log);