通过代理进行身份验证的Python电报机器人

时间:2018-07-13 08:43:49

标签: python proxy telegram telegram-bot

所有人。

我正在尝试创建一个简单的电报机器人。在我的国家/地区,电报已被阻止,因此我需要通过代理进行工作。

<configuration>
  <appSettings>
    <add key="PhoneVersion" value="36.999.1" />
    <add key="TabletVersion" value="36.999.1" />
    <add key="DesktopVersion" value="36.999.1" />
  </appSettings>
</configuration>

但是我有下一条日志

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()

我使用了the next docs

免费代理可以正常工作,但就我的目标而言,一次又一次失去连接很不好。我也不喜欢在VPN下工作,因为我公司有些官僚作风。

谢谢!

4 个答案:

答案 0 :(得分:0)

我想您正在使用SOCKS5代理。在这种情况下,在您的示例中,代理url协议应为<style> .videoWrapper { position: relative; padding-bottom: 56.25%; } .videoWrapper .video-modal-poster img { position: absolute; width: 100%; height: 100%; top: 0; left: 0; z-index: 10; } .videoWrapper .video-modal-poster:hover img { z-index:0; } .videoWrapper iframe { position: absolute; top: 0; left: 0; width: 100%; height: 100%; z-index: 1; } </style> <div class="videoWrapper"> <a href="#" class="video-modal-poster"> <img alt="" src="" width="353" height="199" /> <iframe width="353" height="199" src="https://www.youtube.com/embed/" frameborder="0" allowfullscreen="allowfullscreen"></iframe> </a> </div> 而不是socks://

https://

答案 1 :(得分:0)

我刚刚遇到了同样的问题,并提出了以下建议。

1)确保我们从urllib3导入了所需的功能:

from urllib3 import make_headers

2)填写适当的值:

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

答案 2 :(得分:0)

url可能会有所帮助。它是 关于如何向您的机器人添加袜子或http代理

以http代理为例

SELECT  itemfk,
        dates.year,
        dates.week,
        isnull(transactionsperweek.TotalQty,0) as TotalQty
from    (
            SELECT  iit.ItemFK,
                    year(iit.transactiondate) xYear,
                    datepart(wk,iit.transactiondate) xWeek,
                    abs(sum(iit.quantity)) TotalQty
            from    iteminventorytransaction iit
            INNER JOIN ItemInventoryTransactionType iitt on ItemInventoryTransactionTypePK = iit.ItemInventoryTransactionTypeFK
            where   iit.itemfk = 5311
                    and iit.ItemInventoryTransactionTypeFK in (10,8)
                    and iit.TransactionDate BETWEEN
                    -- 1 year up to the sunday of last week 
                    DateAdd(wk,-51,DATEADD(day,-1 - (DATEPART(weekday, GETDATE()) + @@DATEFIRST - 2) % 7,GETDATE()))
                    AND DATEADD(day,-1 - (DATEPART(weekday, GETDATE()) + @@DATEFIRST - 2) % 7,GETDATE())
                    AND Quantity < 0
            group by iit.itemfk,
                    year(iit.transactiondate),
                    datepart(wk,iit.transactiondate)
        ) transactionsPerWeek
RIGHT JOIN  (
                select  year,
                        week
                from    DatesList
                where   date > DateAdd(wk,-51,DATEADD(day,-1 - (DATEPART(weekday, GETDATE()) + @@DATEFIRST - 2) % 7,GETDATE()))
                        AND date < DATEADD(day,-1 - (DATEPART(weekday, GETDATE()) + @@DATEFIRST - 2) % 7,GETDATE())
                group by year,
                        week
            ) Dates ON dates.week = transactionsPerWeek.xWeek
                    AND dates.year = transactionsPerWeek.xYear
where   week not in (52,53)

答案 3 :(得分:0)

我使用 tor 连接,问题得到解决。

  1. Download,安装并运行 Tor 浏览器。
  2. 安装 pysocks 依赖项。 pip install pysocks
  3. 在代码中:REQUEST_KWARGS = { 'proxy_url': 'socks5h://127.0.0.1:9150' }