Skip to content

Commit

Permalink
Merge b2d8165 into c8e570f
Browse files Browse the repository at this point in the history
  • Loading branch information
brickerino committed Nov 1, 2018
2 parents c8e570f + b2d8165 commit 20242f5
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
6 changes: 6 additions & 0 deletions docs/observers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,12 @@ or pickle file containing...
* optionally: a boolean for ``silent_completion``. If set to true, regular experiment completions
will use no or less intrusive notifications, depending on the receiving device's platform.
Experiment starts will always be sent silently, interruptions and failures always with full notifications.
* optionally: a string for ``proxy_url``. Specify this field, if Telegram is blocked in the local network or
in the country, and you want to use proxy server.
Format: ``PROTOCOL://PROXY_HOST:[PROXY_PORT]/``. Socks5 and HTTP protocols are supported.
These settings also could be received from ``HTTPS_PROXY`` or ``https_proxy`` environment variable.
* optionally: ``username`` for proxy.
* optionally: ``password`` for proxy.

The observer is then added to the experment like this:

Expand Down
31 changes: 30 additions & 1 deletion sacred/observers/telegram_obs.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,37 @@ def from_config(cls, filename):
import telegram
d = load_config_file(filename)
obs = None

if 'proxy_url' in d:
from telegram.utils.request import Request

if d['proxy_url'].startswith('socks5'):
urllib3_proxy_kwargs = dict()
for key in ['username', 'password']:
if key in d:
urllib3_proxy_kwargs[key] = d[key]
request = Request(proxy_url=d['proxy_url'], urllib3_proxy_kwargs=urllib3_proxy_kwargs)
elif d['proxy_url'].startswith('http'):
cred_string = ''
if 'username' in d:
cred_string += d['username']
if 'password' in d:
cred_string += ':' + d['password']
if len(cred_string) > 0:
domain = d['proxy_url'].split('/')[-1].split('@')[-1]
cred_string += '@'
proxy_url = 'http://{}{}'.format(cred_string, domain)
request = Request(proxy_url=proxy_url)
else:
request = Request(proxy_url=d['proxy_url'])
else:
raise Exception("Proxy URL should be in format PROTOCOL://PROXY_HOST[:PROXY_PORT].\n"
"HTTP and Socks5 are supported.")
else:
request = None

if 'token' in d and 'chat_id' in d:
bot = telegram.Bot(d['token'])
bot = telegram.Bot(d['token'], request=request)
obs = cls(bot, **d)
else:
raise ValueError("Telegram configuration file must contain "
Expand Down

0 comments on commit 20242f5

Please sign in to comment.