-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
267 lines (227 loc) · 7.4 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
const { Telegraf } = require("telegraf");
require("dotenv").config();
const express = require("express");
const app = express();
const { Schema, model, default: mongoose } = require("mongoose");
const { v1: uuidv1 } = require("uuid");
const bodyParser = require("body-parser");
const cors = require("cors");
const bot = new Telegraf(process.env.BOT_TOKEN);
const Queue = require("queue-promise");
const User = require("./models/userModel");
const checkIfUserExists = require("./helpers/checkIfUserExists");
const showAccountInfo = require("./helpers/showAccountInfo");
const dontReferYourself = require("./helpers/dontReferYourself");
const hasBeenReferred = require("./helpers/hasBeenReferred");
const checkForIncompleteTasks = require("./helpers/checkForIncompleteTasks");
const checkMembership = require("./helpers/checkMembership");
const setReferredString = require("./helpers/setReferredString");
const showLeaderboard = require("./helpers/showLeaderboard");
const handleReferral = require("./helpers/handleReferral");
// Create a queue instance
const queue = new Queue({
concurrent: 25, // Process one request at a time
interval: 3000, // Interval between dequeue operations (1 second)
});
app.use(
cors({
origin: "*",
})
);
// Parse URL-encoded bodies (deprecated in Express v4.16+)
app.use(bodyParser.urlencoded({ extended: false }));
// Parse JSON bodies
app.use(express.json());
app.get("/", (req, res) => {
res.send("Hello World");
});
const port = process.env.PORT || 8933;
mongoose
.connect(process.env.URI)
.then(() => {
app.listen(port, () => {
console.log(`App is listening on port ${port}`);
});
console.log("Connected to db.");
//Send the leaderboard stats every 20 minutes
const interval = 20 * 60 * 1000; //
setInterval(async () => {
queue.enqueue(async () => {
await showLeaderboard(bot);
});
}, interval);
})
.catch((err) => {
console.log(`Error connecting to db: ${err}`);
});
bot.start(async (ctx) => {
try {
const { chat } = ctx.message;
if (chat.type != "private") {
return ctx.reply("Please use that command in a private chat.");
}
queue.enqueue(async () => {
const userId = ctx.from.id;
let inviteId = ctx.payload;
let linkFirstChunk = "t.me/example_Bot?start=";
//Check if user came via an invite link
if (inviteId) {
//Check if user has incomplete tasks
await checkForIncompleteTasks(ctx, userId, true); //Use these arguments when they click a link
//Check if link is valid
const linkOwnerData = await User.findOne({
referralLink: linkFirstChunk + inviteId,
});
//If the link is valid
if (linkOwnerData) {
//Check if user tried to refer themselves.
if (linkOwnerData.chatId == userId) {
return await dontReferYourself(ctx, linkOwnerData);
}
//If they didn't refer themselves, but they already have an account
const userData = await checkIfUserExists(userId);
if (userData) {
return await showAccountInfo(ctx, userData, true);
}
//Hasn't done any of the above, so handle the referral
return await handleReferral(ctx, linkOwnerData);
}
//If the link is invalid
if (!linkOwnerData) {
return ctx.reply(
"Sorry that link is invalid. Please check and try again."
);
}
}
await checkForIncompleteTasks(ctx, userId); //Use these arguments if user started the bot but have incomplete tasks
//If user already exists but they didn't click a link, show their account details
const userData = await checkIfUserExists(userId);
if (userData) {
return showAccountInfo(ctx, userData);
}
const replyText = `
This example bot rewards users for referrals. To begin, complete the tasks below:`;
const replyMarkup = {
reply_markup: {
inline_keyboard: [
[{ text: "Join our group", url: "t.me/examplebotgroup" }],
[{ text: "Join our channel", url: "t.me/examplebotchannel" }],
[
{
text: "Follow our instagram",
url: "https://www.instagram.com/exampleinstagram",
},
],
[
{
text: "Click me when you're done",
callback_data: "send_link",
},
],
],
},
};
ctx.reply(replyText, { ...replyMarkup });
});
} catch (error) {
console.log(error);
ctx.reply("An error occured. Please try again.");
}
});
bot.action("send_link", async (ctx) => {
queue.enqueue(async () => {
try {
//If user clicked the button after account creation
const userData = await checkIfUserExists(ctx.from.id);
if (userData) {
return showAccountInfo(ctx, userData);
}
//Check if user has completed all tasks
const result = await checkMembership(ctx, ["@examplebotgroup", "@examplebotchannel"]);
console.log(result);
//If join check failed
if (!result.success) {
return ctx.reply("An error occured, please try again.");
}
//If user hasn't joined TG channel
if (!result.joined) {
return ctx.reply(
"You haven't completed the tasks, please join all our socials to continue."
);
}
const newReferralLink = `t.me/example_Bot?start=${uuidv1()}`;
const newUser = new User({
chatId: ctx.from.id,
referralLink: newReferralLink,
referredUsers: [],
referralCount: 0,
username: ctx.from.username,
});
await newUser.save();
ctx.reply(
`This is your referral link\n\n\`${newReferralLink}\`\n\nKeep sharing it to refer users`,
{ parse_mode: "Markdown" }
);
} catch (error) {
console.log(error);
}
});
});
bot.command("myinvite", async (ctx) => {
queue.enqueue(async () => {
try {
const { chat } = ctx.message;
// Access the message object
const message = ctx.message;
// Get the message ID
const messageId = message.message_id;
const userDetails = await User.findOne({ chatId: ctx.from.id });
if (!userDetails) {
return ctx.reply(
"You have no account yet. Please use the /start command in a private chat and complete the tasks to continue."
);
}
const referred = userDetails.referralCount;
const replyText = `
Username: *${userDetails.username}*
Your have referred: *${setReferredString(referred)}*
Keep sharing your link with others.
\`${userDetails.referralLink}\` _(Tap to copy)_
`;
ctx.reply(replyText, {
reply_to_message_id: messageId,
parse_mode: "Markdown",
});
} catch (error) {
console.log(error);
ctx.reply("An error occured. Please try again.");
}
});
});
bot.command("invite", async (ctx) => {
queue.enqueue(async () => {
await showLeaderboard(bot, ctx);
});
});
// Handle incoming messages
bot.on("message", async (ctx) => {
queue.enqueue(async () => {
if (ctx.message.text === "/invite") {
// Reply to the user who sent the /invite command
await showLeaderboard(bot, ctx);
}
});
});
// Set bot commands for Telegram
bot.telegram.setMyCommands([
{ command: "start", description: "Start the example Bot" },
{
command: "myinvite",
description: "Check your invite status",
},
{
command: "invite",
description: "See the referral leaderboard",
},
]);
bot.launch();