Skip to content

Instantly share code, notes, and snippets.

@Kylmakalle
Created January 19, 2018 23:07
Show Gist options
  • Save Kylmakalle/2c8a6284d1f2d02f529ea69afbc2fa0c to your computer and use it in GitHub Desktop.
Save Kylmakalle/2c8a6284d1f2d02f529ea69afbc2fa0c to your computer and use it in GitHub Desktop.
from aiogram import Bot, types
from aiogram.dispatcher import Dispatcher, filters
from aiogram.utils import executor
import re
class RegexpCommandsFilter(filters.AsyncFilter):
"""
Check commands by regexp in message
"""
def __init__(self, regexp_commands):
self.regexp_commands = [re.compile(command, flags=re.IGNORECASE | re.MULTILINE) for command in regexp_commands]
async def check(self, message):
if not message.is_command():
return False
command = message.text.split()[0][1:]
command, _, mention = command.partition('@')
if mention and mention != (await message.bot.me).username:
return False
for command in self.regexp_commands:
search = command.search(message.text)
if search:
message.conf['regexp_command'] = search
return True
return False
@dp.message_handler(RegexpCommandsFilter(regexp_commands=['item_([0-9]*)']))
async def send_welcome(message: types.Message):
regexp_command = message.conf['regexp_command']
await message.reply("You have requested an item with number: {}".format(regexp_command.group(1)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment