1
0
mirror of https://github.com/eternnoir/pyTelegramBotAPI.git synced 2023-08-10 21:12:57 +03:00

Compare commits

...

12 Commits

Author SHA1 Message Date
Badiboy
b9b4885568 Merge pull request #1597 from Badiboy/master
Copyright update
2022-06-29 19:25:40 +03:00
Badiboy
ce0a974c91 Copyright update 2022-06-29 19:24:27 +03:00
Badiboy
6ff015689a Merge pull request #1595 from coder2020official/master
Add the possibility to getbase class of a State object
2022-06-29 18:34:06 +03:00
_run
6459aded82 Merge branch 'eternnoir:master' into master 2022-06-29 20:22:59 +05:00
_run
fec47cecaf Add the possibility to getbase class of a State object 2022-06-29 20:21:42 +05:00
Badiboy
3892b0fb80 Merge pull request #1593 from coder2020official/master
Fixed previous fix 🤦‍♂️
2022-06-29 12:58:43 +03:00
_run
fbb9a73fc0 F###, forgot to put await 2022-06-29 14:52:37 +05:00
Badiboy
db5c53b8e5 Merge pull request #1591 from coder2020official/master
Pass only the necessary data
2022-06-28 17:58:35 +03:00
_run
6e8abc709e Pass only the necessary data 2022-06-28 19:51:51 +05:00
Badiboy
752f35614c Merge pull request #1587 from coder2020official/master
Middleware update: everything in data will be passed to handler if ne…
2022-06-26 12:09:38 +03:00
_run
a2893945b2 Async changes and sync improvements 2022-06-25 22:15:53 +05:00
_run
1686ce4f44 Middleware update: everything in data will be passed to handler if needed. 2022-06-25 21:48:44 +05:00
5 changed files with 89 additions and 40 deletions

View File

