-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathapp.js
146 lines (120 loc) · 4.54 KB
/
app.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
const { Telegraf } = require('telegraf')
const config = require("./config");
const LocalSession = require('telegraf-session-local')
const { generateBtns } = require('./utils')
const { EmojiCaptcha, CaptchaStatus } = require('./core')
const bot = new Telegraf(config.core.botToken)
bot.use((new LocalSession({
database: 'captcha_db.json',
property: 'session',
getSessionKey: (ctx) => {
if (ctx.callbackQuery) {
return `${ctx.callbackQuery.from.id}:${ctx.callbackQuery.message.chat.id}`
} else if (ctx.from && ctx.chat) {
return `${ctx.from.id}:${ctx.chat.id}`
}
return undefined
}
})).middleware())
// bot.use(async (ctx, next) => {
// ctx.state.captcha = Object.keys(ctx.session).length === 0 ? new EmojiCaptcha() : EmojiCaptcha.from(ctx.session)
// const result = await next()
// ctx.session = JSON.parse(JSON.stringify(ctx.state.captcha))
// return result
// })
bot.on('message', async (ctx) => {
if (ctx.message && ctx.message.new_chat_members && ctx.message.new_chat_members.length) {
for (const newMember of ctx.message.new_chat_members) {
try {
if (newMember.is_bot) {
await ctx.replyWithHTML(`@${newMember.username} is a bot.`)
} else {
await ctx.telegram.restrictChatMember(ctx.message.chat.id, newMember.id)
const introductionMessage = await ctx.replyWithHTML(`Dear <${newMember.first_name}>, new members are automatically muted.
Please solve the following captcha to be unmuted.
`)
ctx.state.captcha = Object.keys(ctx.session).length === 0 ? new EmojiCaptcha() : EmojiCaptcha.from(ctx.session)
ctx.session = JSON.parse(JSON.stringify(ctx.state.captcha))
if (ctx.state.captcha.status === CaptchaStatus.PASS) {
await ctx.reply('You have already passed the captcha.')
} else if (ctx.state.captcha.status === CaptchaStatus.FAILED) {
await ctx.reply('You have failed the captcha.')
} else if (ctx.state.captcha.status === CaptchaStatus.EXPIRED) {
await ctx.reply('The captcha has expired.')
} else {
const challengeMessage = await ctx.reply(
`
Please select the emojis you see here:
${ctx.state.captcha.presentedEmojis.map(x => x.hex).join('-')}
<b>Attempts left:</b> ${ctx.state.captcha.attemptsLeft}
`,
generateBtns(ctx.state.captcha.choices)
)
setTimeout(async () => {
await ctx.telegram.deleteMessage(challengeMessage.chat.id, challengeMessage.message_id)
await ctx.replyWithHTML(`Challenge has expired. Manual unmute is required.`, {
reply_to_message_id: introductionMessage.message_id
})
}, config.textEmojiChallenge.timeout)
}
}
} catch (err) {
console.error(err)
}
}
}
})
// bot.on('message', async (ctx) => {
// if (ctx.state.captcha.status === CaptchaStatus.PASS) {
// await ctx.reply('You have already passed the captcha.')
// } else if (ctx.state.captcha.status === CaptchaStatus.FAILED) {
// await ctx.reply('You have failed the captcha.')
// } else if (ctx.state.captcha.status === CaptchaStatus.EXPIRED) {
// await ctx.reply('The captcha has expired.')
// } else {
// await ctx.reply(
// `
// Please select the emojis you see here:
// ${ctx.state.captcha.presentedEmojis.map(x => x.hex).join('-')}
// <b>Attempts left:</b> ${ctx.state.captcha.attemptsLeft}
// `,
// generateBtns(ctx.state.captcha.choices)
// )
// }
// })
bot.action(/([a-fA-F0-9]{8,16})\.png/, async ctx => {
try {
const isCorrect = ctx.state.captcha.check(ctx.match[0])
if (isCorrect) {
await ctx.answerCbQuery('✅ That\'s correct!')
} else {
await ctx.editMessageText(
`
Please select the emojis you see here:
${ctx.state.captcha.presentedEmojis.map(x => x.hex).join('-')}
<b>Attempts left:</b> ${ctx.state.captcha.attemptsLeft}
`,
{
reply_markup: ctx.callbackQuery.message.reply_markup,
parse_mode: 'HTML'
}
)
await ctx.answerCbQuery('❌ That\'s incorrect! focus!')
}
} catch (error) {
if (error.message === 'no attempts left') {
await ctx.editMessageText('You have failed the captcha.')
} else {
console.error(error)
}
}
})
// await ctx.reply(
// `
// Please select the emojis you see here:
// ${ctx.state.captcha.presentedEmojis.map(x => x.hex).join('-')}
// <b>Attempts left:</b> ${ctx.state.captcha.attemptsLeft}
// `,
// generateBtns(ctx.state.captcha.choices)
// )
bot.launch()