【问题标题】:Telethon write a message/start a chat after button - bot send message before /startTelethon 在按钮后写消息/开始聊天 - 机器人在 /start 之前发送消息
【发布时间】:2020-10-18 22:35:11
【问题描述】:

我正在尝试使用 Telethon 为电报编写访问机器人。

  1. 用户已添加到群组或通过邀请链接加入。
  2. 受到限制,必须按下按钮。
  3. 机器人会写一条简单的验证码消息,例如“1+2=?”
  4. 如果正确,则此人不受限制,如果错误,则被踢出...

一切都很好,但是:
如果新加入的用户没有先写“/start”,则用户不会收到来自机器人的消息。

这个人有没有可能收到消息?

我读过在某处不可能做到这一点,但是外面有机器人怎么做?

【问题讨论】:

  • 您需要将验证码发送到群组,因为机器人无法开始新对话
  • 但是应该有可能通过私信做到这一点?有一些频道/组以这种方式处理它?
  • 使用私人消息的唯一方法是用户在加入群组之前/启动机器人,据我所知,没有群组在做这样的事情。

标签: python telegram telegram-bot telethon


【解决方案1】:

您可以检测用户何时加入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()

【讨论】:

  • 哇,完美!我认为 url 按钮的部分是我正在搜索的。我将对其进行测试并提供反馈。非常感谢@Lonami ...最后几天的大力支持,以及对不错的模块的大概率:D
  • 如果它解决了问题,请确保将答案标记为已接受,如果它有用,请确保将其标记为已接受,它可以帮助我并帮助其他人找到此答案;)
  • 完美运行!再次感谢你。一个简短的问题要理解...?start=captcha' 如果您已经在机器人打开的情况下进行了转换,则不会每次都开始。最好放在 start 里面(如果你有两个不同的 /start 用例,不要用 ````pattern= start captcha`` 这样做,但是有一个额外的参数来检查函数处理程序,比如@987654330 @ 还是我错过了什么?因为就像我说的那样,它也会触发 /start ,有时会同时触发 ...
  • 您可以使用?“运算符”使部分模式可选,例如( captcha)?
  • @MaTok 不错的答案!我有一个类似的问题,但在执行上略有不同。你能帮我在这里吗? stackoverflow.com/questions/70164049/…
猜你喜欢
  • 1970-01-01
  • 2018-08-20
  • 1970-01-01
  • 1970-01-01
  • 2020-02-21
  • 2016-04-13
  • 2018-01-09
  • 1970-01-01
  • 2020-06-13
相关资源
最近更新 更多