Replies: 2 comments 2 replies
|
No buttons are always attached to a messages. Though since you said channel, I am not sure if either is_persistent might make it not quote in a new message. Or maybe setting the bot commands in botfather/via set_my_commands? |
|
The issue is What you actually want is But there's a conceptual mismatch here: you want persistent shortcut buttons that are always visible regardless of which message is active. That's not really how Telegram channels work — buttons always belong to a specific message. The closest clean solution: a pinned "shortcuts" message Post one dedicated shortcuts message, pin it, and keep its buttons always fresh by editing it on bot restart. from telegram import InlineKeyboardButton, InlineKeyboardMarkup
SHORTCUTS_MSG_ID = None # store this persistently (file/db/env)
CHANNEL_ID = "@yourchannel"
def shortcuts_markup():
return InlineKeyboardMarkup([
[
InlineKeyboardButton("Cmd 1", callback_data="cmd1"),
InlineKeyboardButton("Cmd 2", callback_data="cmd2"),
],
[
InlineKeyboardButton("Cmd 3", callback_data="cmd3"),
]
])
async def post_or_refresh_shortcuts(bot):
global SHORTCUTS_MSG_ID
if SHORTCUTS_MSG_ID:
# Bot restarted — just refresh the buttons on existing message
try:
await bot.edit_message_reply_markup(
chat_id=CHANNEL_ID,
message_id=SHORTCUTS_MSG_ID,
reply_markup=shortcuts_markup()
)
return
except Exception:
pass # Message was deleted, fall through to re-post
# Post fresh
msg = await bot.send_message(
chat_id=CHANNEL_ID,
text="⚡ Quick Commands",
reply_markup=shortcuts_markup()
)
SHORTCUTS_MSG_ID = msg.message_id
# Persist this ID somewhere so it survives restarts
save_msg_id(SHORTCUTS_MSG_ID) # your own helper — json file, db, etc.
await bot.pin_chat_message(
chat_id=CHANNEL_ID,
message_id=SHORTCUTS_MSG_ID,
disable_notification=True
)Call Handling the button presses: Since it's a channel and inline buttons fire from telegram.ext import CallbackQueryHandler
async def handle_shortcut(update, context):
query = update.callback_query
await query.answer() # removes the loading spinner
if query.data == "cmd1":
await context.bot.send_message(chat_id=CHANNEL_ID, text="cmd1 result...")
elif query.data == "cmd2":
await context.bot.send_message(chat_id=CHANNEL_ID, text="cmd2 result...")
app.add_handler(CallbackQueryHandler(handle_shortcut))Why not
|
Uh oh!
There was an error while loading. Please reload this page.
I would like to attach some buttons to the bottom of the TG channel, as a shortcut to save time on typing.
I'm using bot.send_message() with parameters chat_id, text, parse_mode, reply_markup.
The reply_markup code is about like this:
To ensure the buttons are there, also when the bot restarts, is added to a channel etc, I set them for every message sent by the bot.
The buttons do appear in the channel, but the original bot message is included as a quotes message that the a button press replies to.
Can I change this, so the buttons are just shortcuts for commands, and not replies to specific bot messages?
All reactions