username = '******' await bot.send_message(message.chat.id, username) # You can make it case insensitive @bot.message_handler(text=TextFilter(equals=l_("My first name"), ignore_case=True)) async def return_user_id(message: types.Message): await bot.send_message(message.chat.id, message.from_user.first_name) all_menu_texts = [] for language in i18n.available_translations: for menu_text in ("My user id", "My user name", "My first name"): all_menu_texts.append(_(menu_text, language)) # When user confused language. (handles all menu buttons texts) @bot.message_handler(text=TextFilter(contains=all_menu_texts, ignore_case=True)) async def missed_message(message: types.Message): await bot.send_message(message.chat.id, _("Seems you confused language"), reply_markup=keyboards.menu_keyboard(_)) if __name__ == '__main__': bot.setup_middleware(i18n) bot.add_custom_filter(TextMatchFilter()) asyncio.run(bot.infinity_polling())
from telebot.async_telebot import AsyncTeleBot from telebot import asyncio_filters bot = AsyncTeleBot('TOKEN') # Handler @bot.message_handler(chat_types=['supergroup'], is_chat_admin=True) async def answer_for_admin(message): await bot.send_message(message.chat.id, "hello my admin") # Register filter bot.add_custom_filter(asyncio_filters.IsAdminFilter(bot)) import asyncio asyncio.run(bot.polling())
from telebot.async_telebot import AsyncTeleBot import telebot bot = AsyncTeleBot('TOKEN') # Chat id can be private or supergroups. @bot.message_handler(chat_id=[12345678], commands=['admin'] ) # chat_id checks id corresponds to your list or not. async def admin_rep(message): await bot.send_message(message.chat.id, "You are allowed to use this command.") @bot.message_handler(commands=['admin']) async def not_admin(message): await bot.send_message(message.chat.id, "You are not allowed to use this command") # Do not forget to register bot.add_custom_filter(telebot.asyncio_filters.ChatFilter()) import asyncio asyncio.run(bot.polling())
from telebot.async_telebot import AsyncTeleBot import telebot bot = AsyncTeleBot('TOKEN') # Check if message is a reply @bot.message_handler(is_reply=True) async def start_filter(message): await bot.send_message(message.chat.id, "Looks like you replied to my message.") # Check if message was forwarded @bot.message_handler(is_forwarded=True) async def text_filter(message): await bot.send_message(message.chat.id, "I do not accept forwarded messages!") # Do not forget to register filters bot.add_custom_filter(telebot.asyncio_filters.IsReplyFilter()) bot.add_custom_filter(telebot.asyncio_filters.ForwardFilter()) import asyncio asyncio.run(bot.polling())
message.chat.id) as data: await bot.send_message( message.chat.id, "Ready, take a look:\n<b>Name: {name}\nSurname: {surname}\nAge: {age}</b>" .format(name=data['name'], surname=data['surname'], age=message.text), parse_mode="html") await bot.delete_state(message.from_user.id, message.chat.id) #incorrect number @bot.message_handler(state=MyStates.age, is_digit=False) async def age_incorrect(message): """ Will process for wrong input when state is MyState.age """ await bot.send_message( message.chat.id, 'Looks like you are submitting a string in the field age. Please enter a number' ) # register filters bot.add_custom_filter(asyncio_filters.StateFilter(bot)) bot.add_custom_filter(asyncio_filters.IsDigitFilter()) import asyncio asyncio.run(bot.polling())
@bot.message_handler(is_admin=True, commands=['admin']) # Check if user is admin async def admin_rep(message): await bot.send_message(message.chat.id, "Hi admin") @bot.message_handler(is_admin=False, commands=['admin']) # If user is not admin async def not_admin(message): await bot.send_message(message.chat.id, "You are not admin") @bot.message_handler(text=['hi']) # Response to hi message async def welcome_hi(message): await bot.send_message(message.chat.id, 'You said hi') @bot.message_handler(text=['bye']) # Response to bye message async def bye_user(message): await bot.send_message(message.chat.id, 'You said bye') # Do not forget to register filters bot.add_custom_filter(MainFilter()) bot.add_custom_filter(IsAdmin()) import asyncio asyncio.run(bot.polling())
from telebot.async_telebot import AsyncTeleBot import telebot bot = AsyncTeleBot('TOKEN') # Check if message starts with @admin tag @bot.message_handler(text_startswith="@admin") async def start_filter(message): await bot.send_message(message.chat.id, "Looks like you are calling admin, wait...") # Check if text is hi or hello @bot.message_handler(text=['hi', 'hello']) async def text_filter(message): await bot.send_message( message.chat.id, "Hi, {name}!".format(name=message.from_user.first_name)) # Do not forget to register filters bot.add_custom_filter(telebot.asyncio_filters.TextMatchFilter()) bot.add_custom_filter(telebot.asyncio_filters.TextStartsFilter()) import asyncio asyncio.run(bot.polling())