-
Notifications
You must be signed in to change notification settings - Fork 0
Emoji
You've now made a cool bot, but it's lacking personality? Add some emoji!
The easiest way to use emoji is to directly put them in your strings. The Unicode website has a chart with all the emoji. Simply select any emoji you want, this works with both the images and the raw characters in the "Brow." column, and paste it in your string.
text = "🌈⛈🎉🌹🐧😊"
In the code you may see squares with numbers in them instead of the emoji themself. This means the font in your text editor does not have an image for that character, but it is still there.
This will work without problems on Python 3. On Python 2 you need to declare the encoding of your source file, put this line at the top:
# -*- coding: utf-8 -*-
this tells Python that your source file is encoded in UTF8. Note that if you have a shebang at the top, the encoding line comes second:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
Finally, test your emoji by sending it to yourself over Telegram. Know that Telegram does not support all the emoji.
python-telegram-bot has an internal emoji module that you can use to handle Telegram-support emojis without having to copy/paste the Unicode representations directly:
> from telegram import Emoji
> Emoji.AIRPLANE
'✈'
You can also use the more general-purpose emoji module, which will emojize entire blocks of text instead of having to explicitly call each emoticon as with telegram.Emoji:
from emoji import emojize
bot.sendMessage(emojize("yummy :cake:", use_aliases=True))
Note: the emojize
function uses regular expressions and takes on the order of microseconds to complete. If your bot handles billions of messages per second, put the emoji in reusable variables to micro-optimize:
cake = emojize(":cake:", use_aliases=True)
- Wiki of
python-telegram-bot
© Copyright 2015-2025 – Licensed by Creative Commons
- Architecture Overview
- Builder Pattern for
Application
- Types of Handlers
- Working with Files and Media
- Exceptions, Warnings and Logging
- Concurrency in PTB
- Advanced Filters
- Storing data
- Making your bot persistent
- Adding Defaults
- Job Queue
- Arbitrary
callback_data
- Avoiding flood limits
- Webhooks
- Bot API Forward Compatiblity
- Frequently requested design patterns
- Code snippets
- Performance Optimizations
- Telegram Passport
- Bots built with PTB
- Automated Bot Tests