Telegram webhooks (#5793)

* new component telegram_webhooks

* keyboard support in telegram notify

* telegram_webhooks has no tests

* requirement like notify/telegram

* ops, requirements_all.txt needed for travis

* ops, requirements_all.txt is generated by script/gen_requirements_all.py

* check telegram trusted networks in web handler

* raise an event now

* use of hass.config.api.base_url

* more readable

* small cleanups

* Small style change for HA guideline

* fix lint

* revert return to origin
This commit is contained in:
Stefano Scipioni authored and Pascal Vizeli committed 2017-02-09 23:05:28 +01:00
1 parent ecbbb06b2f
commit ebfff6a907
4 files changed
+149 -4

No files matched your search

+17 -4
View File
@@ -22,6 +22,7 @@ _LOGGER = logging.getLogger(__name__)
REQUIREMENTS = ['python-telegram-bot==5.3.0']
ATTR_PHOTO = 'photo'
ATTR_KEYBOARD = 'keyboard'
ATTR_DOCUMENT = 'document'
ATTR_CAPTION = 'caption'
ATTR_URL = 'url'
@@ -107,6 +108,10 @@ class TelegramNotificationService(BaseNotificationService):
return self.send_location(data.get(ATTR_LOCATION))
elif data is not None and ATTR_DOCUMENT in data:
return self.send_document(data.get(ATTR_DOCUMENT))
elif data is not None and ATTR_KEYBOARD in data:
keys = data.get(ATTR_KEYBOARD)
keys = keys if isinstance(keys, list) else [keys]
return self.send_keyboard(message, keys)
if title:
text = '{} {}'.format(title, message)
@@ -122,7 +127,18 @@ class TelegramNotificationService(BaseNotificationService):
parse_mode=parse_mode)
except telegram.error.TelegramError:
_LOGGER.exception("Error sending message")
return
def send_keyboard(self, message, keys):
"""Display keyboard."""
import telegram
keyboard = telegram.ReplyKeyboardMarkup([
[key.strip() for key in row.split(",")] for row in keys])
try:
self.bot.sendMessage(chat_id=self._chat_id, text=message,
reply_markup=keyboard)
except telegram.error.TelegramError:
_LOGGER.exception("Error sending message")
def send_photo(self, data):
"""Send a photo."""
@@ -141,7 +157,6 @@ class TelegramNotificationService(BaseNotificationService):
photo=photo, caption=caption)
except telegram.error.TelegramError:
_LOGGER.exception("Error sending photo")
return
def send_document(self, data):
"""Send a document."""
@@ -160,7 +175,6 @@ class TelegramNotificationService(BaseNotificationService):
document=document, caption=caption)
except telegram.error.TelegramError:
_LOGGER.exception("Error sending document")
return
def send_location(self, gps):
"""Send a location."""
@@ -174,4 +188,3 @@ class TelegramNotificationService(BaseNotificationService):
latitude=latitude, longitude=longitude)
except telegram.error.TelegramError:
_LOGGER.exception("Error sending location")
return