Skip to content

Commit

Permalink
Add proxy support for telegram_bot (home-assistant#8717)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
azogue authored and fabaff committed Jul 30, 2017
1 parent cee49f3 commit 37fef40
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions homeassistant/components/telegram_bot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@
ATTR_USERNAME = 'username'

CONF_ALLOWED_CHAT_IDS = 'allowed_chat_ids'
CONF_PROXY_URL = 'proxy_url'
CONF_PROXY_PARAMS = 'proxy_params'

DOMAIN = 'telegram_bot'

Expand All @@ -85,6 +87,8 @@
vol.Required(CONF_ALLOWED_CHAT_IDS):
vol.All(cv.ensure_list, [vol.Coerce(int)]),
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({
Expand Down Expand Up @@ -240,7 +244,9 @@ def async_setup(hass, config):
hass,
p_config.get(CONF_API_KEY),
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
Expand Down Expand Up @@ -303,18 +309,24 @@ def _render_template_attr(data, attribute):
class TelegramNotificationService:
"""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."""
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]
self._last_message_id = {user: None for user in self.allowed_chat_ids}
self._parsers = {PARSER_HTML: ParseMode.HTML,
PARSER_MD: ParseMode.MARKDOWN}
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

def _get_msg_ids(self, msg_data, chat_id):
Expand Down

0 comments on commit 37fef40

Please sign in to comment.