Expand/minimize text in message #4267
Replies: 3 comments 2 replies
|
Hello, this feature is not supported by Telegram. They may include this in the future - https://t.me/designers/232 |
|
Hi you can not catch press updates on certain words that is not a thing You can attach an inline button and when pressing on that you can edit the message. |
|
This is a classic "accordion / expandable message" pattern. In Telegram, you can't do true DOM-style expand/collapse, but you can simulate it perfectly using The concept:
Code example: from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import ApplicationBuilder, CallbackQueryHandler, ContextTypes
TOKEN = "YOUR_TOKEN"
# Your event data
EVENTS = {
"event_1": {
"title": "📅 Event 1",
"details": (
"🎉 *Community Meetup*\n"
"📍 Location: Central Park\n"
"🕐 Time: Saturday, 5 PM\n"
"📝 Bring your ID and enthusiasm!\n"
"Contact @admin for more info."
)
},
"event_2": {
"title": "📅 Event 2",
"details": (
"🏆 *Quiz Night*\n"
"📍 Location: The Tavern\n"
"🕐 Time: Friday, 8 PM\n"
"📝 Teams of 4. Prize: ₹500 voucher.\n"
"Register by Thursday!"
)
},
}
def collapsed_keyboard():
"""All events shown as collapsed buttons."""
return InlineKeyboardMarkup([
[InlineKeyboardButton(data["title"], callback_data=f"expand_{key}")]
for key, data in EVENTS.items()
])
def expanded_keyboard(expanded_key: str):
"""One event expanded, rest collapsed."""
rows = []
for key, data in EVENTS.items():
if key == expanded_key:
# This one is open — show a collapse button for it
rows.append([InlineKeyboardButton("▲ Collapse", callback_data=f"collapse_{key}")])
else:
rows.append([InlineKeyboardButton(data["title"], callback_data=f"expand_{key}")])
return InlineKeyboardMarkup(rows)
def build_message_text(expanded_key: str | None) -> str:
if expanded_key is None:
return "Tap an event to see details:"
return f"Tap an event to see details:\n\n{EVENTS[expanded_key]['details']}"
async def handle_callback(update: Update, context: ContextTypes.DEFAULT_TYPE):
query = update.callback_query
await query.answer()
data = query.data
if data.startswith("expand_"):
key = data.removeprefix("expand_")
if key not in EVENTS:
return
await query.edit_message_text(
text=build_message_text(key),
reply_markup=expanded_keyboard(key),
parse_mode="Markdown"
)
elif data.startswith("collapse_"):
key = data.removeprefix("collapse_")
await query.edit_message_text(
text=build_message_text(None),
reply_markup=collapsed_keyboard(),
parse_mode="Markdown"
)
if __name__ == "__main__":
app = ApplicationBuilder().token(TOKEN).build()
app.add_handler(CallbackQueryHandler(handle_callback))
app.run_polling()How "auto-collapse on expand another" works: Notice To trigger the initial message from a button press, just have your existing handler do: await context.bot.send_message(
chat_id=update.effective_chat.id,
text="Tap an event to see details:",
reply_markup=collapsed_keyboard()
)This keeps the chat clean — one message, no clutter, expands/collapses in place. |
Uh oh!
There was an error while loading. Please reload this page.
Hi all.
My bot posts a message in response to a button press in a group. This message contains a fair amount of text and over time the chat looks cluttered.
What I'm hoping for is a feature where I can replace the text with the word 'event' which when pressed will expand the original text that link represents in that message. When the user moves away from that message and selects something else that text minimizes back to it's 'event' link again.
Would there be a way to do this and if there is a code example would be greatly appreciated.
Thanks and regards.
All reactions