|
| 1 | +import functools |
| 2 | +from typing import List |
| 3 | +from typing import Union as u |
| 4 | +import BugReporter |
| 5 | +import logging |
| 6 | +from telegram.ext import (BaseFilter, CallbackContext, CommandHandler, |
| 7 | + ConversationHandler, Dispatcher, Filters, Handler, |
| 8 | + MessageHandler) |
| 9 | +from telegram import Update |
| 10 | + |
| 11 | + |
| 12 | +def auth(auth_users_id:u[List[u[str,int]],str,int], error:u[str,callable]): |
| 13 | + def decorator_auth(func): |
| 14 | + @functools.wraps(func) |
| 15 | + def wrapper(u:Update, c:CallbackContext): |
| 16 | + run = False |
| 17 | + if isinstance(auth_users_id,list): |
| 18 | + user_id = type(auth_users_id[0])(u.effective_user.id) |
| 19 | + run = user_id in auth_users_id |
| 20 | + else: |
| 21 | + user_id = type(auth_users_id)(u.effective_user.id) |
| 22 | + run = user_id == auth_users_id |
| 23 | + |
| 24 | + if run: |
| 25 | + return func(u,c) |
| 26 | + else: |
| 27 | + if callable(error): |
| 28 | + return error(u,c) |
| 29 | + else: |
| 30 | + u.effective_chat.send_message(error) |
| 31 | + |
| 32 | + return wrapper |
| 33 | + return decorator_auth |
| 34 | + |
| 35 | +def HandlerDecorator (handlerClass,**kwargs): |
| 36 | + def decorator_handler(func): |
| 37 | + return handlerClass(callback = func, **kwargs) |
| 38 | + return decorator_handler |
| 39 | + |
| 40 | +def MessageHandlerDecorator(self,filters = Filters.all, group=1, **kwargs): |
| 41 | + def decorator_message(func): |
| 42 | + return MessageHandler(filters, func, kwargs) |
| 43 | + return decorator_message |
| 44 | + |
| 45 | +def CommandHandlerDecorator(_func=None, command=None, *args, **kwargs): |
| 46 | + def decorator_command(func): |
| 47 | + if not isinstance(command,str): |
| 48 | + command = func.__name__ |
| 49 | + return CommandHandler(command,func,*args,**kwargs) |
| 50 | + |
| 51 | + if _func: |
| 52 | + return decorator_command(_func) |
| 53 | + else: |
| 54 | + return decorator_command |
| 55 | + |
| 56 | +class DispatcherDecorators: |
| 57 | + def __init__(self, dispatcher:Dispatcher): |
| 58 | + self.dispatcher = dispatcher |
| 59 | + |
| 60 | + def commandHandler(self, _func=None, command=None, group=1, *args, **kwargs): |
| 61 | + def decorator_command(func): |
| 62 | + if not isinstance(command,str): |
| 63 | + command = func.__name__ |
| 64 | + logging.debug(f'add command handler. command:{command} => {func}') |
| 65 | + try: |
| 66 | + self.dispatcher.add_handler(CommandHandler(command,func,*args,**kwargs), group) |
| 67 | + except: |
| 68 | + logging.exception('exception while trying to add a command') |
| 69 | + BugReporter.exception('exception while trying to add a command') |
| 70 | + |
| 71 | + |
| 72 | + return func |
| 73 | + |
| 74 | + if _func: |
| 75 | + return decorator_command(_func) |
| 76 | + else: |
| 77 | + return decorator_command |
| 78 | + |
| 79 | + def messageHandler(self, _func=None, filters=Filters.all, group=1, *args, **kwargs): |
| 80 | + def decorator_message(func): |
| 81 | + logging.debug(f'add message handler. handler: {func}') |
| 82 | + try: |
| 83 | + self.dispatcher.add_handler(MessageHandler(filters, func,*args, kwargs), group) |
| 84 | + except: |
| 85 | + logging.exception('exception while trying to add a command') |
| 86 | + BugReporter.exception('exception while trying to add a command') |
| 87 | + |
| 88 | + return func |
| 89 | + |
| 90 | + if _func: |
| 91 | + return decorator_message(_func) |
| 92 | + else: |
| 93 | + return decorator_message |
| 94 | + |
| 95 | + def addHandler(self, handler_:Handler = None, group=1): |
| 96 | + def decorator_handler(handler:Handler): |
| 97 | + logging.debug(f'add {type(handler).__name__}. handler: {handler.callback}') |
| 98 | + try: |
| 99 | + self.dispatcher.add_handler(handler, group) |
| 100 | + except: |
| 101 | + logging.exception('exception while trying to add a command') |
| 102 | + BugReporter.exception('exception while trying to add a command') |
| 103 | + return handler |
| 104 | + if handler_: |
| 105 | + return decorator_handler(handler_) |
| 106 | + else: |
| 107 | + return decorator_handler |
| 108 | + |
| 109 | +class ConversationDecorator: |
| 110 | + def __init__(self, entry_points:List[Handler], **kwargs): |
| 111 | + self.entry_points = entry_points |
| 112 | + self.states = dict() |
| 113 | + self.fallbacks = [] |
| 114 | + self.__kwargs = kwargs |
| 115 | + |
| 116 | + def state(self, *states): |
| 117 | + def decorator_state(handler:Handler): |
| 118 | + for state in states: |
| 119 | + if not state in self.states: |
| 120 | + self.states[state] = [] |
| 121 | + self.states[state].append(handler) |
| 122 | + return handler |
| 123 | + return decorator_state |
| 124 | + |
| 125 | + def fallback(self, handler:Handler): |
| 126 | + self.fallbacks.append(handler) |
| 127 | + return handler |
| 128 | + |
| 129 | + def get_handler(self): |
| 130 | + return ConversationHandler(self.entry_points, self.state, self.fallbacks, **self.__kwargs) |
0 commit comments