F
F
frony2018-04-20 11:27:58
Python
frony, 2018-04-20 11:27:58

How to run a telegram bot from behind a proxy?

I teach at MSTU. Bauman and my students write bots on laboratory telegrams. Due to the blocking of the telegram, we have problems. We use the PyCharm environment and the pyTelegramBotAPI library. I'm trying to start a bot from behind a proxy like so:

import telebot
from telebot import apihelper

apihelper.proxy = {'http':'http://x.x.x.x:port'}

TOKEN = ''
bot = telebot.TeleBot(TOKEN)

@bot.message_handler(commands=['start'])

def start(m):
msg = bot.send_message(m.chat.id, 'Привет!' )

bot.polling()

Answer:
ERROR - TeleBot: "ConnectTimeout occurred, args=(MaxRetryError("HTTPSConnectionPool(host='api.telegram.org', port=443): Max retries exceeded with url: /bot547971947:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/getUpdates?offset=1&timeout=20 (Caused by ConnectTimeoutError(<urllib3.connection.VerifiedHTTPSConnection object at 0x02A022F0>, 'Connection to api.telegram.org timed out. (connect timeout=3.5)'))",),)

The proxy is working, the browser accesses api.telegram.org through the proxy.
Tried Sock5 as well, same thing.
In principle, it doesn’t matter to me how to solve this problem, the main thing is that students can run bots. There is no access to the lab room

Answer the question

In order to leave comments, you need to log in

5 answer(s)
D
Danil K., 2019-01-10
@frony

https://github.com/eternnoir/pyTelegramBotAPI#proxy
documentation:
but if it doesn't work, try:
Found here

A
Arseniy Oguzov, 2018-04-22
@psy667

Install the latest gunicorn, PySocks, pyTelegramBotAPI, requests and urllib3 libraries
Then try another proxy like this one

ip = '195.201.137.246'
port = '1080'

apihelper.proxy = {
  'https': 'socks5://{}:{}'.format(ip,port)
}

L
lll000lll, 2018-04-23
@lll000lll

I have already spent a lot of time trying to figure out a similar problem. Faced on the ninth Debian, either updated to the latest versions.
In order to radically exclude the version with old libs (you never know, this is Debian, the lib versions are behind), I checked it on a freshly installed Ubuntu server, version 17, specially for this purpose.
Attempts to find the answer to the question in Google, or the corresponding telegram groups, did not give anything.
As a result, I switched to this bot https://github.com/python-telegram-bot/python-tele...
Here's how to configure it to work through a proxy https://github.com/python-telegram-bot/python-tele ...
And if the proxy is with authorization, only the first method worked for me.
By the way, this bot is more popular than telebot in terms of the number of stars, although the differences between them are not entirely clear to me. Telebot uses decorators, the code is more concise and more beautiful.

V
vladqwelkj, 2018-06-26
@vladqwelkj

Sorry for offtopic, I solved the problem by switching to another library - "telepot"

import telepot

basic_auth = ('username', 'password')
SetProxy = telepot.api.set_proxy("http://10.10.10.10:1080", basic_auth)
bot = telepot.Bot('11111111:xxxxxxxxxxxxxxxxxxxxxxxxxxxx')

print(bot.getMe())

D
DingoBingo, 2020-05-27
@DingoBingo

There was the same error, I downloaded the application on the WindScribe PC, tried it from Western Canada, everything works. https://windscribe.com/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question