如何以编程方式向@BotFather发送消息?

时间:2020-04-25 21:39:52

标签: bots telegram telegram-bot node-telegram-bot-api

我知道,如果您将命令“ / newbot”发送到机器人“ @BotFather”,则可以在电报中创建一个机器人。
但是,为此,您需要拿起设备,打开电报,与“ @BotFather”打开聊天,然后向他发送命令“ / newbot”。
可以通过编程方式完成上述所有操作吗?
附注:这不是懒惰,而是优化解决方案的尝试。

1 个答案:

答案 0 :(得分:1)

是的,可以与mtproto库(热解图,telethon,madelineproto等...)创建此类交互

以下是使用telethon的PoC脚本(首先安装python3 -m pip install -U telethon来安装依赖性):

from telethon import TelegramClient, events

api_id = ...
api_hash = "..."
client = TelegramClient('session', api_id, api_hash)

BOT_NAME="..."
BOT_USER_NAME="..." # must end with -bot

@client.on(events.NewMessage)
async def message_handler(event):
    if 'Please choose a name for your bot' in event.raw_text:
        await event.reply(BOT_NAME)
    elif 'choose a username for your bot' in event.raw_text:
        await event.reply(BOT_USER_NAME)
    elif 'Done! Congratulations on your new bot' in event.raw_text:
        print("Bot created!")
        await client.disconnect()

async def main():
    await client.send_message('botfather', '/newbot')


with client:
    client.loop.run_until_complete(main())
    client.run_until_disconnected()

您从https://my.telegram.org/获取了app_idapp_hash值 这是Telethon的doc

相关问题