I’m trying to apply decorator from another class on method in my class…
it is my implementation of this Telegram API wrapper library:
https://github.com/eternnoir/pyTelegramBotAPI
But in my example want to use it not from script – but as method of class like that:
class Bot:
def __init__(self, key):
self.key = key
self.bot=telebot.TeleBot(key)
def start(self):
self.bot.polling()
# Handle '/start' and '/help'
@self.bot.message_handler(commands=['help', 'start'])
def send_welcome(self,message):
self.bot.reply_to(message, """
Hi there, I am EchoBot.
I am here to echo your kind words back to you.
Just say anything nice and I'll say the exact same thing to you!
""")
# Handle all other messages with content_type 'text' (content_types defaults to ['text'])
@self.bot.message_handler(func=lambda message: True)
def echo_message(message):
self.bot.reply_to(message, message.text)
All self
are highlighted … and certainly not working – will be glad if someone can explain what im doing wrong?
The original example trying to customize is:
import telebot
bot = telebot.TeleBot("TOKEN")
@bot.message_handler(commands=['start', 'help'])
def send_welcome(message):
bot.reply_to(message, "Howdy, how are you doing?")
@bot.message_handler(func=lambda message: True)
def echo_all(message):
bot.reply_to(message, message.text)
bot.polling()
5
Answers
for those who might need it - solution i found - was to put the functions inside Ctor - and not to apply decorators to class methods .... :
self
though a keyword is more of a placeholder variable name for the 1st argument for all methods that is always the instance object (except forclassmethod
where 1st argument is the class itself andstaticmethod
where there is neither)For any method, even if you use
this
instead ifself
as 1st argument of any method, you can access all the object attributes asthis.foo
or methods asthis.bar()
.So technically, you don’t have access to the object outside any method. The decorator is at the outer most level where you cannot access object (which is passed only to methods as the 1st argument)
A complex and unnecessary workaround I can think of is to write a static method that will help you catch the
self
object from argument and then access it’smessage_handler
I think that proper method is to use lambda functions. You can pass more than one parameter to it and get self from outer scope.
So, i finished with next sample
You can run it from console and it will print START command every time you send /start to it.
You should set object of TeleBot() in method start().