Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use custom emoji in aiogram message formatting #1466

Open
2 tasks done
Ardanila opened this issue Apr 20, 2024 · 1 comment
Open
2 tasks done

How to use custom emoji in aiogram message formatting #1466

Ardanila opened this issue Apr 20, 2024 · 1 comment
Labels
bug Something is wrong with the framework

Comments

@Ardanila
Copy link

Checklist

  • I am sure the error is coming from aiogram code
  • I have searched in the issue tracker for similar bug reports, including closed ones

Operating system

Mac OS Sonoma

Python version

Python 3.8.19

aiogram version

3.4.1

Expected behavior

I expect the bot to send custom (premium) emoji when a custom emoji is provided by user.

Current behavior

When a custom emoji is provided, the bot sends a basic emoji (e.g., 😎) instead of the corresponding custom (premium) emoji.

Steps to reproduce

  1. User sends a message containing a custom emoji.
  2. The bot extracts the custom emoji ID from the message.
  3. The bot attempts to retrieve the custom emoji using the get_custom_emoji_stickers method provided by Aiogram.
  4. The bot sends a message with the retrieved custom emoji sticker.

Code example

...

class UserState(StatesGroup):
waiting_for_emoji = State()

...

@dp.message(CommandStart())
async def cmd_start(message: Message, state: FSMContext):

    await message.answer("Welcome! Please send the custom emoji you want to use.")
    await state.set_state(UserState.waiting_for_emoji)

...

@dp.message(Command('set_emoji'))
async def cmd_set_emoji(message: Message, state: FSMContext):
    await message.answer("Please send the custom emoji you want to use.")
    await state.set_state(UserState.waiting_for_emoji)

...

@dp.message(UserState.waiting_for_emoji)
async def process_emoji(message: Message, state: FSMContext):
    if message.text and message.text.startswith('/'):
        await message.answer("Invalid input. Please send a custom emoji.")
        return

    emoji = message.text
    custom_emoji_id = None

    if message.entities:
        for entity in message.entities:
            if entity.type == 'custom_emoji':
                custom_emoji_id = entity.custom_emoji_id
                break

    if custom_emoji_id:
        await state.update_data(custom_emoji_id=custom_emoji_id)
        await message.answer(f"Custom emoji {emoji} set successfully!")

...

async def process_text_common(text, message, state):

state_data = await state.get_data()
custom_emoji_id = state_data.get('custom_emoji_id')

await message.answer(f"{custom_emoji_id}", parse_mode="HTML", reply_markup=keyboard)

Logs

No response

Additional information

No response

@Ardanila Ardanila added the bug Something is wrong with the framework label Apr 20, 2024
@Oven29
Copy link

Oven29 commented Apr 25, 2024

Hello, in order for the bot to send custom emojis, it needs to buy a username. e. g https://t.me/CryptoBot has a username (@ send, @ calc), so he can send custom emoji. Also to send custom emoji must use entities

f'...<tg-emoji emoji-id="{custom_emoji_id}">{emoji}</tg-emoji>...'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something is wrong with the framework
Projects
None yet
Development

No branches or pull requests

2 participants