当我想使用 Tor 连接到电报机器人时,会返回错误消息
requests.exceptions.ConnectTimeout:HTTPSConnectionPool(host ='api.telegram.org',port = 443):超过最大重试次数,网址:/bot/getUpdates?offset = 1&timeout = 20(由ConnectTimeoutError(
,'连接到api.telegram.org 超时。(连接超时=20)'))
您收到此错误是因为直接连接电报服务器存在问题!您可能需要使用 VPN 或更改您的 dns 提供商才能在 Ubuntu 中使用 tor 和 telegram bot,您必须执行以下操作:
首先使用 sudo apt install tor
安装
tor然后运行comcommsblow
sudo apt install privoxy torsocks
nano /etc/privoxy/config
然后
pip install pysocks
在包含 forward-socks5t / 127.0.0.1:9050 .
的非通信行之后
现在
sudo systemctl enable privoxy.service
sudo systemctl start privoxy.service
现在确保在
/etc/tor/torsocks.conf
文件中取消注释
TorAddress 127.0.0.1
TorPort 9050
重新启动系统
接下来在您的机器人源代码中添加代码
from telebot import apihelper
apihelper.proxy = {'https': 'socks5h://127.0.0.1:9050',
# 'http':'http://127.0.0.1:8118',
# 'https':'https://127.0.0.1:8118'
}
bot = telebot.TeleBot(TOKEN) # be sure telebot.TeleBot calls after apihelper.proxy
注意:如果无法连接,请自行强制停止并重新连接