@@ -9,6 +9,7 @@ import time
import traceback
from typing import Any, Callable, List, Optional, Union
# these imports are used to avoid circular import error
import telebot.util
import telebot.types
@@ -385,7 +386,7 @@ class TeleBot:
new_poll_answers = None
new_my_chat_members = None
new_chat_members = None
chat_join_request = None
new_chat_join_request = None
for update in updates:
if apihelper.ENABLE_MIDDLEWARE:
@@ -442,8 +443,8 @@ class TeleBot:
if new_chat_members is None: new_chat_members = []
new_chat_members.append(update.chat_member)
if update.chat_join_request:
if chat_join_request is None: chat_join_request = []
chat_join_request.append(update.chat_join_request)
if new_chat_join_request is None: new_chat_join_request = []
new_chat_join_request.append(update.chat_join_request)
if new_messages:
self.process_new_messages(new_messages)
@@ -471,8 +472,8 @@ class TeleBot:
self.process_new_my_chat_member(new_my_chat_members)
if new_chat_members:
self.process_new_chat_member(new_chat_members)
if chat_join_request:
self.process_new_chat_join_request(chat_join_request)
if new_chat_join_request:
self.process_new_chat_join_request(new_chat_join_request)
def process_new_messages(self, new_messages):
self._notify_next_handlers(new_messages)
@@ -3977,7 +3978,7 @@ class TeleBot:
middlewares = [i for i in self.middlewares if update_type in i.update_types]
return middlewares
def _run_middlewares_and_handler(self, message, handlers, middlewares, *args, **kwargs):
def _run_middlewares_and_handler(self, message, handlers, middlewares):
"""
This class is made to run handler and middleware in queue.
@@ -3998,6 +3999,7 @@ class TeleBot:
return
elif isinstance(result, SkipHandler) and skip_handler is False:
skip_handler = True
try:
if handlers and not skip_handler:
@@ -4009,24 +4011,31 @@ class TeleBot:
params.append(i)
if len(params) == 1:
handler['function'](message)
elif len(params) == 2:
if handler.get('pass_bot') is True:
handler['function'](message, self)
elif handler.get('pass_bot') is False:
handler['function'](message, data)
elif len(params) == 3:
if params[2] == 'bot' and handler.get('pass_bot') is True:
handler['function'](message, data, self)
else:
if "data" in params:
if len(params) == 2:
handler['function'](message, data)
elif len(params) == 3:
handler['function'](message, data=data, bot=self)
else:
logger.error("It is not allowed to pass data and values inside data to the handler. Check your handler: {}".format(handler['function']))
return
elif not handler.get('pass_bot'):
raise RuntimeError('Your handler accepts 3 parameters but pass_bot is False. Please re-check your handler.')
else:
handler['function'](message, self, data)
data_copy = data.copy()
for key in list(data_copy):
# remove data from data_copy if handler does not accept it
if key not in params:
del data_copy[key]
if handler.get('pass_bot'): data_copy["bot"] = self
if len(data_copy) > len(params) - 1: # remove the message parameter
logger.error("You are passing more data than the handler needs. Check your handler: {}".format(handler['function']))
return
handler["function"](message, **data_copy)
except Exception as e:
handler_error = e
@@ -4035,6 +4044,7 @@ class TeleBot:
return self.exception_handler.handle(e)
logging.error(str(e))
return
# remove the bot from data
if middlewares:
for middleware in middlewares:
middleware.post_process(message, data, handler_error)

View File

@@ -277,7 +277,7 @@ class AsyncTeleBot:
handler_error = None
data = {}
process_handler = True
params = []
if middlewares:
for middleware in middlewares:
middleware_result = await middleware.pre_process(message, data)
@@ -295,27 +295,34 @@ class AsyncTeleBot:
continue
elif process_update:
try:
params = []
for i in signature(handler['function']).parameters:
params.append(i)
if len(params) == 1:
await handler['function'](message)
break
elif len(params) == 2:
if handler['pass_bot']:
await handler['function'](message, self)
break
else:
await handler['function'](message, data)
break
elif len(params) == 3:
if handler['pass_bot'] and params[1] == 'bot':
await handler['function'](message, self, data)
break
else:
await handler['function'](message, data)
break
else:
if "data" in params:
if len(params) == 2:
await handler['function'](message, data)
elif len(params) == 3:
await handler['function'](message, data=data, bot=self)
else:
logger.error("It is not allowed to pass data and values inside data to the handler. Check your handler: {}".format(handler['function']))
return
else:
data_copy = data.copy()
for key in list(data_copy):
# remove data from data_copy if handler does not accept it
if key not in params:
del data_copy[key]
if handler.get('pass_bot'): data_copy["bot"] = self
if len(data_copy) > len(params) - 1: # remove the message parameter
logger.error("You are passing more data than the handler needs. Check your handler: {}".format(handler['function']))
return
await handler["function"](message, **data_copy)
except Exception as e:
handler_error = e

View File

@@ -29,6 +29,7 @@ class StatesGroup:
if not name.startswith('__') and not callable(value) and isinstance(value, State):
# change value of that variable
value.name = ':'.join((cls.__name__, name))
value.group = cls
class SkipHandler:

View File

@@ -1,3 +1,30 @@
"""
Copyright (c) 2017-2018 Alex Root Junior
Permission is hereby granted, free of charge, to any person obtaining a copy of this
software and associated documentation files (the "Software"), to deal in the Software
without restriction, including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so, subject to the
following conditions:
The above copyright notice and this permission notice shall be included in all copies
or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
OR OTHER DEALINGS IN THE SOFTWARE.
This file was added during the pull request. The maintainers overlooked that it was copied
"as is" from another project and they do not consider it as a right way to develop a project.
However, due to backward compatibility we had to leave this file in the project with the above
copyright added, as it is required by the original project license.
"""
import typing

View File

@@ -163,6 +163,10 @@ class StatesGroup:
if not name.startswith('__') and not callable(value) and isinstance(value, State):
# change value of that variable
value.name = ':'.join((cls.__name__, name))
value.group = cls
class BaseMiddleware: