There is a piece of code:

@bot.message_handler(func=lambda message: True, content_types=['sticker']) def handle_sticker(message): bot.delete_message(message.chat.id, message.message_id) bot.send_message(message.chat.id, text=("@{!s}, " + strings_data.get(get_language(message.from_user.language_code)).get("withoutstickers")).format(bot.get_chat_member(message.chat.id, message.from_user.id).user.username)) 

explanation - when receiving a sticker, the bot deletes it and sends a message like '@username, without stickers.' but there are users without a username and then it is accessed '@None'.

it is necessary to check for the presence of username, and in its absence - to return first_name. I know how to get username and first_name bot.get_chat_member(message.chat.id, message.from_user.id).user.username (or user.first_name )

but I don’t know how to attach a check. can you help?

    1 answer 1

    after a couple of hours came to this decision. Maybe not the best, but it works as I need.

    create a function (correct if it's something else) that checks if there is a username

     # получение username, если пусто - возвращает first_name def get_username(message): usr = bot.get_chat_member(message.chat.id, message.from_user.id) if not usr.user.username: return usr.user.first_name else: return usr.user.username 

    further we modify the handler, deleting messages with stickers

      # удаление стикеров @bot.message_handler(func=lambda message: True, content_types=['sticker']) def handle_sticker(message): # sticker_id = bot.get_file(message.sticker.file_id) bot.delete_message(message.chat.id, message.message_id) stickertext = (strings_data.get(get_language(message.from_user.language_code)).get("withoutstickers")) if get_username(message) == bot.get_chat_member(message.chat.id, message.from_user.id).user.username: bot.send_message(message.chat.id, text=("@{!s}, " + stickertext).format(get_username(message))) else: bot.send_message(message.chat.id, text=("{!s}, " + stickertext).format(get_username(message))) 

    you see that there appeared an if-else , the values ​​in which differ by one symbol. This was done so that when mentioning on first_name there was no @ sign at the beginning, since the left person could be mentioned at all, and there was still no mention on first_name , this is plain text. although you can try to do it by user.id , but if someone needs it, he will finish it.