fix telegram bug (#2653)

This commit is contained in:
Pascal Vizeli 2016-07-29 15:20:23 +02:00 committed by GitHub
parent 55624bcff9
commit 548d154cd8

View File

@ -8,43 +8,69 @@ import io
import logging import logging
import urllib import urllib
import requests import requests
from requests.auth import HTTPBasicAuth import voluptuous as vol
import homeassistant.helpers.config_validation as cv
from homeassistant.components.notify import ( from homeassistant.components.notify import (
ATTR_TITLE, ATTR_DATA, DOMAIN, BaseNotificationService) ATTR_TITLE, ATTR_DATA, BaseNotificationService)
from homeassistant.const import CONF_API_KEY from homeassistant.const import (CONF_API_KEY, CONF_NAME, ATTR_LOCATION,
from homeassistant.helpers import validate_config ATTR_LATITUDE, ATTR_LONGITUDE, CONF_PLATFORM)
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
REQUIREMENTS = ['python-telegram-bot==5.0.0'] REQUIREMENTS = ['python-telegram-bot==5.0.0']
ATTR_PHOTO = "photo" ATTR_PHOTO = "photo"
ATTR_FILE = "file"
ATTR_URL = "url"
ATTR_CAPTION = "caption" ATTR_CAPTION = "caption"
ATTR_USERNAME = "username"
ATTR_PASSWORD = "password" CONF_CHAT_ID = 'chat_id'
PLATFORM_SCHEMA = vol.Schema({
vol.Required(CONF_PLATFORM): "telegram",
vol.Optional(CONF_NAME): cv.string,
vol.Required(CONF_API_KEY): cv.string,
vol.Required(CONF_CHAT_ID): cv.string,
})
def get_service(hass, config): def get_service(hass, config):
"""Get the Telegram notification service.""" """Get the Telegram notification service."""
import telegram import telegram
if not validate_config({DOMAIN: config},
{DOMAIN: [CONF_API_KEY, 'chat_id']},
_LOGGER):
return None
try: try:
bot = telegram.Bot(token=config[CONF_API_KEY]) chat_id = config.get(CONF_CHAT_ID)
api_key = config.get(CONF_API_KEY)
bot = telegram.Bot(token=api_key)
username = bot.getMe()['username'] username = bot.getMe()['username']
_LOGGER.info("Telegram bot is '%s'.", username) _LOGGER.info("Telegram bot is '%s'.", username)
except urllib.error.HTTPError: except urllib.error.HTTPError:
_LOGGER.error("Please check your access token.") _LOGGER.error("Please check your access token.")
return None return None
return TelegramNotificationService(config[CONF_API_KEY], config['chat_id']) return TelegramNotificationService(api_key, chat_id)
def load_data(url=None, file=None, username=None, password=None):
"""Load photo/document into ByteIO/File container from a source."""
try:
if url is not None:
# load photo from url
if username is not None and password is not None:
req = requests.get(url, auth=(username, password), timeout=15)
else:
req = requests.get(url, timeout=15)
return io.BytesIO(req.content)
elif file is not None:
# load photo from file
return open(file, "rb")
else:
_LOGGER.warning("Can't load photo no photo found in params!")
except (OSError, IOError, requests.exceptions.RequestException):
_LOGGER.error("Can't load photo into ByteIO")
return None
# pylint: disable=too-few-public-methods # pylint: disable=too-few-public-methods
@ -64,7 +90,18 @@ class TelegramNotificationService(BaseNotificationService):
import telegram import telegram
title = kwargs.get(ATTR_TITLE) title = kwargs.get(ATTR_TITLE)
data = kwargs.get(ATTR_DATA, {}) data = kwargs.get(ATTR_DATA)
# exists data for send a photo/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]
for photo_data in photos:
self.send_photo(photo_data)
return
elif data is not None and ATTR_LOCATION in data:
return self.send_location(data.get(ATTR_LOCATION))
# send message # send message
try: try:
@ -74,41 +111,30 @@ class TelegramNotificationService(BaseNotificationService):
_LOGGER.exception("Error sending message.") _LOGGER.exception("Error sending message.")
return return
def send_photo(self, data):
"""Send a photo."""
import telegram
caption = data.pop(ATTR_CAPTION, None)
# send photo # send photo
if ATTR_PHOTO in data: try:
# if not a list photo = load_data(**data)
if not isinstance(data[ATTR_PHOTO], list): self.bot.sendPhoto(chat_id=self._chat_id,
photos = [data[ATTR_PHOTO]] photo=photo, caption=caption)
else: except telegram.error.TelegramError:
photos = data[ATTR_PHOTO] _LOGGER.exception("Error sending photo.")
return
try: def send_location(self, gps):
for photo_data in photos: """Send a location."""
caption = photo_data.get(ATTR_CAPTION, None) import telegram
latitude = float(gps.get(ATTR_LATITUDE, 0.0))
longitude = float(gps.get(ATTR_LONGITUDE, 0.0))
# file is a url # send location
if ATTR_URL in photo_data: try:
# use http authenticate self.bot.sendLocation(chat_id=self._chat_id,
if ATTR_USERNAME in photo_data and\ latitude=latitude, longitude=longitude)
ATTR_PASSWORD in photo_data: except telegram.error.TelegramError:
req = requests.get( _LOGGER.exception("Error sending location.")
photo_data[ATTR_URL], return
auth=HTTPBasicAuth(photo_data[ATTR_USERNAME],
photo_data[ATTR_PASSWORD])
)
else:
req = requests.get(photo_data[ATTR_URL])
file_id = io.BytesIO(req.content)
elif ATTR_FILE in photo_data:
file_id = open(photo_data[ATTR_FILE], "rb")
else:
_LOGGER.error("No url or path is set for photo!")
continue
self.bot.sendPhoto(chat_id=self._chat_id,
photo=file_id, caption=caption)
except (OSError, IOError, telegram.error.TelegramError,
urllib.error.HTTPError):
_LOGGER.exception("Error sending photo.")
return