mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 03:07:37 +00:00
Telegram_bot three platform support proxy_url and proxy_params (#12878)
* telegram_bot three platform support proxy_url and proxy_params * add telegram_bot._initialize_bot to create the bot instance * rename _initialize_bot to initialize_bot
This commit is contained in:
parent
d119610cf1
commit
35bae1eef2
@ -237,13 +237,12 @@ def async_setup(hass, config):
|
|||||||
_LOGGER.exception("Error setting up platform %s", p_type)
|
_LOGGER.exception("Error setting up platform %s", p_type)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
bot = initialize_bot(p_config)
|
||||||
notify_service = TelegramNotificationService(
|
notify_service = TelegramNotificationService(
|
||||||
hass,
|
hass,
|
||||||
p_config.get(CONF_API_KEY),
|
bot,
|
||||||
p_config.get(CONF_ALLOWED_CHAT_IDS),
|
p_config.get(CONF_ALLOWED_CHAT_IDS),
|
||||||
p_config.get(ATTR_PARSER),
|
p_config.get(ATTR_PARSER)
|
||||||
p_config.get(CONF_PROXY_URL),
|
|
||||||
p_config.get(CONF_PROXY_PARAMS)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
@ -302,15 +301,28 @@ def async_setup(hass, config):
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def initialize_bot(p_config):
|
||||||
|
"""Initialize telegram bot with proxy support."""
|
||||||
|
from telegram import Bot
|
||||||
|
from telegram.utils.request import Request
|
||||||
|
|
||||||
|
api_key = p_config.get(CONF_API_KEY)
|
||||||
|
proxy_url = p_config.get(CONF_PROXY_URL)
|
||||||
|
proxy_params = p_config.get(CONF_PROXY_PARAMS)
|
||||||
|
|
||||||
|
request = None
|
||||||
|
if proxy_url is not None:
|
||||||
|
request = Request(proxy_url=proxy_url,
|
||||||
|
urllib3_proxy_kwargs=proxy_params)
|
||||||
|
return Bot(token=api_key, request=request)
|
||||||
|
|
||||||
|
|
||||||
class TelegramNotificationService:
|
class TelegramNotificationService:
|
||||||
"""Implement the notification services for the Telegram Bot domain."""
|
"""Implement the notification services for the Telegram Bot domain."""
|
||||||
|
|
||||||
def __init__(self, hass, api_key, allowed_chat_ids, parser,
|
def __init__(self, hass, bot, allowed_chat_ids, parser):
|
||||||
proxy_url=None, proxy_params=None):
|
|
||||||
"""Initialize the service."""
|
"""Initialize the service."""
|
||||||
from telegram import Bot
|
|
||||||
from telegram.parsemode import ParseMode
|
from telegram.parsemode import ParseMode
|
||||||
from telegram.utils.request import Request
|
|
||||||
|
|
||||||
self.allowed_chat_ids = allowed_chat_ids
|
self.allowed_chat_ids = allowed_chat_ids
|
||||||
self._default_user = self.allowed_chat_ids[0]
|
self._default_user = self.allowed_chat_ids[0]
|
||||||
@ -318,11 +330,7 @@ class TelegramNotificationService:
|
|||||||
self._parsers = {PARSER_HTML: ParseMode.HTML,
|
self._parsers = {PARSER_HTML: ParseMode.HTML,
|
||||||
PARSER_MD: ParseMode.MARKDOWN}
|
PARSER_MD: ParseMode.MARKDOWN}
|
||||||
self._parse_mode = self._parsers.get(parser)
|
self._parse_mode = self._parsers.get(parser)
|
||||||
request = None
|
self.bot = bot
|
||||||
if proxy_url is not None:
|
|
||||||
request = Request(proxy_url=proxy_url,
|
|
||||||
urllib3_proxy_kwargs=proxy_params)
|
|
||||||
self.bot = Bot(token=api_key, request=request)
|
|
||||||
self.hass = hass
|
self.hass = hass
|
||||||
|
|
||||||
def _get_msg_ids(self, msg_data, chat_id):
|
def _get_msg_ids(self, msg_data, chat_id):
|
||||||
|
@ -8,8 +8,8 @@ import asyncio
|
|||||||
import logging
|
import logging
|
||||||
|
|
||||||
from homeassistant.components.telegram_bot import (
|
from homeassistant.components.telegram_bot import (
|
||||||
|
initialize_bot,
|
||||||
PLATFORM_SCHEMA as TELEGRAM_PLATFORM_SCHEMA)
|
PLATFORM_SCHEMA as TELEGRAM_PLATFORM_SCHEMA)
|
||||||
from homeassistant.const import CONF_API_KEY
|
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -20,8 +20,9 @@ PLATFORM_SCHEMA = TELEGRAM_PLATFORM_SCHEMA
|
|||||||
def async_setup_platform(hass, config):
|
def async_setup_platform(hass, config):
|
||||||
"""Set up the Telegram broadcast platform."""
|
"""Set up the Telegram broadcast platform."""
|
||||||
# Check the API key works
|
# Check the API key works
|
||||||
import telegram
|
|
||||||
bot = telegram.Bot(config[CONF_API_KEY])
|
bot = initialize_bot(config)
|
||||||
|
|
||||||
bot_config = yield from hass.async_add_job(bot.getMe)
|
bot_config = yield from hass.async_add_job(bot.getMe)
|
||||||
_LOGGER.debug("Telegram broadcast platform setup with bot %s",
|
_LOGGER.debug("Telegram broadcast platform setup with bot %s",
|
||||||
bot_config['username'])
|
bot_config['username'])
|
||||||
|
@ -13,10 +13,11 @@ from aiohttp.client_exceptions import ClientError
|
|||||||
from aiohttp.hdrs import CONNECTION, KEEP_ALIVE
|
from aiohttp.hdrs import CONNECTION, KEEP_ALIVE
|
||||||
|
|
||||||
from homeassistant.components.telegram_bot import (
|
from homeassistant.components.telegram_bot import (
|
||||||
|
initialize_bot,
|
||||||
CONF_ALLOWED_CHAT_IDS, BaseTelegramBotEntity,
|
CONF_ALLOWED_CHAT_IDS, BaseTelegramBotEntity,
|
||||||
PLATFORM_SCHEMA as TELEGRAM_PLATFORM_SCHEMA)
|
PLATFORM_SCHEMA as TELEGRAM_PLATFORM_SCHEMA)
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP, CONF_API_KEY)
|
EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP)
|
||||||
from homeassistant.core import callback
|
from homeassistant.core import callback
|
||||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||||
|
|
||||||
@ -35,8 +36,7 @@ class WrongHttpStatus(Exception):
|
|||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def async_setup_platform(hass, config):
|
def async_setup_platform(hass, config):
|
||||||
"""Set up the Telegram polling platform."""
|
"""Set up the Telegram polling platform."""
|
||||||
import telegram
|
bot = initialize_bot(config)
|
||||||
bot = telegram.Bot(config[CONF_API_KEY])
|
|
||||||
pol = TelegramPoll(bot, hass, config[CONF_ALLOWED_CHAT_IDS])
|
pol = TelegramPoll(bot, hass, config[CONF_ALLOWED_CHAT_IDS])
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
|
@ -14,9 +14,10 @@ import voluptuous as vol
|
|||||||
from homeassistant.components.http import HomeAssistantView
|
from homeassistant.components.http import HomeAssistantView
|
||||||
from homeassistant.components.http.const import KEY_REAL_IP
|
from homeassistant.components.http.const import KEY_REAL_IP
|
||||||
from homeassistant.components.telegram_bot import (
|
from homeassistant.components.telegram_bot import (
|
||||||
CONF_ALLOWED_CHAT_IDS, BaseTelegramBotEntity, PLATFORM_SCHEMA)
|
CONF_ALLOWED_CHAT_IDS, BaseTelegramBotEntity, PLATFORM_SCHEMA,
|
||||||
|
initialize_bot)
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
CONF_API_KEY, EVENT_HOMEASSISTANT_STOP, HTTP_BAD_REQUEST,
|
EVENT_HOMEASSISTANT_STOP, HTTP_BAD_REQUEST,
|
||||||
HTTP_UNAUTHORIZED, CONF_URL)
|
HTTP_UNAUTHORIZED, CONF_URL)
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
|
||||||
@ -50,7 +51,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|||||||
def async_setup_platform(hass, config):
|
def async_setup_platform(hass, config):
|
||||||
"""Set up the Telegram webhooks platform."""
|
"""Set up the Telegram webhooks platform."""
|
||||||
import telegram
|
import telegram
|
||||||
bot = telegram.Bot(config[CONF_API_KEY])
|
bot = initialize_bot(config)
|
||||||
|
|
||||||
current_status = yield from hass.async_add_job(bot.getWebhookInfo)
|
current_status = yield from hass.async_add_job(bot.getWebhookInfo)
|
||||||
base_url = config.get(CONF_URL, hass.config.api.base_url)
|
base_url = config.get(CONF_URL, hass.config.api.base_url)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user