如何移除Telebot选择聊天后referral link前的机器人用户名?
解决方案:移除Inline模式消息中的机器人用户名前缀
问题原因
当使用switch_inline_query参数时,Telegram的Inline模式会自动在输入框内容前追加@BotUsername,这是平台的默认行为,因此无论使用telebot还是aiogram都会出现该问题,无法通过修改按钮参数直接规避。
可行解决方案
方案1:改用回调按钮+机器人主动发送链接(推荐)
跳过Inline模式的输入流程,让机器人直接在目标聊天中发送纯链接。将按钮类型改为回调按钮,配合回调处理逻辑实现:
Telebot 示例代码
def switch(message): markup = types.InlineKeyboardMarkup() bot_name = bot.get_me().username target_link = ref_link.format(bot_name, message.chat.id) # 用回调数据传递目标链接 callback_button = types.InlineKeyboardButton(text='Try', callback_data=f"send_link:{target_link}") markup.add(callback_button) bot.send_message(message.chat.id, "Выбрать чат", reply_markup=markup) # 处理回调请求 @bot.callback_query_handler(func=lambda call: call.data.startswith('send_link:')) def handle_send_link(call): target_link = call.data.split(':', 1)[1] # 提示用户输入目标聊天ID bot.send_message(call.message.chat.id, "请输入目标聊天的ID:", reply_markup=types.ForceReply()) # 处理用户输入的聊天ID并发送链接 @bot.message_handler(func=lambda msg: msg.reply_to_message and msg.reply_to_message.text == "请输入目标聊天的ID:") def send_to_target_chat(message): try: chat_id = int(message.text) # 实际场景建议用用户会话存储链接,此处简化处理 target_link = "存储的目标链接" bot.send_message(chat_id, target_link) bot.send_message(message.chat.id, "链接已发送!") except ValueError: bot.send_message(message.chat.id, "无效的聊天ID,请输入数字格式的ID。")
Aiogram 示例代码
from aiogram import Bot, Dispatcher, types from aiogram.filters import Command bot = Bot(token="YOUR_TOKEN") dp = Dispatcher() # 发送选择按钮 @dp.message(Command("switch")) async def switch(message: types.Message): bot_name = (await bot.get_me()).username target_link = ref_link.format(bot_name, message.chat.id) markup = types.InlineKeyboardMarkup( inline_keyboard=[[types.InlineKeyboardButton(text='Try', callback_data=f"send_link:{target_link}")]] ) await message.answer("Выбрать чат", reply_markup=markup) # 处理回调请求 @dp.callback_query(lambda call: call.data.startswith('send_link:')) async def handle_send_link(call: types.CallbackQuery): target_link = call.data.split(':', 1)[1] # 将链接存入用户会话 await dp.storage.set_data(user=call.from_user.id, data={"target_link": target_link}) await call.message.answer("请输入目标聊天的ID:", reply_markup=types.ForceReply()) # 处理聊天ID输入并发送链接 @dp.message(lambda msg: msg.reply_to_message and msg.reply_to_message.text == "请输入目标聊天的ID:") async def send_to_target_chat(message: types.Message): try: chat_id = int(message.text) user_data = await dp.storage.get_data(user=message.from_user.id) target_link = user_data.get("target_link") if target_link: await bot.send_message(chat_id, target_link) await message.answer("链接已发送!") except ValueError: await message.answer("无效的聊天ID,请输入数字格式的ID。") if __name__ == "__main__": import asyncio asyncio.run(dp.start_polling(bot))
方案2:通过Inline查询结果返回纯链接
保留原有的“选择聊天”交互流程,利用Inline查询结果让用户发送纯链接:
Telebot 示例代码
def switch(message): markup = types.InlineKeyboardMarkup() # 传入空字符串触发Inline查询 switch_button = types.InlineKeyboardButton(text='Try', switch_inline_query="") markup.add(switch_button) bot.send_message(message.chat.id, "Выбрать чат", reply_markup=markup) # 处理Inline查询,返回纯链接的结果选项 @bot.inline_handler(lambda query: query.query == "") def inline_query_handler(query): bot_name = bot.get_me().username target_link = ref_link.format(bot_name, query.from_user.id) # 构造Inline结果,用户选择后发送纯链接 result = types.InlineQueryResultArticle( id='1', title='发送邀请链接', input_message_content=types.InputTextMessageContent(message_text=target_link) ) bot.answer_inline_query(query.id, [result])
Aiogram 示例代码
@dp.message(Command("switch")) async def switch(message: types.Message): markup = types.InlineKeyboardMarkup( inline_keyboard=[[types.InlineKeyboardButton(text='Try', switch_inline_query="")]] ) await message.answer("Выбрать чат", reply_markup=markup) # 处理Inline查询 @dp.inline_query(lambda query: query.query == "") async def inline_query_handler(query: types.InlineQuery): bot_name = (await bot.get_me()).username target_link = ref_link.format(bot_name, query.from_user.id) result = types.InlineQueryResultArticle( id='1', title='发送邀请链接', input_message_content=types.InputTextMessageContent(message_text=target_link) ) await bot.answer_inline_query(query.id, [result])
方案对比
- 方案1:无需用户额外操作,机器人直接完成发送,流程更简洁;但需要用户提供目标聊天ID(或通过其他方式自动获取)。
- 方案2:保留原有的“选择聊天”交互逻辑,用户体验更连贯;但需要用户额外点击一次Inline结果选项。
内容的提问来源于stack exchange,提问作者dARRNT
相关产品推荐
相关产品推荐

