mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 12:17:07 +00:00
Add proxy support for telegram_bot (#8717)
* Add proxy support for telegram_bot New optional config parameters `proxy_url` and `proxy_params` (a dict) ```yaml telegram_bot: platform: polling api_key: !secret telegram_bot_api_key allowed_chat_ids: - !secret telegram_bot_chatid proxy_url: socks5://proxy_ip:proxy_port proxy_params: username: my-username password: my-secret-password ``` * change `ATTR_` for `CONF_` for config params
This commit is contained in:
parent
cee49f313f
commit
37fef4016e
@ -59,6 +59,8 @@ ATTR_USER_ID = 'user_id'
|
|||||||
ATTR_USERNAME = 'username'
|
ATTR_USERNAME = 'username'
|
||||||
|
|
||||||
CONF_ALLOWED_CHAT_IDS = 'allowed_chat_ids'
|
CONF_ALLOWED_CHAT_IDS = 'allowed_chat_ids'
|
||||||
|
CONF_PROXY_URL = 'proxy_url'
|
||||||
|
CONF_PROXY_PARAMS = 'proxy_params'
|
||||||
|
|
||||||
DOMAIN = 'telegram_bot'
|
DOMAIN = 'telegram_bot'
|
||||||
|
|
||||||
@ -85,6 +87,8 @@ PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA.extend({
|
|||||||
vol.Required(CONF_ALLOWED_CHAT_IDS):
|
vol.Required(CONF_ALLOWED_CHAT_IDS):
|
||||||
vol.All(cv.ensure_list, [vol.Coerce(int)]),
|
vol.All(cv.ensure_list, [vol.Coerce(int)]),
|
||||||
vol.Optional(ATTR_PARSER, default=PARSER_MD): cv.string,
|
vol.Optional(ATTR_PARSER, default=PARSER_MD): cv.string,
|
||||||
|
vol.Optional(CONF_PROXY_URL): cv.string,
|
||||||
|
vol.Optional(CONF_PROXY_PARAMS): dict,
|
||||||
})
|
})
|
||||||
|
|
||||||
BASE_SERVICE_SCHEMA = vol.Schema({
|
BASE_SERVICE_SCHEMA = vol.Schema({
|
||||||
@ -240,7 +244,9 @@ def async_setup(hass, config):
|
|||||||
hass,
|
hass,
|
||||||
p_config.get(CONF_API_KEY),
|
p_config.get(CONF_API_KEY),
|
||||||
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
|
||||||
@ -303,10 +309,12 @@ def async_setup(hass, config):
|
|||||||
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, api_key, allowed_chat_ids, parser,
|
||||||
|
proxy_url=None, proxy_params=None):
|
||||||
"""Initialize the service."""
|
"""Initialize the service."""
|
||||||
from telegram import Bot
|
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]
|
||||||
@ -314,7 +322,11 @@ 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)
|
||||||
self.bot = Bot(token=api_key)
|
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.hass = hass
|
self.hass = hass
|
||||||
|
|
||||||
def _get_msg_ids(self, msg_data, chat_id):
|
def _get_msg_ids(self, msg_data, chat_id):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user