0

So, i use PyTelegramBotAPI to send messages to my channel with help of bot. Today i need to setup Proxy for my bot, i read docs https://github.com/eternnoir/pyTelegramBotAPI#proxy

Do like that:

import telebot
from telebot import apihelper

bot = telebot.TeleBot(config.telebot_token)
apihelper.proxy = {'https', 'socks5://login:pass@12.11.22.33:8000'}

And got exception:

Connected to pydev debugger (build 172.3757.67)
Traceback (most recent call last):
  File "/Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py", line 1596, in <module>
    globals = debugger.run(setup['file'], None, None, is_module)
  File "/Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py", line 1023, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "/Applications/PyCharm.app/Contents/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/Users/Stas/PycharmProjects/newsbot/parser.py", line 658, in <module>
    RssProcess('rsslinks.txt')
  File "/Users/Stas/PycharmProjects/newsbot/parser.py", line 511, in __init__
    telegrambot.bot.send_message(config.telebot_id, 'RssProcess Started...')
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/telebot/__init__.py", line 439, in send_message
    reply_markup, parse_mode, disable_notification))
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/telebot/apihelper.py", line 135, in send_message
    return _make_request(token, method_url, params=payload, method='post')
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/telebot/apihelper.py", line 54, in _make_request
    timeout=(connect_timeout, read_timeout), proxies=proxy)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/requests/sessions.py", line 499, in request
    prep.url, proxies, stream, verify, cert
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/requests/sessions.py", line 671, in merge_environment_settings
    no_proxy = proxies.get('no_proxy') if proxies is not None else None
AttributeError: 'set' object has no attribute 'get'

I try to read what is wrong and found such code:

if self.trust_env:
    # Set environment's proxies.
    no_proxy = proxies.get('no_proxy') if proxies is not None else None
    env_proxies = get_environ_proxies(url, no_proxy=no_proxy)
    for (k, v) in env_proxies.items():
        proxies.setdefault(k, v)

    # Look for requests environment configuration and be compatible
    # with cURL.
    if verify is True or verify is None:
        verify = (os.environ.get('REQUESTS_CA_BUNDLE') or
                  os.environ.get('CURL_CA_BUNDLE'))

Main problem in line

no_proxy = proxies.get('no_proxy')

I use PyCharm, with installed

PySocks 1.6.8 and Requests 2.18.4

How to connect through Proxy to telegram with my proxy? Also that proxy good work on my phone and PC.

4

3 回答 3

1

Same problem here. I need to use http and https too and this code works for me:

import telebot
from telebot import apihelper

bot = telebot.TeleBot(config.telebot_token)
apihelper.proxy = {
  'http', 'socks5://login:pass@12.11.22.33:8000',
  'https', 'socks5://login:pass@12.11.22.33:8000'
}
于 2018-04-16T15:03:14.490 回答
0

It seems the problem is in the dictionary you assign to proxy variable.

apihelper.proxy = {'https': 'socks5://login:pass@12.11.22.33:8000'}

Worked for me. Hope this helps.

于 2018-04-17T20:12:02.547 回答
-1
pip3 uninstall requests
pip3 uninstall requests==2.10.0
#socks5 Tor (no login, no pass)
apihelper.proxy = {'https':'socks5://127.0.0.1:9150'}
#or soks5
apihelper.proxy = {'https':'socks5://login:pass@orbtl.s5.opennetwork.cc:port'}

apihelper.proxy = {'https':'socks5://14611055481:U777Vluhz8@orbtl.s5.opennetwork.cc:999'}<br>

ok!

于 2019-07-14T14:36:26.483 回答