This repository has been archived by the owner on Dec 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
bot.js
249 lines (237 loc) · 7.21 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
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
require("dotenv").config();
const { Telegraf, Scenes, session } = require("telegraf");
const connection = require("./core/connect");
const { sendText, sendSticker } = require("./core/helper");
const User = require("./db/userSchema");
const connect = require("./db/connectMongo");
const ADMIN = "306549960"; // Change this
const rateLimit = require("telegraf-ratelimit");
const bot = new Telegraf(process.env.BOT_TOKEN);
const logger = require("pino")();
const axios = require("axios");
const sock = connection.startSock();
// Set limit to 3 message/hr
const limitConfig = {
window: 3.6e6, // 1hr milisecond
limit: 3, // 3 sticker allowed per hr
onLimitExceeded: async (ctx, next) => {
try {
await ctx.reply("rate limit exceeded deleting...");
await ctx.deleteMessage();
} catch (e) {}
},
};
bot.use(rateLimit(limitConfig));
bot.use(session());
const KEY = process.env.KEY; // invitation code
const Tg2Wa = new Scenes.WizardScene(
"TelegramToWhatsapp",
async (ctx) => {
try {
await ctx.reply(`Send invitation code to access this bot! :)`);
return ctx.wizard.next();
} catch (e) {
logger.error(`Bot Blocked || Something Wrong`);
}
},
async (ctx) => {
try {
ctx.wizard.state.key = await ctx.message.text;
if (ctx.wizard.state.key === KEY) {
await ctx.reply("Success !!");
await ctx.reply("Hit /continue");
return ctx.wizard.next();
} else {
await ctx.reply(`Invalid invitation code\nStart again ?? /setup`);
return ctx.scene.leave();
}
} catch (e) {
logger.error(`Bot Blocked || Something Wrong`);
}
},
async (ctx) => {
try {
await ctx.reply("Enter your name");
return ctx.wizard.next();
} catch (e) {
logger.error(`Bot Blocked || Something Wrong`);
}
},
async (ctx) => {
try {
ctx.wizard.state.name = await ctx.message.text;
await ctx.reply(
`Enter your mobile no in international format\ne.g. +919876543210`
);
return ctx.wizard.next();
} catch (e) {
logger.error(`Bot Blocked || Something Wrong`);
}
},
async (ctx) => {
try {
ctx.wizard.state.mobile = await ctx.message.text;
const { name, mobile } = await ctx.wizard.state;
if (!mobile.includes("+91") && !Number.isInteger(mobile)) {
// if(isValidPhone(mobile)
await ctx.reply(
`Not an Indian Number Or\nInvalid Format\ne.g. +919876543210 `
);
return await ctx.scene.leave();
} else {
await ctx.reply(`Your name: ${name}\nYour Number: ${mobile}`);
await ctx.tg.sendMessage(
ADMIN,
`Name: ${name}\nMobile Number: ${mobile}\nUser chat: ${JSON.stringify(
ctx.chat
)}`
);
let alreadyThere = await User.findOne({
userid: JSON.stringify(ctx.chat.id),
}).exec();
if (alreadyThere) {
await ctx.reply("Welcome Back");
return await ctx.scene.leave();
}
await ctx.reply("Saving your number to db...");
const added = await new User({
userid: JSON.stringify(ctx.chat.id),
name: name,
phone: mobile,
});
added.save(function (err) {
if (err) return handleError(err);
// saved!
});
if (added) {
await ctx.reply("Done");
await ctx.reply("Now you can start sending me stickers");
} else {
await ctx.reply("Something Wrong");
}
return await ctx.scene.leave();
}
} catch (e) {
logger.error(`Bot Blocked || Something Wrong`);
}
}
);
const stage = new Scenes.Stage([Tg2Wa]);
bot.use(stage.middleware());
bot.command("start", async (ctx) => {
try {
await ctx.reply(
`Hello \nStart Setup: /setup \nUpdate Number /update\nInfo: /me \nKey:`
);
} catch (e) {
logger.error(`Bot Blocked || Something Wrong`);
}
});
bot.command("setup", async (ctx) => {
try {
await ctx.scene.enter("TelegramToWhatsapp");
} catch (e) {
logger.error(`Bot Blocked || Something Wrong`);
}
});
bot.command("me", async (ctx) => {
try {
const { userid, name, phone } = await User.findOne({
userid: JSON.stringify(ctx.chat.id),
}).exec();
await ctx.reply(`Your name: ${name}\nYour Number: ${phone}`);
} catch (e) {
logger.error(`Bot Blocked || Something Wrong`);
}
});
bot.command("update", async (ctx) => {
try {
await ctx.reply("Enter new number");
bot.on("message", async (ctx) => {
const newNum = ctx.message.text;
try {
if (!newNum.includes("+91") && !Number.isInteger(newNum)) {
return await ctx.reply(
`Not an Indian Number Or\nInvalid Format\ne.g. +919876543210 `
);
} else {
await ctx.reply(`Updating...`);
const filter = { userid: JSON.stringify(ctx.chat.id) };
const update = { phone: newNum };
const updateNum = await User.findOneAndUpdate(filter, update);
await ctx.reply(`Updated Check Here: /me`);
}
} catch (e) {
logger.error(`Bot Blocked || Something Wrong`);
}
});
} catch (e) {
logger.error(`Bot Blocked || Something Wrong`);
}
});
bot.on("sticker", async (ctx) => {
try {
const { phone } = await User.findOne({
userid: JSON.stringify(ctx.chat.id),
}).exec();
let { sticker } = await ctx.message;
if (sticker.is_animated) {
return await ctx.reply("animated(tgs) stickers not supported");
} else {
let stickerId = await sticker.file_id;
let { file_id } = await ctx.telegram.getFile(stickerId);
let { href } = await ctx.telegram.getFileLink(file_id);
await sendSticker(sock, phone, href);
}
} catch (e) {
console.log(e);
logger.error(`Bot Blocked || Something Wrong`);
}
});
bot.command("help", async (ctx) => {
try {
let alreadyThere = await User.findOne({
userid: JSON.stringify(ctx.chat.id),
}).exec();
if (alreadyThere) {
return await ctx.reply(`Update Number /update\nInfo: /me \nKey:`);
}
await ctx.reply(
`Start Setup: /setup \nUpdate Number /update\nInfo: /me \nKey:`
);
} catch (error) {
logger.error(`Bot Blocked || Something Wrong`);
}
});
bot.command("sendpack", async (ctx) => {
try {
const { phone } = await User.findOne({
userid: JSON.stringify(ctx.chat.id),
}).exec();
let pack = "";
let packName = ctx.message.text.split("/").pop();
try {
pack = await ctx.telegram.getStickerSet(packName);
} catch (e) {
return await ctx.reply(
"Invalid Pack Name\nE.g https://t.me/addstickers/Actualiteit\nDon't try tosend animated(tgs) sticker pack."
);
}
let links = await Promise.all(
pack.stickers.map(async (item, index) => {
let { file_id } = await ctx.telegram.getFile(item.file_id);
let { href } = await ctx.telegram.getFileLink(file_id);
return Promise.resolve(href);
})
);
for (let i = 0; i < links.length; i++) {
setTimeout(async () => {
await sendSticker(sock, phone, links[i]);
}, i * 3000); // 3000 milliseconds = 3 seconds
}
} catch (error) {
logger.error(`Bot Blocked || Something Wrong`);
}
});
bot.launch();
// bot.command('/ban