You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Pyrogram如何发送带参数的/start命令或跳转指定TG链接?

解决Telegram机器人带参数/start命令的程序实现方法

直接发送带参数的/start命令

client.join_chat()是用于加入群组或频道的方法,不能用来和机器人对话,这也是你收到「无效用户名」错误的原因。要给机器人发送带参数的/start命令,直接用发送消息的API即可:

示例(Pyrogram客户端)

from pyrogram import Client
import asyncio

async def send_start_with_param():
    # 替换为你的Telegram客户端配置
    app = Client("your_session_name", api_id=12345, api_hash="your_api_hash")
    await app.start()
    # 发送带参数的/start命令到目标机器人
    await app.send_message("Bot_name", "/start mc_dSs8EMrECY1Jvwk")
    await app.stop()

asyncio.run(send_start_with_param())

示例(python-telegram-bot框架)

如果你是用自己的机器人转发命令,也可以通过机器人API发送:

from telegram import Bot
import asyncio

async def send_start_command():
    bot = Bot(token="your_bot_token")
    # 发送带参数的/start命令到目标机器人
    await bot.send_message(chat_id="Bot_name", text="/start mc_dSs8EMrECY1Jvwk")
    await bot.close()

asyncio.run(send_start_command())

正确设置跳转链接的InlineButton

之前的InlineButton无反应,大概率是机器人未处理带参数的/start逻辑,或者链接格式有误。以下是正确的按钮配置方式:

示例(python-telegram-bot)

from telegram import InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import Updater, CommandHandler

def show_button(update, context):
    # 构造带参数的跳转链接
    start_url = "https://t.me/Bot_name?start=mc_dSs8EMrECY1Jvwk"
    keyboard = [[InlineKeyboardButton("打开机器人对话", url=start_url)]]
    reply_markup = InlineKeyboardMarkup(keyboard)
    update.message.reply_text("点击按钮跳转:", reply_markup=reply_markup)

def main():
    updater = Updater("your_bot_token", use_context=True)
    updater.dispatcher.add_handler(CommandHandler("showbtn", show_button))
    updater.start_polling()
    updater.idle()

if __name__ == "__main__":
    main()

关键注意事项

  • 目标机器人必须提前开发处理/start <参数>的逻辑,否则即使发送了带参数的命令,也不会有预期响应。
  • 链接中的Bot_name必须是目标机器人的准确用户名(可带@或不带,Telegram都能识别)。

内容的提问来源于stack exchange,提问作者Михаил Гордеев

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 01:50:14