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_botpull/12965/head
parent
d119610cf1
commit
35bae1eef2
|
@ -237,13 +237,12 @@ def async_setup(hass, config):
|
|||
_LOGGER.exception("Error setting up platform %s", p_type)
|
||||
return False
|
||||
|
||||
bot = initialize_bot(p_config)
|
||||
notify_service = TelegramNotificationService(
|
||||
hass,
|
||||
p_config.get(CONF_API_KEY),
|
||||
bot,
|
||||
p_config.get(CONF_ALLOWED_CHAT_IDS),
|
||||
p_config.get(ATTR_PARSER),
|
||||
p_config.get(CONF_PROXY_URL),
|
||||
p_config.get(CONF_PROXY_PARAMS)
|
||||
p_config.get(ATTR_PARSER)
|
||||
)
|
||||
|
||||
@asyncio.coroutine
|
||||
|
@ -302,15 +301,28 @@ def async_setup(hass, config):
|
|||
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:
|
||||
"""Implement the notification services for the Telegram Bot domain."""
|
||||
|
||||
def __init__(self, hass, api_key, allowed_chat_ids, parser,
|
||||
proxy_url=None, proxy_params=None):
|
||||
def __init__(self, hass, bot, allowed_chat_ids, parser):
|
||||
"""Initialize the service."""
|
||||
from telegram import Bot
|
||||
from telegram.parsemode import ParseMode
|
||||
from telegram.utils.request import Request
|
||||
|
||||
self.allowed_chat_ids = allowed_chat_ids
|
||||
self._default_user = self.allowed_chat_ids[0]
|
||||
|
@ -318,11 +330,7 @@ class TelegramNotificationService:
|
|||
self._parsers = {PARSER_HTML: ParseMode.HTML,
|
||||
PARSER_MD: ParseMode.MARKDOWN}
|
||||
self._parse_mode = self._parsers.get(parser)
|
||||
request = None
|
||||
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.bot = bot
|
||||
self.hass = hass
|
||||
|
||||
def _get_msg_ids(self, msg_data, chat_id):
|
||||
|
|
|
@ -8,8 +8,8 @@ import asyncio
|
|||
import logging
|
||||
|
||||
from homeassistant.components.telegram_bot import (
|
||||
initialize_bot,
|
||||
PLATFORM_SCHEMA as TELEGRAM_PLATFORM_SCHEMA)
|
||||
from homeassistant.const import CONF_API_KEY
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
@ -20,8 +20,9 @@ PLATFORM_SCHEMA = TELEGRAM_PLATFORM_SCHEMA
|
|||
def async_setup_platform(hass, config):
|
||||
"""Set up the Telegram broadcast platform."""
|
||||
# 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)
|
||||
_LOGGER.debug("Telegram broadcast platform setup with bot %s",
|
||||
bot_config['username'])
|
||||
|
|
|
@ -13,10 +13,11 @@ from aiohttp.client_exceptions import ClientError
|
|||
from aiohttp.hdrs import CONNECTION, KEEP_ALIVE
|
||||
|
||||
from homeassistant.components.telegram_bot import (
|
||||
initialize_bot,
|
||||
CONF_ALLOWED_CHAT_IDS, BaseTelegramBotEntity,
|
||||
PLATFORM_SCHEMA as TELEGRAM_PLATFORM_SCHEMA)
|
||||
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.helpers.aiohttp_client import async_get_clientsession
|
||||
|
||||
|
@ -35,8 +36,7 @@ class WrongHttpStatus(Exception):
|
|||
@asyncio.coroutine
|
||||
def async_setup_platform(hass, config):
|
||||
"""Set up the Telegram polling platform."""
|
||||
import telegram
|
||||
bot = telegram.Bot(config[CONF_API_KEY])
|
||||
bot = initialize_bot(config)
|
||||
pol = TelegramPoll(bot, hass, config[CONF_ALLOWED_CHAT_IDS])
|
||||
|
||||
@callback
|
||||
|
|
|
@ -14,9 +14,10 @@ import voluptuous as vol
|
|||
from homeassistant.components.http import HomeAssistantView
|
||||
from homeassistant.components.http.const import KEY_REAL_IP
|
||||
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 (
|
||||
CONF_API_KEY, EVENT_HOMEASSISTANT_STOP, HTTP_BAD_REQUEST,
|
||||
EVENT_HOMEASSISTANT_STOP, HTTP_BAD_REQUEST,
|
||||
HTTP_UNAUTHORIZED, CONF_URL)
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
|
||||
|
@ -50,7 +51,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|||
def async_setup_platform(hass, config):
|
||||
"""Set up the Telegram webhooks platform."""
|
||||
import telegram
|
||||
bot = telegram.Bot(config[CONF_API_KEY])
|
||||
bot = initialize_bot(config)
|
||||
|
||||
current_status = yield from hass.async_add_job(bot.getWebhookInfo)
|
||||
base_url = config.get(CONF_URL, hass.config.api.base_url)
|
||||
|
|
Loading…
Reference in New Issue