-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
114 lines (98 loc) · 3.46 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
const TelegramBot = require('node-telegram-bot-api');
const schedule = require('node-schedule');
require('dotenv').config();
const bot = new TelegramBot(process.env.TELE_TOKEN, { polling: true });
let tasks = [];
let reminderInterval = null;
let reminderJob = null;
//Cpmmand to start
bot.onText(/\/start/, (msg) => {
const chatId = msg.chat.id;
const welcomeMessage = `
Welcome to Task Reminder Bot!
This bot helps you manage and remind you of your daily tasks.
Commands:
- /task <tasks> - Add tasks for the day. Seperate each task with - ;.
- /settime <minutes|hours> - Set reminder intervals (e.g., 30m for 30 minutes, 1h for 1 hour).
- /done <task_number> - Mark a specific task as done.
- /doneall - Mark all tasks as done for the day.
- /viewtasks - View the list of current tasks.
- /help - Show this help message.
`;
bot.sendMessage(chatId, welcomeMessage.trim());
});
//Command to list all tasks to be done
bot.onText(/\/task (.+)/, (msg, match) => {
const chatId = msg.chat.id;
tasks = match[1].split(';').map(task => task.trim());
const taskList = tasks.map((task, index) => `${index + 1}. ${task}`).join('\n');
bot.sendMessage(chatId, `Tasks:\n${taskList}`);
});
//Command to set time for task reminder
bot.onText(/\/settime (\d+)([mh])/, (msg, match) => {
const chatId = msg.chat.id;
const value = parseInt(match[1], 10);
const unit = match[2];
let interval;
if (unit === 'm') {
interval = value;
} else if (unit === 'h') {
interval = value * 60;
} else {
bot.sendMessage(chatId, 'Invalid unit. Use `m` for minutes or `h` for hours.');
return;
}
reminderInterval = interval;
// Cancel any existing reminder job
if (reminderJob) {
reminderJob.cancel();
}
reminderJob = schedule.scheduleJob(`*/${interval} * * * *`, () => {
bot.sendMessage(chatId, `Reminder: Don't forget to complete your tasks!`);
});
bot.sendMessage(chatId, `Reminder set to every ${interval} minute${interval > 1 ? 's' : ''}.`);
});
//Command to indicate a certain task is done
bot.onText(/\/done (\d+)/, (msg, match) => {
const chatId = msg.chat.id;
const taskNumber = parseInt(match[1], 10) - 1;
if (taskNumber >= 0 && taskNumber < tasks.length) {
tasks.splice(taskNumber, 1);
bot.sendMessage(chatId, `Task ${taskNumber + 1} done.`);
} else {
bot.sendMessage(chatId, 'Invalid task number.');
}
});
//Command to indicate all tasks are done
bot.onText(/\/doneall/, (msg) => {
const chatId = msg.chat.id;
tasks = [];
if (reminderJob) {
reminderJob.cancel();
}
bot.sendMessage(chatId, 'All tasks done.');
});
//Cpmmand to view all tasks
bot.onText(/\/viewtasks/, (msg) => {
const chatId = msg.chat.id;
if (tasks.length > 0) {
const taskList = tasks.map((task, index) => `${index + 1}. ${task}`).join('\n');
bot.sendMessage(chatId, `Current Tasks:\n${taskList}`);
} else {
bot.sendMessage(chatId, 'No tasks to show.');
}
});
//Help command to list commands
bot.onText(/\/help/, (msg) => {
const chatId = msg.chat.id;
const helpMessage = `
Commands:
- /task <tasks> - Add tasks for the day (separated by semicolons).
- /settime <minutes|hours> - Set reminder intervals (e.g., 30m for 30 minutes, 1h for 1 hour).
- /done <task_number> - Mark a specific task as done.
- /doneall - Mark all tasks as done for the day.
- /viewtasks - View the list of current tasks.
- /help - Show this help message.
`;
bot.sendMessage(chatId, helpMessage.trim());
});