From 6e8abc709e592b2b8dcaccd579726bf2cb63767c Mon Sep 17 00:00:00 2001
From: _run <khumogo1@gmail.com>
Date: Tue, 28 Jun 2022 19:51:51 +0500
Subject: [PATCH] Pass only the necessary data

---
 telebot/__init__.py      | 15 ++++++++++-----
 telebot/async_telebot.py | 20 ++++++++++++--------
 2 files changed, 22 insertions(+), 13 deletions(-)

diff --git a/telebot/__init__.py b/telebot/__init__.py
index 23887c1..f034cca 100644
--- a/telebot/__init__.py
+++ b/telebot/__init__.py
@@ -4022,12 +4022,19 @@ class TeleBot:
                                     return
                             
                             else:
-                                if handler.get('pass_bot'): data["bot"] = self
-                                if len(data) > len(params) - 1: # remove the message parameter
+
+                                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)
+                                handler["function"](message, **data_copy)
 
         except Exception as e:
             handler_error = e
@@ -4038,8 +4045,6 @@ class TeleBot:
                 logging.error(str(e))
                 return
         # remove the bot from data
-        if "bot" in data:
-            del data["bot"]
         if middlewares:
             for middleware in middlewares:
                 middleware.post_process(message, data, handler_error)
diff --git a/telebot/async_telebot.py b/telebot/async_telebot.py
index f41f5cd..5c7f50c 100644
--- a/telebot/async_telebot.py
+++ b/telebot/async_telebot.py
@@ -309,13 +309,20 @@ class AsyncTeleBot:
                                 logger.error("It is not allowed to pass data and values inside data to the handler. Check your handler: {}".format(handler['function']))
                                 return
                         
-                        else:
-                        
-                            if handler.get('pass_bot'): data["bot"] = self
-                            if len(data) > len(params) - 1:
+                        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)
+                            
+                            handler["function"](message, **data_copy)
                 except Exception as e:
                     handler_error = e
 
@@ -324,9 +331,6 @@ class AsyncTeleBot:
                             return self.exception_handler.handle(e)
                         logging.error(str(e))
                         return
-        # remove the bot from data
-        if "bot" in data:
-            del data["bot"]
 
         if middlewares:
             for middleware in middlewares: