如何将以下代码包装在一个类中:
import telebot
bot = telebot.TeleBot('...')
if __name__ == '__main__':
#Tele = Telegram()
#Tele.run()
@bot.message_handler(commands=['start'])
def start_command(message):
bot.send_message(message.chat.id, "Hello!")
bot.polling()
最终结果应该是这样的:
import telebot
class Telegram():
def __init__(self):
self.token = '...'
self.bot = telebot.TeleBot('...')
@self.bot.message_handler(commands=['start'])
def start_command(self, message):
self.bot.send_message(message.chat.id, "Hello!")
def run(self):
self.bot.polling()
if __name__ == '__main__':
Tele = Telegram()
Tele.run()
尝试: