Skip to content
Browse files

Merge pull request #374 from brickerino/master

Add proxy support to Telegram observer.
  • Loading branch information...
Qwlouse committed Nov 2, 2018
2 parents b738fa6 + b2d8165 commit 94785b9592b16e8498212ad9cfa0e05d49a1819d
Showing with 36 additions and 1 deletion.
  1. +6 −0 docs/observers.rst
  2. +30 −1 sacred/observers/
@@ -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:

@@ -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)
request = Request(proxy_url=d['proxy_url'])
raise Exception("Proxy URL should be in format PROTOCOL://PROXY_HOST[:PROXY_PORT].\n"
"HTTP and Socks5 are supported.")
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)
raise ValueError("Telegram configuration file must contain "

0 comments on commit 94785b9

Please sign in to comment.
You can’t perform that action at this time.