Bot decorators will pass only supported arguments to decorated functions

Command, parser, button and query decorators will not pass unsupported 
arguments to their decorated functions anymore (legacy compatibility 
with custombot and new feature for bot).
This commit is contained in:
Davte 2019-07-15 13:06:02 +02:00
parent e412548edf
commit a8a797a176

View File

@ -31,6 +31,7 @@ Usage
import asyncio import asyncio
from collections import OrderedDict from collections import OrderedDict
import io import io
import inspect
import logging import logging
import os import os
import re import re
@ -1310,8 +1311,16 @@ class Bot(TelegramBot, ObjectWithDatabase):
user_record=user_record, user_record=user_record,
authorization_level=authorization_level authorization_level=authorization_level
): ):
return await command_handler(bot=bot, update=update, # Pass supported arguments from locals() to command_handler
user_record=user_record) return await command_handler(
**{
name: argument
for name, argument in locals().items()
if name in inspect.signature(
command_handler
).parameters
}
)
return self.unauthorized_message return self.unauthorized_message
self.commands[command] = dict( self.commands[command] = dict(
handler=decorated_command_handler, handler=decorated_command_handler,
@ -1370,7 +1379,14 @@ class Bot(TelegramBot, ObjectWithDatabase):
user_record=user_record, user_record=user_record,
authorization_level=authorization_level authorization_level=authorization_level
): ):
return await parser(bot, message, user_record) # Pass supported arguments from locals() to parser
return await parser(
**{
name: argument
for name, argument in locals().items()
if name in inspect.signature(parser).parameters
}
)
return bot.unauthorized_message return bot.unauthorized_message
self.text_message_parsers[condition] = dict( self.text_message_parsers[condition] = dict(
handler=decorated_parser, handler=decorated_parser,
@ -1442,8 +1458,7 @@ class Bot(TelegramBot, ObjectWithDatabase):
user_record=user_record, user_record=user_record,
authorization_level=authorization_level authorization_level=authorization_level
): ):
return await handler(bot, update, user_record) # Remove `prefix` from `data`
# Remove `prefix` from `ðata`
data = extract(update['data'], prefix) data = extract(update['data'], prefix)
# If a specific separator or default separator is set, # If a specific separator or default separator is set,
# use it to split `data` string in a list. # use it to split `data` string in a list.
@ -1455,6 +1470,14 @@ class Bot(TelegramBot, ObjectWithDatabase):
else element else element
for element in data.split(_separator) for element in data.split(_separator)
] ]
# Pass supported arguments from locals() to handler
return await handler(
**{
name: argument
for name, argument in locals().items()
if name in inspect.signature(handler).parameters
}
)
return bot.unauthorized_message return bot.unauthorized_message
self.callback_handlers[prefix] = dict( self.callback_handlers[prefix] = dict(
handler=decorated_button_handler, handler=decorated_button_handler,
@ -1493,8 +1516,14 @@ class Bot(TelegramBot, ObjectWithDatabase):
user_record=user_record, user_record=user_record,
authorization_level=authorization_level authorization_level=authorization_level
): ):
return await handler(bot=self, update=update, # Pass supported arguments from locals() to handler
user_record=user_record) return await handler(
**{
name: argument
for name, argument in locals().items()
if name in inspect.signature(handler).parameters
}
)
return self.unauthorized_message return self.unauthorized_message
self.inline_query_handlers[condition] = dict( self.inline_query_handlers[condition] = dict(
handler=decorated_query_handler, handler=decorated_query_handler,