-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinternal_wrapper.py
More file actions
39 lines (30 loc) · 1.1 KB
/
internal_wrapper.py
File metadata and controls
39 lines (30 loc) · 1.1 KB
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
import logging
from custom_exceptions import BotNotAllowed
from telegram.ext import CommandHandler
# formating the log
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
logger = logging.getLogger(__name__)
def internal_command_handler(command_name, func):
return CommandHandler(command_name, lambda update, context: wrap(func, update, context))
# every REGISTERED action pass through this wrap, that way we can filter and log actions.
def wrap(action, update=None, context=None):
if (update == None or context == None):
return
# log action and filter
log(action, update, context)
filter(update)
# invoke action
action(update, context)
# put filters here.
def filter(update):
user_filter(update.message.from_user)
def user_filter(user):
if (user.is_bot):
raise BotNotAllowed("Bot are not allowed to send messages.")
def log(action, update, context):
logger.info({
'full_name': update.message.from_user,
'action': action.__name__,
'args': context.args
})