本文最后更新于 2026年5月11日。
开发教程
https://blog.csdn.net/xirigh/article/details/125826591 很清晰,但会报错
https://juejin.cn/post/7163189744795385863 # 创建一个Python Telegram机器人(附代码)
下面这个是最新版的教程
https://krau.top/posts/tg-bot-dev-note-kmua
https://docs.python-telegram-bot.org/en/v20.7/telegram.ext.applicationbuilder.html#telegram.ext.ApplicationBuilder.build
https://docs.python-telegram-bot.org/en/v20.7/examples.echobot.html 实例教程
GitHub开源机器人
https://github.com/anasty17/mirror-leech-telegram-bot
https://github.com/n3d1117/chatgpt-telegram-bot 继承chatgpt
https://github.com/MRK-YT/Rose-Bot 或许可以一试需要数据库
https://github.com/eternnoir/pyTelegramBotAPI 一个API
开发前的准备工作
开发语言的选择
电报机器人是通过API的形式提供服务的,对语言没有要求,只要能做到可调用http的api即可开发。 我们先讲解使用python开发的教程。使用Node开发的教程以后再写。
python环境搭建
1.python的安装:安装的教程网上很多了 ,这里就不赘述了。 2.安装开发库:pip install -U pyrogram tgcrypto
机器人token申请
在telegram中搜索@BotFather, 输入命令: /start 可以获取交互的命令说明。
创建电报的web app
输入网址 https://my.telegram.org/apps 如果没有登录过,会弹出输入输入账号的界面,在电报的客户端会收到一条验证码,填入后即可登录成功。然后根据提示,创建一个web app。成功后会显示 api_id和api_hash,要记录下来并不要泄露。
Done! Congratulations on your new bot. You will find it at t.me/WeiyounBot. You can now add a description, about section and profile picture for your bot, see /help for a list of commands. By the way, when you’ve finished creating your cool bot, ping our Bot Support if you want a better username for it. Just make sure the bot is fully operational before you do this.
Use this token to access the HTTP API:
6863673453:AAGkjk1GF_JlkxpluSMhCx9H4n4kfIA7w-E
Keep your token secure and store it safely, it can be used by anyone to control your bot.
For a description of the Bot API, see this page: https://core.telegram.org/bots/api
创建机器人
在 Telegram 应用中通过与 @BotFather 交谈来创建自己的机器人,具体方式跟着引导来操作就可以了,需要注意的是每个机器人都会对应一个 Token,机器人的所有信息交互都会用到它,所以要保管好不要泄漏,避免其他人也能使用你的机器人做事情,创建机器人后可用链接 https://api.telegram.org/bot<token>/getMe 在浏览器里查看机器人信息,并能够看到以下反馈信息。
{
"ok": true,
"result": {
"id": 985878205,
"is_bot": true,
"first_name": " iOS 团队",
"username": "WandaFilm_Bot"
}
}
我的机器人可以使用下面的链接查询:
https://api.telegram.org/bot6863673453:AAGkjk1GF_JlkxpluSMhCx9H4n4kfIA7w-E/getMe
使用第三方库官方文档
https://github.com/python-telegram-bot/python-telegram-bot?tab=readme-ov-file
https://docs.python-telegram-bot.org/en/v20.7/
https://python-telegram-bot.org/
今天在网上查资料,质量良莠不齐,好不容易找到一些规范的代码发现也报错,因为网上的今年最新的教程还是python-telegram-bot 13.0版本的,而我安装默认最新是python-telegram-bot 20.0版本的。
所以还是要找官方根源文档。
在telegram官方api文档没有找到python的开发教程,在github找到python-telegram-bot的代码库,然后找到示例代码才正常运行起来(填入自己的机器人 token,本地开启全局代理,不然会报超时错误)。
“`python
import logging
from telegram import Update
from telegram.ext import ApplicationBuilder, ContextTypes, CommandHandler
logging.basicConfig(
format=’%(asctime)s – %(name)s – %(levelname)s – %(message)s’,
level=logging.INFO
)
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
await context.bot.send_message(chat_id=update.effective_chat.id, text=”I’m a bot, please talk to me!”)
if name == ‘main‘:
# application = ApplicationBuilder().token(‘TOKEN’).build()
application = ApplicationBuilder().token(‘6863673453:AAGkjk1GF_JlkxpluSMhCx9H4n4kfIA7w-E’).build()
start_handler = CommandHandler(‘start’, start)
application.add_handler(start_handler)
application.run_polling()
“`