2017-05-02 16:18:47 +00:00
|
|
|
"""
|
|
|
|
Telegram bot polling implementation.
|
2017-04-12 04:10:56 +00:00
|
|
|
|
2017-05-02 16:18:47 +00:00
|
|
|
For more details about this platform, please refer to the documentation at
|
|
|
|
https://home-assistant.io/components/telegram_bot.polling/
|
|
|
|
"""
|
2017-04-12 04:10:56 +00:00
|
|
|
import asyncio
|
|
|
|
from asyncio.futures import CancelledError
|
|
|
|
import logging
|
|
|
|
|
|
|
|
import async_timeout
|
|
|
|
from aiohttp.client_exceptions import ClientError
|
2017-11-04 19:04:05 +00:00
|
|
|
from aiohttp.hdrs import CONNECTION, KEEP_ALIVE
|
2017-04-12 04:10:56 +00:00
|
|
|
|
2017-05-10 04:42:17 +00:00
|
|
|
from homeassistant.components.telegram_bot import (
|
2018-03-07 11:44:07 +00:00
|
|
|
initialize_bot,
|
2017-05-30 04:55:06 +00:00
|
|
|
CONF_ALLOWED_CHAT_IDS, BaseTelegramBotEntity,
|
|
|
|
PLATFORM_SCHEMA as TELEGRAM_PLATFORM_SCHEMA)
|
2017-05-10 04:42:17 +00:00
|
|
|
from homeassistant.const import (
|
2018-03-07 11:44:07 +00:00
|
|
|
EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP)
|
2017-04-12 04:10:56 +00:00
|
|
|
from homeassistant.core import callback
|
|
|
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2017-05-30 04:55:06 +00:00
|
|
|
PLATFORM_SCHEMA = TELEGRAM_PLATFORM_SCHEMA
|
2017-11-09 20:17:23 +00:00
|
|
|
RETRY_SLEEP = 10
|
|
|
|
|
|
|
|
|
|
|
|
class WrongHttpStatus(Exception):
|
|
|
|
"""Thrown when a wrong http status is received."""
|
|
|
|
|
|
|
|
pass
|
2017-05-30 04:55:06 +00:00
|
|
|
|
2017-04-12 04:10:56 +00:00
|
|
|
|
|
|
|
@asyncio.coroutine
|
2017-06-03 09:50:37 +00:00
|
|
|
def async_setup_platform(hass, config):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Set up the Telegram polling platform."""
|
2018-03-07 11:44:07 +00:00
|
|
|
bot = initialize_bot(config)
|
2017-04-12 04:10:56 +00:00
|
|
|
pol = TelegramPoll(bot, hass, config[CONF_ALLOWED_CHAT_IDS])
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def _start_bot(_event):
|
|
|
|
"""Start the bot."""
|
|
|
|
pol.start_polling()
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def _stop_bot(_event):
|
|
|
|
"""Stop the bot."""
|
|
|
|
pol.stop_polling()
|
|
|
|
|
2017-11-04 19:04:05 +00:00
|
|
|
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START, _start_bot)
|
|
|
|
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, _stop_bot)
|
2017-04-12 04:10:56 +00:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
class TelegramPoll(BaseTelegramBotEntity):
|
2017-11-04 19:04:05 +00:00
|
|
|
"""Asyncio telegram incoming message handler."""
|
2017-04-12 04:10:56 +00:00
|
|
|
|
|
|
|
def __init__(self, bot, hass, allowed_chat_ids):
|
|
|
|
"""Initialize the polling instance."""
|
|
|
|
BaseTelegramBotEntity.__init__(self, hass, allowed_chat_ids)
|
|
|
|
self.update_id = 0
|
|
|
|
self.websession = async_get_clientsession(hass)
|
|
|
|
self.update_url = '{0}/getUpdates'.format(bot.base_url)
|
2017-11-04 19:04:05 +00:00
|
|
|
self.polling_task = None # The actual polling task.
|
2017-04-12 04:10:56 +00:00
|
|
|
self.timeout = 15 # async post timeout
|
2017-11-04 19:04:05 +00:00
|
|
|
# Polling timeout should always be less than async post timeout.
|
2017-04-12 04:10:56 +00:00
|
|
|
self.post_data = {'timeout': self.timeout - 5}
|
|
|
|
|
|
|
|
def start_polling(self):
|
|
|
|
"""Start the polling task."""
|
|
|
|
self.polling_task = self.hass.async_add_job(self.check_incoming())
|
|
|
|
|
|
|
|
def stop_polling(self):
|
|
|
|
"""Stop the polling task."""
|
|
|
|
self.polling_task.cancel()
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def get_updates(self, offset):
|
|
|
|
"""Bypass the default long polling method to enable asyncio."""
|
|
|
|
resp = None
|
|
|
|
if offset:
|
|
|
|
self.post_data['offset'] = offset
|
|
|
|
try:
|
|
|
|
with async_timeout.timeout(self.timeout, loop=self.hass.loop):
|
|
|
|
resp = yield from self.websession.post(
|
|
|
|
self.update_url, data=self.post_data,
|
2017-11-04 19:04:05 +00:00
|
|
|
headers={CONNECTION: KEEP_ALIVE}
|
2017-04-12 04:10:56 +00:00
|
|
|
)
|
2017-06-12 04:42:35 +00:00
|
|
|
if resp.status == 200:
|
|
|
|
_json = yield from resp.json()
|
2017-11-09 20:17:23 +00:00
|
|
|
return _json
|
2018-07-23 08:16:05 +00:00
|
|
|
raise WrongHttpStatus('wrong status {}'.format(resp.status))
|
2017-04-12 04:10:56 +00:00
|
|
|
finally:
|
|
|
|
if resp is not None:
|
|
|
|
yield from resp.release()
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def check_incoming(self):
|
2017-11-09 20:17:23 +00:00
|
|
|
"""Continuously check for incoming telegram messages."""
|
2017-04-12 04:10:56 +00:00
|
|
|
try:
|
|
|
|
while True:
|
2017-11-09 20:17:23 +00:00
|
|
|
try:
|
|
|
|
_updates = yield from self.get_updates(self.update_id)
|
|
|
|
except (WrongHttpStatus, ClientError) as err:
|
|
|
|
# WrongHttpStatus: Non-200 status code.
|
|
|
|
# Occurs at times (mainly 502) and recovers
|
|
|
|
# automatically. Pause for a while before retrying.
|
|
|
|
_LOGGER.error(err)
|
|
|
|
yield from asyncio.sleep(RETRY_SLEEP)
|
|
|
|
except (asyncio.TimeoutError, ValueError):
|
|
|
|
# Long polling timeout. Nothing serious.
|
|
|
|
# Json error. Just retry for the next message.
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
# no exception raised. update received data.
|
|
|
|
_updates = _updates.get('result')
|
|
|
|
if _updates is None:
|
|
|
|
_LOGGER.error("Incorrect result received.")
|
|
|
|
else:
|
|
|
|
for update in _updates:
|
|
|
|
self.update_id = update['update_id'] + 1
|
|
|
|
self.process_message(update)
|
2017-04-12 04:10:56 +00:00
|
|
|
except CancelledError:
|
2017-05-02 20:47:20 +00:00
|
|
|
_LOGGER.debug("Stopping Telegram polling bot")
|