Telegram Bot enhancements with callback queries and new notification services (#7454)

* telegram_bot and notify.telegram enhancements:
- Receive callback queries and produce `telegram_callback` events.
- Custom reply_markup (keyboard or inline_keyboard) for every type of message (message, photo, location & document).
- `disable_notification`, `disable_web_page_preview`, `reply_to_message_id` and `parse_mode` optional keyword args.
- Line break between title and message fields: `'{}\n{}'.format(title, message)`
- Move Telegram notification services to `telegram_bot` component and forward service calls from the telegram notify service to the telegram component, so now the `notify.telegram` platform depends of `telegram_bot`, and there is no need for `api_key` in the notifier configuration. The notifier calls the new notification services of the bot component:
	- telegram_bot/send_message
	- telegram_bot/send_photo
	- telegram_bot/send_document
	- telegram_bot/send_location
	- telegram_bot/edit_message
	- telegram_bot/edit_caption
	- telegram_bot/edit_replymarkup
	- telegram_bot/answer_callback_query
- Added descriptions of the new notification services with a services.yaml file.
- CONFIG_SCHEMA instead of PLATFORM_SCHEMA for the `telegram_bot` component, so only one platform is allowed.
- Async component setup.

* telegram_bot and notify.telegram enhancements: change in requirements_all.txt.
This commit is contained in:
Eugenio Panadero 2017-05-10 06:42:17 +02:00 committed by Paulus Schoutsen
parent 1312ee0f7d
commit b30c352e37
6 changed files with 763 additions and 242 deletions

View file

@ -5,60 +5,52 @@ For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/telegram_bot.webhooks/
"""
import asyncio
import datetime as dt
import logging
from ipaddress import ip_network
import voluptuous as vol
from homeassistant.const import (
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, \
BaseTelegramBotEntity, PLATFORM_SCHEMA
from homeassistant.const import CONF_API_KEY
from homeassistant.components.http.util import get_real_ip
from homeassistant.components.telegram_bot import (
CONF_ALLOWED_CHAT_IDS, CONF_TRUSTED_NETWORKS, BaseTelegramBotEntity)
from homeassistant.const import (
CONF_API_KEY, EVENT_HOMEASSISTANT_STOP,
HTTP_BAD_REQUEST, HTTP_UNAUTHORIZED)
DEPENDENCIES = ['http']
REQUIREMENTS = ['python-telegram-bot==5.3.1']
_LOGGER = logging.getLogger(__name__)
TELEGRAM_HANDLER_URL = '/api/telegram_webhooks'
REMOVE_HANDLER_URL = ''
CONF_TRUSTED_NETWORKS = 'trusted_networks'
DEFAULT_TRUSTED_NETWORKS = [
ip_network('149.154.167.197/32'),
ip_network('149.154.167.198/31'),
ip_network('149.154.167.200/29'),
ip_network('149.154.167.208/28'),
ip_network('149.154.167.224/29'),
ip_network('149.154.167.232/31')
]
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Optional(CONF_TRUSTED_NETWORKS, default=DEFAULT_TRUSTED_NETWORKS):
vol.All(cv.ensure_list, [ip_network])
})
def setup_platform(hass, config, async_add_devices, discovery_info=None):
@asyncio.coroutine
def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
"""Set up the Telegram webhooks platform."""
import telegram
bot = telegram.Bot(config[CONF_API_KEY])
current_status = bot.getWebhookInfo()
handler_url = '{0}{1}'.format(
hass.config.api.base_url, TELEGRAM_HANDLER_URL)
current_status = yield from hass.async_add_job(bot.getWebhookInfo)
# 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)):
last_error_date = dt.datetime.fromtimestamp(last_error_date)
_LOGGER.info("telegram webhook last_error_date: %s. Status: %s",
last_error_date, current_status)
else:
_LOGGER.debug("telegram webhook Status: %s", current_status)
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):
result = yield from hass.async_add_job(bot.setWebhook, handler_url)
if result:
_LOGGER.info("Set new telegram webhook %s", handler_url)
else:
_LOGGER.error("Set telegram webhook failed %s", handler_url)
return False
hass.bus.listen_once(
hass.bus.async_listen_once(
EVENT_HOMEASSISTANT_STOP,
lambda event: bot.setWebhook(REMOVE_HANDLER_URL))
hass.http.register_view(BotPushReceiver(