如何解决Telethon在按钮之后写消息/开始聊天-机器人在/ start之前发送消息
我正在尝试用telethon编写访问机器人进行电报。
- 用户已添加到群组或通过邀请链接加入。
- 受到限制,必须按下按钮。
- 该机器人编写了一个简单的验证码消息,例如“ 1 + 2 =?”
- 如果是对的话,这个人会不受限制,如果错了,会被踢出去...
一切正常,但是:
如果新加入的用户并没有先写“ / start”,则该用户不会从漫游器收到消息。
该人是否仍然有可能收到该消息?
我读到不可能在某个地方这样做,但是外面有机器人如何做到这一点?
解决方法
您可以检测到用户何时加入events.ChatAction
,并限制他们立即使用client.edit_permissions
发送消息。在小组中,您可以通过带有按钮的消息让他们知道,他们必须私下解决该机器人的验证码,您可以使用events.Message
对此做出反应。
from telethon import TelegramClient,Button,events
bot = TelegramClient(...)
async def main():
async with bot:
# Needed to find the username of the bot itself,# to link users to a private conversation with it.
me = await bot.get_me()
@bot.on(events.ChatAction)
async def handler(event):
if event.user_joined:
# Don't let them send messages
await bot.edit_permissions(event.chat_id,event.user_id,send_messages=False)
# Send a message with URL button to start your bot with parameter "captcha"
url = f'https://t.me/{me.username}?start=captcha'
await event.reply(
'Welcome! Please solve a captcha before talking',buttons=Button.url('Solve captcha',url))
# Detect when people start your bot with parameter "captcha"
@bot.on(events.NewMessage(pattern='/start captcha'))
async def handler(event):
# Make them solve whatever proof you want here
await event.respond('Please solve this captcha: `1+2 = ?`')
# ...more logic here to handle the rest or with more handlers...
await bot.run_until_disconnected()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。