telegram_bot: Support for sending videos (#10470)

* telegram_bot: Support for sending videos

Telegram python library has a sendVideo function that can be used
similar to sending photos and documents.

* fix lint issue

* fix grammar
This commit is contained in:
Vignesh Venkat 2017-11-11 15:13:35 -08:00 committed by Anders Melchiorsen
parent 79001fc361
commit 96e7944fa8
3 changed files with 54 additions and 9 deletions

View File

@ -21,6 +21,7 @@ DEPENDENCIES = [DOMAIN]
ATTR_KEYBOARD = 'keyboard'
ATTR_INLINE_KEYBOARD = 'inline_keyboard'
ATTR_PHOTO = 'photo'
ATTR_VIDEO = 'video'
ATTR_DOCUMENT = 'document'
CONF_CHAT_ID = 'chat_id'
@ -63,7 +64,7 @@ class TelegramNotificationService(BaseNotificationService):
keys = keys if isinstance(keys, list) else [keys]
service_data.update(inline_keyboard=keys)
# Send a photo, a document or a location
# Send a photo, video, document, or location
if data is not None and ATTR_PHOTO in data:
photos = data.get(ATTR_PHOTO, None)
photos = photos if isinstance(photos, list) else [photos]
@ -72,6 +73,14 @@ class TelegramNotificationService(BaseNotificationService):
self.hass.services.call(
DOMAIN, 'send_photo', service_data=service_data)
return
elif data is not None and ATTR_VIDEO in data:
videos = data.get(ATTR_VIDEO, None)
videos = videos if isinstance(videos, list) else [videos]
for video_data in videos:
service_data.update(video_data)
self.hass.services.call(
DOMAIN, 'send_video', service_data=service_data)
return
elif data is not None and ATTR_LOCATION in data:
service_data.update(data.get(ATTR_LOCATION))
return self.hass.services.call(

View File

@ -65,6 +65,7 @@ DOMAIN = 'telegram_bot'
SERVICE_SEND_MESSAGE = 'send_message'
SERVICE_SEND_PHOTO = 'send_photo'
SERVICE_SEND_VIDEO = 'send_video'
SERVICE_SEND_DOCUMENT = 'send_document'
SERVICE_SEND_LOCATION = 'send_location'
SERVICE_EDIT_MESSAGE = 'edit_message'
@ -154,6 +155,7 @@ SERVICE_SCHEMA_DELETE_MESSAGE = vol.Schema({
SERVICE_MAP = {
SERVICE_SEND_MESSAGE: SERVICE_SCHEMA_SEND_MESSAGE,
SERVICE_SEND_PHOTO: SERVICE_SCHEMA_SEND_FILE,
SERVICE_SEND_VIDEO: SERVICE_SCHEMA_SEND_FILE,
SERVICE_SEND_DOCUMENT: SERVICE_SCHEMA_SEND_FILE,
SERVICE_SEND_LOCATION: SERVICE_SCHEMA_SEND_LOCATION,
SERVICE_EDIT_MESSAGE: SERVICE_SCHEMA_EDIT_MESSAGE,
@ -277,12 +279,11 @@ def async_setup(hass, config):
if msgtype == SERVICE_SEND_MESSAGE:
yield from hass.async_add_job(
partial(notify_service.send_message, **kwargs))
elif msgtype == SERVICE_SEND_PHOTO:
elif (msgtype == SERVICE_SEND_PHOTO or
msgtype == SERVICE_SEND_VIDEO or
msgtype == SERVICE_SEND_DOCUMENT):
yield from hass.async_add_job(
partial(notify_service.send_file, True, **kwargs))
elif msgtype == SERVICE_SEND_DOCUMENT:
yield from hass.async_add_job(
partial(notify_service.send_file, False, **kwargs))
partial(notify_service.send_file, msgtype, **kwargs))
elif msgtype == SERVICE_SEND_LOCATION:
yield from hass.async_add_job(
partial(notify_service.send_location, **kwargs))
@ -518,11 +519,15 @@ class TelegramNotificationService:
callback_query_id,
text=message, show_alert=show_alert, **params)
def send_file(self, is_photo=True, target=None, **kwargs):
"""Send a photo or a document."""
def send_file(self, file_type=SERVICE_SEND_PHOTO, target=None, **kwargs):
"""Send a photo, video, or document."""
params = self._get_msg_kwargs(kwargs)
caption = kwargs.get(ATTR_CAPTION)
func_send = self.bot.sendPhoto if is_photo else self.bot.sendDocument
func_send = {
SERVICE_SEND_PHOTO: self.bot.sendPhoto,
SERVICE_SEND_VIDEO: self.bot.sendVideo,
SERVICE_SEND_DOCUMENT: self.bot.sendDocument
}.get(file_type)
file_content = load_data(
self.hass,
url=kwargs.get(ATTR_URL),

View File

@ -59,6 +59,37 @@ send_photo:
description: List of rows of commands, comma-separated, to make a custom inline keyboard with buttons with asociated callback data.
example: '["/button1, /button2", "/button3"] or [[["Text button1", "/button1"], ["Text button2", "/button2"]], [["Text button3", "/button3"]]]'
send_video:
description: Send a video.
fields:
url:
description: Remote path to a video.
example: 'http://example.org/path/to/the/video.mp4'
file:
description: Local path to an image.
example: '/path/to/the/video.mp4'
caption:
description: The title of the video.
example: 'My video'
username:
description: Username for a URL which require HTTP basic authentication.
example: myuser
password:
description: Password for a URL which require HTTP basic authentication.
example: myuser_pwd
target:
description: An array of pre-authorized chat_ids to send the document to. If not present, first allowed chat_id is the default.
example: '[12345, 67890] or 12345'
disable_notification:
description: Sends the message silently. iOS users and Web users will not receive a notification, Android users will receive a notification with no sound.
example: true
keyboard:
description: List of rows of commands, comma-separated, to make a custom keyboard.
example: '["/command1, /command2", "/command3"]'
inline_keyboard:
description: List of rows of commands, comma-separated, to make a custom inline keyboard with buttons with asociated callback data.
example: '["/button1, /button2", "/button3"] or [[["Text button1", "/button1"], ["Text button2", "/button2"]], [["Text button3", "/button3"]]]'
send_document:
description: Send a document.
fields: