Bump python-telegram-bot package to 21.0.1 (#110297)
* Bump python-telegram-bot package version to the latest. * PySocks is no longer required as python-telegram-bot doesn't use urllib3 anymore. * Fix moved ParseMode import * Update filters import to new structure. * Refactor removed Request objects to HTTPXRequest objects. * Update to support asyncc functions * Update timeout to new kwarg connect_timeout is the most obvious option based on current param description, but this may need changing. * Compatibility typo. * Make methods async and use Bot client async natively * Type needs to be Optional That's what the source types are from the library Also handle new possibility of None value * Add socks support version of the library * Refactor load_data function Update to be async friendly Refactor to use httpx instead of requests. * Refactor Dispatcher references to Application This is the newer model of the same class. * Make more stuff async-friendly. * Update tests to refactor Dispatcher usage out. * Remove import and reference directly * Refactor typing method * Use async_fire now we have async support * Fix some over complicate inheritance. * Add the polling test telegram_text event fired back in. * Add extra context to comment * Handler should also be async * Use underscores instead of camelCase Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Renamed kwarg. * Refactor current timeout param to be read timeout Reading the old version of the library code I believe this matches the existing functionality best * Combine unload methods into one listener * Fix test by stopping HA as part of fixture * Add new fixture to mock stop_polling call Use this in all polling tests. * No longer need to check if application is running It was to stop a test failing. * Make sure the updater is started in tests Mock external call methods Remove stop_polling mock. * Use cleaner references to patched methods * Improve test by letting the library create the Update object * Mock component tear down methods to be async * Bump mypy cache version * Update dependency to install from git Allows use as a custom component in 2024.3 Allows us to track mypy issue resolution. * Update manifest and requirements for new python-telegram-bot release. * Remove pytest filterwarnings entry for old version of python-telegram-bot library. --------- Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
parent
15b59d310a
commit
d2effd8693
11 changed files with 240 additions and 192 deletions
|
@ -8,7 +8,7 @@ import string
|
|||
|
||||
from telegram import Update
|
||||
from telegram.error import TimedOut
|
||||
from telegram.ext import Dispatcher, TypeHandler
|
||||
from telegram.ext import Application, TypeHandler
|
||||
|
||||
from homeassistant.components.http import HomeAssistantView
|
||||
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
|
||||
|
@ -36,16 +36,17 @@ async def async_setup_platform(hass, bot, config):
|
|||
_LOGGER.error("Invalid telegram webhook %s must be https", pushbot.webhook_url)
|
||||
return False
|
||||
|
||||
await pushbot.start_application()
|
||||
webhook_registered = await pushbot.register_webhook()
|
||||
if not webhook_registered:
|
||||
return False
|
||||
|
||||
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, pushbot.deregister_webhook)
|
||||
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, pushbot.stop_application)
|
||||
hass.http.register_view(
|
||||
PushBotView(
|
||||
hass,
|
||||
bot,
|
||||
pushbot.dispatcher,
|
||||
pushbot.application,
|
||||
config[CONF_TRUSTED_NETWORKS],
|
||||
secret_token,
|
||||
)
|
||||
|
@ -57,13 +58,13 @@ class PushBot(BaseTelegramBotEntity):
|
|||
"""Handles all the push/webhook logic and passes telegram updates to `self.handle_update`."""
|
||||
|
||||
def __init__(self, hass, bot, config, secret_token):
|
||||
"""Create Dispatcher before calling super()."""
|
||||
"""Create Application before calling super()."""
|
||||
self.bot = bot
|
||||
self.trusted_networks = config[CONF_TRUSTED_NETWORKS]
|
||||
self.secret_token = secret_token
|
||||
# Dumb dispatcher that just gets our updates to our handler callback (self.handle_update)
|
||||
self.dispatcher = Dispatcher(bot, None)
|
||||
self.dispatcher.add_handler(TypeHandler(Update, self.handle_update))
|
||||
# Dumb Application that just gets our updates to our handler callback (self.handle_update)
|
||||
self.application = Application.builder().bot(bot).updater(None).build()
|
||||
self.application.add_handler(TypeHandler(Update, self.handle_update))
|
||||
super().__init__(hass, config)
|
||||
|
||||
self.base_url = config.get(CONF_URL) or get_url(
|
||||
|
@ -71,15 +72,15 @@ class PushBot(BaseTelegramBotEntity):
|
|||
)
|
||||
self.webhook_url = f"{self.base_url}{TELEGRAM_WEBHOOK_URL}"
|
||||
|
||||
def _try_to_set_webhook(self):
|
||||
async def _try_to_set_webhook(self):
|
||||
_LOGGER.debug("Registering webhook URL: %s", self.webhook_url)
|
||||
retry_num = 0
|
||||
while retry_num < 3:
|
||||
try:
|
||||
return self.bot.set_webhook(
|
||||
return await self.bot.set_webhook(
|
||||
self.webhook_url,
|
||||
api_kwargs={"secret_token": self.secret_token},
|
||||
timeout=5,
|
||||
connect_timeout=5,
|
||||
)
|
||||
except TimedOut:
|
||||
retry_num += 1
|
||||
|
@ -87,11 +88,14 @@ class PushBot(BaseTelegramBotEntity):
|
|||
|
||||
return False
|
||||
|
||||
async def start_application(self):
|
||||
"""Handle starting the Application object."""
|
||||
await self.application.initialize()
|
||||
await self.application.start()
|
||||
|
||||
async def register_webhook(self):
|
||||
"""Query telegram and register the URL for our webhook."""
|
||||
current_status = await self.hass.async_add_executor_job(
|
||||
self.bot.get_webhook_info
|
||||
)
|
||||
current_status = await self.bot.get_webhook_info()
|
||||
# Some logging of Bot current status:
|
||||
last_error_date = getattr(current_status, "last_error_date", None)
|
||||
if (last_error_date is not None) and (isinstance(last_error_date, int)):
|
||||
|
@ -105,7 +109,7 @@ class PushBot(BaseTelegramBotEntity):
|
|||
_LOGGER.debug("telegram webhook status: %s", current_status)
|
||||
|
||||
if current_status and current_status["url"] != self.webhook_url:
|
||||
result = await self.hass.async_add_executor_job(self._try_to_set_webhook)
|
||||
result = await self._try_to_set_webhook()
|
||||
if result:
|
||||
_LOGGER.info("Set new telegram webhook %s", self.webhook_url)
|
||||
else:
|
||||
|
@ -114,10 +118,16 @@ class PushBot(BaseTelegramBotEntity):
|
|||
|
||||
return True
|
||||
|
||||
def deregister_webhook(self, event=None):
|
||||
async def stop_application(self, event=None):
|
||||
"""Handle gracefully stopping the Application object."""
|
||||
await self.deregister_webhook()
|
||||
await self.application.stop()
|
||||
await self.application.shutdown()
|
||||
|
||||
async def deregister_webhook(self):
|
||||
"""Query telegram and deregister the URL for our webhook."""
|
||||
_LOGGER.debug("Deregistering webhook URL")
|
||||
return self.bot.delete_webhook()
|
||||
await self.bot.delete_webhook()
|
||||
|
||||
|
||||
class PushBotView(HomeAssistantView):
|
||||
|
@ -127,11 +137,11 @@ class PushBotView(HomeAssistantView):
|
|||
url = TELEGRAM_WEBHOOK_URL
|
||||
name = "telegram_webhooks"
|
||||
|
||||
def __init__(self, hass, bot, dispatcher, trusted_networks, secret_token):
|
||||
def __init__(self, hass, bot, application, trusted_networks, secret_token):
|
||||
"""Initialize by storing stuff needed for setting up our webhook endpoint."""
|
||||
self.hass = hass
|
||||
self.bot = bot
|
||||
self.dispatcher = dispatcher
|
||||
self.application = application
|
||||
self.trusted_networks = trusted_networks
|
||||
self.secret_token = secret_token
|
||||
|
||||
|
@ -153,6 +163,6 @@ class PushBotView(HomeAssistantView):
|
|||
|
||||
update = Update.de_json(update_data, self.bot)
|
||||
_LOGGER.debug("Received Update on %s: %s", self.url, update)
|
||||
await self.hass.async_add_executor_job(self.dispatcher.process_update, update)
|
||||
await self.application.process_update(update)
|
||||
|
||||
return None
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue