'Python telegram bot via proxy with authentification

everyone.

I am trying to create a simple telegram bot. In my country telegram is blocked so I need to work via proxy.

from telegram.ext import Updater         
from telegram.ext import CommandHandler  

import os

def start(bot, update):
    print("Hello")
    bot.sendMessage(chat_id=512562849, text="Hello.")

REQUEST_KWARGS={
    'proxy_url': 'http://93.171.217.48:10996',
    'urllib3_proxy_kwargs': {
    'username': '***',
    'password': '***',
    }
}


updater = Updater(token='<BOT_TOKEN>',
                  request_kwargs=REQUEST_KWARGS)  


start_handler = CommandHandler('start', start)  

updater.dispatcher.add_handler(start_handler)   
updater.start_polling()

But I have the next log

Exception in thread updater:
...
    _HTTPConnection.__init__(self, *args, **kw)
TypeError: __init__() got an unexpected keyword argument 'username'

I used the next docs.

The free proxy works ok, but for my goals, it's bad to lose connection time after time. I prefer not to work under VPN either because of some bureaucracy in my company.

Thanks in advance!



Solution 1:[1]

I suppose you're using a SOCKS5 proxy. If that's the case, the proxy url protocol should be socks:// instead of https://, in your example:

REQUEST_KWARGS={
    'proxy_url': 'socks5://93.171.217.48:10996',
    'urllib3_proxy_kwargs': {
    'username': '***',
    'password': '***',
}

Solution 2:[2]

I've just ran the same issue and came up with the following.

1) Make sure we import required function from urllib3:

from urllib3 import make_headers

2) Fill appropriate values:

REQUEST_KWARGS = {
    'proxy_url': "http://ip:port",
    'urllib3_proxy_kwargs': {
        'proxy_headers': make_headers(proxy_basic_auth='username:password')
    }
}

Solution 3:[3]

I used tor to connect and my problem was fixed.

  1. Download, install and run the tor browser.
  2. Install pysocks dependency. pip install pysocks
  3. And in the code: REQUEST_KWARGS = { 'proxy_url': 'socks5h://127.0.0.1:9150' }

Solution 4:[4]

I was behind corporate https proxy with auth and I solved with Working Behind a Proxy which basically proposes this:

TOKEN='YOUR_BOT_TOKEN'
REQUEST_KWARGS={
    # "USERNAME:PASSWORD@" is optional, if you need authentication:
    'proxy_url': 'http://USERNAME:PASSWORD@PROXY_HOST:PROXY_PORT/',
}
updater = Updater(TOKEN, request_kwargs=REQUEST_KWARGS)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Eltha
Solution 2 turikhay
Solution 3 S F
Solution 4 Henry Ecker