-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtelegramBotHelper.js
More file actions
136 lines (121 loc) · 4.07 KB
/
Copy pathtelegramBotHelper.js
File metadata and controls
136 lines (121 loc) · 4.07 KB
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
const { Telegraf, Markup } = require('telegraf');
class TelegramBotHelper {
constructor(token, forwardChatId) {
this.bot = new Telegraf(token);
this.forwardChatId = forwardChatId;
this.subscriptions = {};
this.subscriptionPlan = {
free: {
limit: 3,
interval: 24 * 60 * 60 * 1000,
},
premium: {
limit: 10,
interval: 12 * 60 * 60 * 1000,
},
};
}
start() {
this.bot.start((ctx) => ctx.reply('Welcome to the Forwarding Bot!'));
}
help() {
this.bot.help((ctx) => {
const helpMessage =
"To forward a message, simply send it to this bot.\n\n" +
"Available commands:\n" +
"/help - Show this help menu\n" +
"/subscribe - Subscribe to the premium plan\n" +
"/unsubscribe - Unsubscribe from the premium plan\n" +
"/leavegroup - Leave the current group\n" +
"/speedtest - Test bot speed";
ctx.reply(helpMessage);
});
}
leaveGroup() {
this.bot.command('leavegroup', (ctx) => {
const chatId = ctx.message.chat.id;
this.bot.telegram.leaveChat(chatId);
});
}
subscribe() {
this.bot.command('subscribe', (ctx) => {
const chatId = ctx.message.chat.id;
if (this.subscriptions[chatId]) {
ctx.reply('You are already subscribed to the premium plan.');
} else {
this.subscriptions[chatId] = {
plan: 'premium',
remaining: this.subscriptionPlan.premium.limit,
nextReset: Date.now() + this.subscriptionPlan.premium.interval,
};
ctx.reply('You have successfully subscribed to the premium plan.');
}
});
}
unsubscribe() {
this.bot.command('unsubscribe', (ctx) => {
const chatId = ctx.message.chat.id;
if (this.subscriptions[chatId]) {
delete this.subscriptions[chatId];
ctx.reply('You have unsubscribed from the premium plan.');
} else {
ctx.reply('You are not currently subscribed to any plan.');
}
});
}
speedTest() {
this.bot.command('speedtest', async (ctx) => {
const start = Date.now();
const message = await ctx.reply('Testing bot speed...');
const end = Date.now();
const latency = end - start;
await this.bot.telegram.editMessageText(ctx.chat.id, message.message_id, undefined, `Bot latency: ${latency}ms`);
});
}
forwardMessage(prefix) {
this.bot.on('message', (ctx) => {
const chatId = ctx.message.chat.id;
const messageText = ctx.message.text;
if (messageText.startsWith(prefix)) {
if (this.subscriptions[chatId] && this.subscriptions[chatId].remaining > 0) {
this.subscriptions[chatId].remaining--;
const forwardedMessage = messageText.slice(prefix.length).trim();
this.bot.telegram.sendMessage(this.forwardChatId, forwardedMessage);
} else {
ctx.reply('You have reached the maximum number of forwards for your subscription plan.');
}
}
});
}
menu() {
this.bot.hears('Menu', (ctx) => {
const menuKeyboard = Markup.keyboard([
['Forward Message', 'Subscription Plan'],
['Help', 'Leave Group', 'Speed Test'],
]).resize().extra();
ctx.reply('Select an option:', menuKeyboard);
});
}
forwardMessageCommand() {
this.bot.hears('Forward Message', (ctx) => {
ctx.reply('Please send the message you want to forward.');
});
}
subscriptionPlanCommand() {
this.bot.hears('Subscription Plan', (ctx) => {
const chatId = ctx.message.chat.id;
const plan = this.subscriptions[chatId] ? this.subscriptions[chatId].plan : 'free';
const remaining = this.subscriptions[chatId] ? this.subscriptions[chatId].remaining : 0;
const nextReset = this.subscriptions[chatId] ? new Date(this.subscriptions[chatId].nextReset).toLocaleString() : 'N/A';
const subscriptionMessage =
`Your subscription plan: ${plan}\n` +
`Remaining forwards: ${remaining}\n` +
`Next reset: ${nextReset}`;
ctx.reply(subscriptionMessage);
});
}
launch() {
this.bot.launch();
}
}
module.exports = TelegramBotHelper;