如何以编程方式向@BotFather发送消息? [英] How can I send a message to @BotFather programmatically?
问题描述
我知道,如果您将命令"/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_id
和app_hash
值.一个>
这是Telethon的文档.
You acquire the app_id
and app_hash
values from https://my.telegram.org/
and here is telethon's doc.
这篇关于如何以编程方式向@BotFather发送消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!