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