如何以编程方式向@BotFather发送消息? [英] How can I send a message to @BotFather programmatically?

查看:317
本文介绍了如何以编程方式向@BotFather发送消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I know that a bot can be created in a telegram if you send the command "/newbot" to the bot "@BotFather".
But to do this, you need to take your device, open telegram, open a chat with "@BotFather" and send him the command "/newbot".
Can all of the above be done programmatically ?
P.S.: this is not laziness, but an attempt to optimize the solution.

推荐答案

是的,可以与mtproto库(pyrogram,telethon,madelineproto等)创建此类交互

yes, it's possible to create such interaction with mtproto libraries (pyrogram, telethon, madelineproto, etc...)

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

Here is a PoC script using telethon (python3 -m pip install -U telethon first to install the dependency):

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的文档.

You acquire the app_id and app_hash values from https://my.telegram.org/ and here is telethon's doc.

这篇关于如何以编程方式向@BotFather发送消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