xooniverse / televerse

Your gateway to seamless Telegram Bot Development 💙
https://televerse.xooniverse.com
BSD 3-Clause "New" or "Revised" License
64 stars 11 forks source link

Fix #281: `api` re-initialization #282

Closed HeySreelal closed 3 months ago

HeySreelal commented 3 months ago

Fixes #281

What happened?

When the bot.start() is called twice regardless called consecutively or not - we call the _initializeBot method which in turn sets the RawAPI instance to the fetcher and performs the initial get me request.

The API instance on Fetcher is defined as:

  /// Raw API instance.
  late final RawAPI api;

And when we try to set the RawAPI instance for the second time (where it was already set) the system comes to crash as final variables cannot be set again.

What changed?

Added two methods, namely pause and resume to the Bot class to pause fetcher temporarily.

Why?

When the bot.stop() is called Televerse actually closes all the related resources - this includes Fetchers (LongPolling / Webhook), the underlying Dio instance (used for making http calls), etc.

So, instead of creating all the related instances once again, it's better to implement a pause/resume feature which can temporarily hold the update streaming.

Being said that Bot.stop now should only be called when you actually want to stop the bot and release all resources.