Skip to content

Commit

Permalink
Fix telegram webhooks (#7236)
Browse files Browse the repository at this point in the history
* Always register the view if a webhook exists.
* Return True if platform is set up succesfully, False otherwise.
* Remove the webhook when home assistant stops. Webhooks and long
  polling are mutually excklusive. If a webhook is left after home
  assistant is stopped, a polling telegram bot is unable to be set up,
  on next start of home assistant.
  • Loading branch information
MartinHjelmare authored and balloob committed Apr 25, 2017
1 parent 6c594e2 commit 1b55dbe
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
12 changes: 5 additions & 7 deletions homeassistant/components/telegram_bot/__init__.py
Expand Up @@ -50,7 +50,7 @@ def async_setup_platform(p_type, p_config=None, discovery_info=None):
_LOGGER.error("Unknown notification service specified")
return

_LOGGER.info("Setting up1 %s.%s", DOMAIN, p_type)
_LOGGER.info("Setting up %s.%s", DOMAIN, p_type)

try:
if hasattr(platform, 'async_setup_platform'):
Expand All @@ -73,8 +73,6 @@ def async_setup_platform(p_type, p_config=None, discovery_info=None):
_LOGGER.exception('Error setting up platform %s', p_type)
return

return True

setup_tasks = [async_setup_platform(p_type, p_config) for p_type, p_config
in config_per_platform(config, DOMAIN)]

Expand Down Expand Up @@ -103,10 +101,10 @@ def process_message(self, data):
"""Check for basic message rules and fire an event if message is ok."""
data = data.get('message')

if (not data
or 'from' not in data
or 'text' not in data
or data['from'].get('id') not in self.allowed_chat_ids):
if (not data or
'from' not in data or
'text' not in data or
data['from'].get('id') not in self.allowed_chat_ids):
# Message is not correct.
_LOGGER.error("Incoming message does not have required data.")
return False
Expand Down
25 changes: 13 additions & 12 deletions homeassistant/components/telegram_bot/webhooks.py
Expand Up @@ -11,9 +11,8 @@

import voluptuous as vol


from homeassistant.const import (
HTTP_BAD_REQUEST, HTTP_UNAUTHORIZED)
EVENT_HOMEASSISTANT_STOP, HTTP_BAD_REQUEST, HTTP_UNAUTHORIZED)
import homeassistant.helpers.config_validation as cv
from homeassistant.components.http import HomeAssistantView
from homeassistant.components.telegram_bot import CONF_ALLOWED_CHAT_IDS, \
Expand All @@ -27,6 +26,7 @@
_LOGGER = logging.getLogger(__name__)

TELEGRAM_HANDLER_URL = '/api/telegram_webhooks'
REMOVE_HANDLER_URL = ''

CONF_TRUSTED_NETWORKS = 'trusted_networks'
DEFAULT_TRUSTED_NETWORKS = [
Expand All @@ -50,28 +50,29 @@ def setup_platform(hass, config, async_add_devices, discovery_info=None):
bot = telegram.Bot(config[CONF_API_KEY])

current_status = bot.getWebhookInfo()
handler_url = "{0}{1}".format(hass.config.api.base_url,
TELEGRAM_HANDLER_URL)
handler_url = '{0}{1}'.format(
hass.config.api.base_url, TELEGRAM_HANDLER_URL)
if current_status and current_status['url'] != handler_url:
if bot.setWebhook(handler_url):
_LOGGER.info("set new telegram webhook %s", handler_url)

hass.http.register_view(
BotPushReceiver(
hass,
config[CONF_ALLOWED_CHAT_IDS],
config[CONF_TRUSTED_NETWORKS]))

else:
_LOGGER.error("set telegram webhook failed %s", handler_url)
return False

hass.bus.listen_once(
EVENT_HOMEASSISTANT_STOP,
lambda event: bot.setWebhook(REMOVE_HANDLER_URL))
hass.http.register_view(BotPushReceiver(
hass, config[CONF_ALLOWED_CHAT_IDS], config[CONF_TRUSTED_NETWORKS]))
return True


class BotPushReceiver(HomeAssistantView, BaseTelegramBotEntity):
"""Handle pushes from telegram."""

requires_auth = False
url = TELEGRAM_HANDLER_URL
name = "telegram_webhooks"
name = 'telegram_webhooks'

def __init__(self, hass, allowed_chat_ids, trusted_networks):
"""Initialize the class."""
Expand Down

0 comments on commit 1b55dbe

Please sign in to comment.