mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 04:07:08 +00:00
Catch invalid chat ids
This commit is contained in:
parent
ab48a94d2a
commit
52b1080ccd
@ -15,6 +15,14 @@ from homeassistant.components.notify import (
|
|||||||
from homeassistant.const import CONF_API_KEY
|
from homeassistant.const import CONF_API_KEY
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
try:
|
||||||
|
import telegram
|
||||||
|
except ImportError:
|
||||||
|
_LOGGER.exception(
|
||||||
|
"Unable to import python-telegram-bot. "
|
||||||
|
"Did you maybe not install the 'python-telegram-bot' package?")
|
||||||
|
|
||||||
REQUIREMENTS = ['python-telegram-bot==2.8.7']
|
REQUIREMENTS = ['python-telegram-bot==2.8.7']
|
||||||
|
|
||||||
|
|
||||||
@ -26,18 +34,10 @@ def get_service(hass, config):
|
|||||||
_LOGGER):
|
_LOGGER):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
try:
|
|
||||||
import telegram
|
|
||||||
except ImportError:
|
|
||||||
_LOGGER.exception(
|
|
||||||
"Unable to import python-telegram-bot. "
|
|
||||||
"Did you maybe not install the 'python-telegram-bot' package?")
|
|
||||||
return None
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
bot = telegram.Bot(token=config[DOMAIN][CONF_API_KEY])
|
bot = telegram.Bot(token=config[DOMAIN][CONF_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
|
||||||
@ -52,7 +52,6 @@ class TelegramNotificationService(BaseNotificationService):
|
|||||||
""" Implements notification service for Telegram. """
|
""" Implements notification service for Telegram. """
|
||||||
|
|
||||||
def __init__(self, api_key, chat_id):
|
def __init__(self, api_key, chat_id):
|
||||||
import telegram
|
|
||||||
self._api_key = api_key
|
self._api_key = api_key
|
||||||
self._chat_id = chat_id
|
self._chat_id = chat_id
|
||||||
self.bot = telegram.Bot(token=self._api_key)
|
self.bot = telegram.Bot(token=self._api_key)
|
||||||
@ -62,5 +61,8 @@ class TelegramNotificationService(BaseNotificationService):
|
|||||||
|
|
||||||
title = kwargs.get(ATTR_TITLE)
|
title = kwargs.get(ATTR_TITLE)
|
||||||
|
|
||||||
|
try:
|
||||||
self.bot.sendMessage(chat_id=self._chat_id,
|
self.bot.sendMessage(chat_id=self._chat_id,
|
||||||
text=title + " " + message)
|
text=title + " " + message)
|
||||||
|
except telegram.error.TelegramError:
|
||||||
|
_LOGGER.error("Your chat id '%s' is not valid.", self._chat_id)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user