这就是我的意思。注意,我假设
bot.polling
启动后台活动,并且您不必一直在循环中调用它。如果你必须自己做主循环,那么它需要有所不同。
polling = False
def start_polling():
global polling
if not polling:
bot.polling(none_stop=True)
polling = True
def stop_polling():
global polling
if polling:
bot.stop_polling()
polling = False
print(f"\n\n{get_active_session()}")
while True:
if get_active_session() == ID:
stop_polling()
else:
start_polling()
time.sleep(1)
或者,使用类跳过全局:
class Poller:
def __init__(self, bot):
self.polling = False
self.bot = bot
def start_polling(self):
if not self.polling:
self.bot.polling(none_stop=True)
self.polling = True
def stop_polling(self:):
if self.polling:
self.bot.stop_polling()
self.polling = False
def mainloop(self):
while True:
if get_active_session() == ID:
self.stop_polling()
else:
self.start_polling()
time.sleep(1)
poll = Poller(bot)
poll.mainloop()