-
Notifications
You must be signed in to change notification settings - Fork 0
/
.napp.js.b
109 lines (89 loc) · 3.16 KB
/
.napp.js.b
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
require('dotenv').config();
const TelegramBot = require('node-telegram-bot-api');
const schedule = require('node-schedule');
const http = require('http');
// Initialize the bot
const bot = new TelegramBot(process.env.BOT_TOKEN, { polling: true });
const chatId = process.env.CHAT_ID;
// Store vote counts and voters to prevent duplicate voting
let voteCounts = { done: 0, notDone: 0 };
const voters = new Set();
// Define arrays for random button texts
const doneMessages = [
"✅ Y",
"✅ Done",
"✅ Sudah",
];
const notDoneMessages = [
"❌ Gak Mau Isi! udah kaya.",
"❌ Kangen Ditegur",
"❌ Udah Punya Trigger!",
];
// Function to get a random button text
const getRandomDoneMessage = () => {
return doneMessages[Math.floor(Math.random() * doneMessages.length)];
};
const getRandomNotDoneMessage = () => {
return notDoneMessages[Math.floor(Math.random() * notDoneMessages.length)];
};
// Create the voting buttons
const getVoteOptions = () => ({
reply_markup: {
inline_keyboard: [
[
{ text: `${getRandomDoneMessage()} (${voteCounts.done})`, callback_data: 'vote_done' },
{ text: `${getRandomNotDoneMessage()} (${voteCounts.notDone})`, callback_data: 'vote_not_done' },
],
],
},
});
// Function to send the poll message with buttons
const sendPoll = () => {
voteCounts = { done: 0, notDone: 0 }; // Reset counts
voters.clear(); // Clear previous voters
bot.sendMessage(chatId, "Apakah sudah isi timesheet hari ini?", getVoteOptions())
.catch((error) => console.error('Error sending poll:', error));
};
// Handle button clicks
bot.on('callback_query', (callbackQuery) => {
const { data, from, message } = callbackQuery;
const userId = from.id;
// Check if the user has already voted
if (voters.has(userId)) {
bot.answerCallbackQuery(callbackQuery.id, { text: 'Kamu sudah vote!' });
return;
}
voters.add(userId); // Add user to voters set
// Update the vote counts based on user selection
if (data === 'vote_done') voteCounts.done++;
if (data === 'vote_not_done') voteCounts.notDone++;
bot.answerCallbackQuery(callbackQuery.id, { text: 'Terima kasih sudah vote!' });
// Update the buttons with new counts
bot.editMessageReplyMarkup(getVoteOptions().reply_markup, {
chat_id: message.chat.id,
message_id: message.message_id,
}).catch((error) => console.error('Failed to update buttons:', error));
});
// Schedule the polls
// schedule.scheduleJob('* * * * *', sendPoll);
schedule.scheduleJob('0 7 * * 1-5', sendPoll); // 07:00 AM on weekdays
schedule.scheduleJob('0 20 * * 1-5', sendPoll); // 08:00 PM on weekdays
const logMessage = 'Bot is running and sending polls at 07:00 AM and 08:00 PM on weekdays!';
console.log(logMessage);
const sendLogMessage = () => {
const logRequest = http.request({
hostname: 'localhost',
port: 3000,
path: '/',
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
}, (res) => {
console.log(`Log message sent to index.js: ${res.statusCode}`);
});
logRequest.write(JSON.stringify({ message: logMessage }));
logRequest.end();
};
// Wait for 5 seconds before sending the log message
setTimeout(sendLogMessage, 5000);