[telegram] Fix telegram api key update bug

This commit is contained in:
Cathy Hu 2020-07-17 23:37:06 +02:00
parent f6eef5dc73
commit 98f60d498b

View file

@ -18,13 +18,7 @@ class TelegramBot(Censor):
def __init__(self, telegram_model): def __init__(self, telegram_model):
super().__init__(telegram_model.hood) super().__init__(telegram_model.hood)
self.telegram_model = telegram_model self.telegram_model = telegram_model
try: self.enabled = self.telegram_model.enabled
self.bot = Bot(token=telegram_model.api_token)
self.dp = self._create_dispatcher()
except exceptions.ValidationError:
self.telegram_model.enabled = False
finally:
self.enabled = self.telegram_model.enabled
def _create_dispatcher(self): def _create_dispatcher(self):
dp = Dispatcher(self.bot) dp = Dispatcher(self.bot)
@ -35,8 +29,8 @@ class TelegramBot(Censor):
async def run(self): async def run(self):
try: try:
if not self.dp: self.bot = Bot(token=self.telegram_model.api_token)
self.dp = self._create_dispatcher() self.dp = self._create_dispatcher()
logger.debug(f'Bot {self.telegram_model.hood.name} starting.') logger.debug(f'Bot {self.telegram_model.hood.name} starting.')
await gather(self.dp.start_polling(), self._push()) await gather(self.dp.start_polling(), self._push())
except CancelledError: except CancelledError:
@ -45,6 +39,7 @@ class TelegramBot(Censor):
raise raise
except exceptions.ValidationError: except exceptions.ValidationError:
logger.debug(f'Bot {self.telegram_model.hood.name} has invalid auth token.') logger.debug(f'Bot {self.telegram_model.hood.name} has invalid auth token.')
await self.telegram_model.update(enabled=False)
finally: finally:
logger.debug(f'Bot {self.telegram_model.hood.name} stopped.') logger.debug(f'Bot {self.telegram_model.hood.name} stopped.')