mirror of
https://github.com/eternnoir/pyTelegramBotAPI.git
synced 2023-08-10 21:12:57 +03:00
Compare commits
12 Commits
4.6.0
...
b9b4885568
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b9b4885568 | ||
|
|
ce0a974c91 | ||
|
|
6ff015689a | ||
|
|
6459aded82 | ||
|
|
fec47cecaf | ||
|
|
3892b0fb80 | ||
|
|
fbb9a73fc0 | ||
|
|
db5c53b8e5 | ||
|
|
6e8abc709e | ||
|
|
752f35614c | ||
|
|
a2893945b2 | ||
|
|
1686ce4f44 |
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
|
||||
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user