如何从单条Telegram消息拆分表情符号并生成对应按钮
Got it, let's break this down into actionable steps. I'll use Python with the python-telegram-bot library (works for v13.x and v20+ versions) since it's the go-to tool for building Telegram bots.
Step 1: Extract Emojis from the Message
First, we need to pull all emojis out of a Telegram message. Emojis live in specific Unicode ranges, so a regex pattern will help us match them reliably.
Here's a helper function to do the extraction:
import re def extract_emojis(text): # Regex pattern covering most common Unicode emoji ranges emoji_pattern = re.compile( "[" u"\U0001F600-\U0001F64F" # Emoticons u"\U0001F300-\U0001F5FF" # Symbols & pictographs u"\U0001F680-\U0001F6FF" # Transport & map symbols u"\U0001F1E0-\U0001F1FF" # Country flags u"\U00002500-\U00002BEF" # Misc symbols u"\U00002702-\U000027B0" u"\U0001f926-\U0001f937" u"\ufe0f" # Variation selector for colored emojis "]+", flags=re.UNICODE ) return emoji_pattern.findall(text)
Step 2: Generate Inline Buttons for the Emojis
Once we have our list of emojis, we can create inline buttons (the ones that appear directly below messages) for the first 4 emojis.
Here's how to build the button keyboard:
from telegram import InlineKeyboardButton, InlineKeyboardMarkup def generate_emoji_buttons(emojis): # Grab the first 4 emojis (adjust the slice if you need a different number) selected_emojis = emojis[:4] # Create a button for each emoji, with unique callback data buttons = [[InlineKeyboardButton(emoji, callback_data=f"emoji_clicked_{emoji}")] for emoji in selected_emojis] # Convert to markup format to attach to a reply message return InlineKeyboardMarkup(buttons)
Step 3: Put It All Together in a Bot Handler
Now let's integrate these functions into a bot message handler. When a user sends a message with emojis, the bot will extract them and send back the 4 buttons.
from telegram.ext import Updater, MessageHandler, Filters, CallbackQueryHandler def handle_message(update, context): message_text = update.effective_message.text emojis = extract_emojis(message_text) if not emojis: update.effective_message.reply_text("Whoops, no emojis found in your message!") return if len(emojis) < 4: update.effective_message.reply_text(f"Only found {len(emojis)} emojis—need at least 4 to make the buttons!") return keyboard = generate_emoji_buttons(emojis) update.effective_message.reply_text("Here are your emoji buttons:", reply_markup=keyboard) # Optional: Add a handler to respond when users click the buttons def handle_button_click(update, context): query = update.callback_query emoji = query.data.split("_")[-1] query.answer(f"You clicked the {emoji} button!") def main(): # Replace with your bot token from @BotFather updater = Updater("YOUR_BOT_TOKEN") dp = updater.dispatcher dp.add_handler(MessageHandler(Filters.text & ~Filters.command, handle_message)) dp.add_handler(CallbackQueryHandler(handle_button_click)) updater.start_polling() updater.idle() if __name__ == "__main__": main()
Quick Notes:
- Install the library first:
pip install python-telegram-bot(usepython-telegram-bot==13.7for the stable v13 branch, or the latest version for v20+) - The
callback_datain each button lets you track which emoji was clicked—use thehandle_button_clickfunction to add custom responses. - If you prefer reply buttons (that replace the user's keyboard) instead of inline ones, swap
InlineKeyboardButtonwithKeyboardButtonandInlineKeyboardMarkupwithReplyKeyboardMarkup.
内容的提问来源于stack exchange,提问作者Andrey Radkevich




