Home Assistant Unofficial Reference 2024.12.1
webhooks.py
Go to the documentation of this file.
1 """Support for Telegram bots using webhooks."""
2 
3 import datetime as dt
4 from http import HTTPStatus
5 from ipaddress import ip_address
6 import logging
7 import secrets
8 import string
9 
10 from telegram import Update
11 from telegram.error import TimedOut
12 from telegram.ext import Application, TypeHandler
13 
14 from homeassistant.components.http import HomeAssistantView
15 from homeassistant.const import EVENT_HOMEASSISTANT_STOP
16 from homeassistant.helpers.network import get_url
17 
18 from . import CONF_TRUSTED_NETWORKS, CONF_URL, BaseTelegramBotEntity
19 
20 _LOGGER = logging.getLogger(__name__)
21 
22 TELEGRAM_WEBHOOK_URL = "/api/telegram_webhooks"
23 REMOVE_WEBHOOK_URL = ""
24 SECRET_TOKEN_LENGTH = 32
25 
26 
27 async def async_setup_platform(hass, bot, config):
28  """Set up the Telegram webhooks platform."""
29 
30  # Generate an ephemeral secret token
31  alphabet = string.ascii_letters + string.digits + "-_"
32  secret_token = "".join(secrets.choice(alphabet) for _ in range(SECRET_TOKEN_LENGTH))
33 
34  pushbot = PushBot(hass, bot, config, secret_token)
35 
36  if not pushbot.webhook_url.startswith("https"):
37  _LOGGER.error("Invalid telegram webhook %s must be https", pushbot.webhook_url)
38  return False
39 
40  await pushbot.start_application()
41  webhook_registered = await pushbot.register_webhook()
42  if not webhook_registered:
43  return False
44 
45  hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, pushbot.stop_application)
46  hass.http.register_view(
48  hass,
49  bot,
50  pushbot.application,
51  config[CONF_TRUSTED_NETWORKS],
52  secret_token,
53  )
54  )
55  return True
56 
57 
58 class PushBot(BaseTelegramBotEntity):
59  """Handles all the push/webhook logic and passes telegram updates to `self.handle_update`."""
60 
61  def __init__(self, hass, bot, config, secret_token):
62  """Create Application before calling super()."""
63  self.botbot = bot
64  self.trusted_networkstrusted_networks = config[CONF_TRUSTED_NETWORKS]
65  self.secret_tokensecret_token = secret_token
66  # Dumb Application that just gets our updates to our handler callback (self.handle_update)
67  self.applicationapplication = Application.builder().bot(bot).updater(None).build()
68  self.applicationapplication.add_handler(TypeHandler(Update, self.handle_update))
69  super().__init__(hass, config)
70 
71  self.base_urlbase_url = config.get(CONF_URL) or get_url(
72  hass, require_ssl=True, allow_internal=False
73  )
74  self.webhook_urlwebhook_url = f"{self.base_url}{TELEGRAM_WEBHOOK_URL}"
75 
76  async def _try_to_set_webhook(self):
77  _LOGGER.debug("Registering webhook URL: %s", self.webhook_urlwebhook_url)
78  retry_num = 0
79  while retry_num < 3:
80  try:
81  return await self.botbot.set_webhook(
82  self.webhook_urlwebhook_url,
83  api_kwargs={"secret_token": self.secret_tokensecret_token},
84  connect_timeout=5,
85  )
86  except TimedOut:
87  retry_num += 1
88  _LOGGER.warning("Timeout trying to set webhook (retry #%d)", retry_num)
89 
90  return False
91 
92  async def start_application(self):
93  """Handle starting the Application object."""
94  await self.applicationapplication.initialize()
95  await self.applicationapplication.start()
96 
97  async def register_webhook(self):
98  """Query telegram and register the URL for our webhook."""
99  current_status = await self.botbot.get_webhook_info()
100  # Some logging of Bot current status:
101  last_error_date = getattr(current_status, "last_error_date", None)
102  if (last_error_date is not None) and (isinstance(last_error_date, int)):
103  last_error_date = dt.datetime.fromtimestamp(last_error_date)
104  _LOGGER.debug(
105  "Telegram webhook last_error_date: %s. Status: %s",
106  last_error_date,
107  current_status,
108  )
109  else:
110  _LOGGER.debug("telegram webhook status: %s", current_status)
111 
112  if current_status and current_status["url"] != self.webhook_urlwebhook_url:
113  result = await self._try_to_set_webhook_try_to_set_webhook()
114  if result:
115  _LOGGER.debug("Set new telegram webhook %s", self.webhook_urlwebhook_url)
116  else:
117  _LOGGER.error("Set telegram webhook failed %s", self.webhook_urlwebhook_url)
118  return False
119 
120  return True
121 
122  async def stop_application(self, event=None):
123  """Handle gracefully stopping the Application object."""
124  await self.deregister_webhookderegister_webhook()
125  await self.applicationapplication.stop()
126  await self.applicationapplication.shutdown()
127 
128  async def deregister_webhook(self):
129  """Query telegram and deregister the URL for our webhook."""
130  _LOGGER.debug("Deregistering webhook URL")
131  await self.botbot.delete_webhook()
132 
133 
134 class PushBotView(HomeAssistantView):
135  """View for handling webhook calls from Telegram."""
136 
137  requires_auth = False
138  url = TELEGRAM_WEBHOOK_URL
139  name = "telegram_webhooks"
140 
141  def __init__(self, hass, bot, application, trusted_networks, secret_token):
142  """Initialize by storing stuff needed for setting up our webhook endpoint."""
143  self.hasshass = hass
144  self.botbot = bot
145  self.applicationapplication = application
146  self.trusted_networkstrusted_networks = trusted_networks
147  self.secret_tokensecret_token = secret_token
148 
149  async def post(self, request):
150  """Accept the POST from telegram."""
151  real_ip = ip_address(request.remote)
152  if not any(real_ip in net for net in self.trusted_networkstrusted_networks):
153  _LOGGER.warning("Access denied from %s", real_ip)
154  return self.json_message("Access denied", HTTPStatus.UNAUTHORIZED)
155  secret_token_header = request.headers.get("X-Telegram-Bot-Api-Secret-Token")
156  if secret_token_header is None or self.secret_tokensecret_token != secret_token_header:
157  _LOGGER.warning("Invalid secret token from %s", real_ip)
158  return self.json_message("Access denied", HTTPStatus.UNAUTHORIZED)
159 
160  try:
161  update_data = await request.json()
162  except ValueError:
163  return self.json_message("Invalid JSON", HTTPStatus.BAD_REQUEST)
164 
165  update = Update.de_json(update_data, self.botbot)
166  _LOGGER.debug("Received Update on %s: %s", self.urlurl, update)
167  await self.applicationapplication.process_update(update)
168 
169  return None
def __init__(self, hass, bot, application, trusted_networks, secret_token)
Definition: webhooks.py:141
def __init__(self, hass, bot, config, secret_token)
Definition: webhooks.py:61
def async_setup_platform(hass, bot, config)
Definition: webhooks.py:27
str get_url(HomeAssistant hass, *bool require_current_request=False, bool require_ssl=False, bool require_standard_port=False, bool require_cloud=False, bool allow_internal=True, bool allow_external=True, bool allow_cloud=True, bool|None allow_ip=None, bool|None prefer_external=None, bool prefer_cloud=False)
Definition: network.py:131