Webhook没有处理python电报机器人

时间:2018-03-06 17:26:50

标签: python tcp telegram webhooks telegram-bot

拜托,有人帮我这个,我现在真的很难挣几个小时。不知道该怎么做,到处搜索每一页,仍然没有解决方案。

我有一个电报BOT,我正在从getUpdates()迁移到webhook。这里是我执行的代码(它可以工作,你会看到它):

library(dygraphs)

dyBarSeries <- function(dygraph, name, ...) {
  file <- "plotters/barseries.js" #you need to link to the downloaded file
  plotter_ <- paste0(readLines(file, skipNul = T), collapse = "\n")

  dots <- list(...)
  do.call('dySeries', c(list(dygraph = dygraph, name = name, plotter = 
plotter_), dots))

}

lungDeaths <- cbind(ldeaths, mdeaths)



dygraph(lungDeaths) %>% 
  dyBarSeries("ldeaths") %>%
  dySeries("mdeaths")

Webhook在DEBUG显示时开始:

BOT_TOKEN = 'xxxx' #no show
HOST     = 'y.y.y.y' #no show
PORT     = 443
CERT     = '/home/bot/scripts/python/cert.pem'
CERT_KEY = '/home/bot/scripts/python/private.key'

bot = telegram.bot.Bot(token=TOKEN)
updater = Updater(bot=bot)
updater.start_webhook( listen=HOST,
                       port=PORT, url_path=BOT_TOKEN,
                       key=CERT_KEY, cert=CERT,
                       webhook_url='https://%s:%s/%s'%(HOST,PORT,BOT_TOKEN)
                     )

updater.dispatcher.add_handler(CommandHandler('start', start))
updater.idle()

但是当我向我的机器人发送/ start命令时,没有任何东西到来。

然后,我打开了服务器上的443端口:

[root@rrpump python]# python3.6 bot_telegram.py
2018-03-06 16:04:04,243 - telegram.ext.dispatcher - DEBUG - Setting singleton dispatcher as <telegram.ext.dispatcher.Dispatcher object at 0x7fd490525ef0>
2018-03-06 16:04:04,243 - JobQueue - DEBUG - JobQueue thread started
2018-03-06 16:04:04,243 - telegram.ext.updater - DEBUG - dispatcher - started
2018-03-06 16:04:04,244 - telegram.ext.updater - DEBUG - updater - started
2018-03-06 16:04:04,244 - telegram.ext.updater - DEBUG - Updater thread started
2018-03-06 16:04:04,247 - telegram.ext.dispatcher - DEBUG - Dispatcher started
2018-03-06 16:04:04,263 - telegram.bot - DEBUG - Entering: set_webhook
2018-03-06 16:04:04,265 - telegram.vendor.ptb_urllib3.urllib3.connectionpool - DEBUG - Starting new HTTPS connection (1): api.telegram.org
2018-03-06 16:04:04,704 - telegram.vendor.ptb_urllib3.urllib3.connectionpool - DEBUG - https://api.telegram.org:443 "POST /botxxxx/setWebhook HTTP/1.1" 200 57
2018-03-06 16:04:04,706 - telegram.bot - DEBUG - True
2018-03-06 16:04:04,706 - telegram.bot - DEBUG - Exiting: set_webhook
2018-03-06 16:04:04,706 - telegram.utils.webhookhandler - DEBUG - Webhook Server started.

并验证我的机器人脚本是否正在侦听该端口:

iptables -A INPUT -p tcp -m tcp --dport 443 -j ACCEPT

是的,确实如此。但是,我的机器人没有通过电报响应/启动命令。

然后,我尝试了POSTMAN并模拟了直接发送到我的webhook的/ start命令,就像解释here一样:

postman /start command sent

没有成功。 webhook仍然没有收到任何东西。直到现在还清楚:api.telegram.org服务器不是问题。当我执行updater.bot.getWebhookInfo()时,我可以看到有14个挂起的更新,我通过电报发送到我的机器人的所有14'/启动':

[root@rrpump bot]# lsof -i | grep python3.6
python3.6 30598     root    7u  IPv4 2781012079      0t0  TCP rrpump.com:https (LISTEN)
python3.6 30598     root    9u  IPv6 2781012225      0t0  TCP rrpump.com:43682->[2001:67c:4e8:f004::9]:https (ESTABLISHED)

然后我尝试直接从我的服务器内部向我的webhook发送带有'/ start'的消息,就像here一样:

print(updater.bot.getWebhookInfo().to_dict())
{'has_custom_certificate': True,
 'last_error_date': 1520355514,
 'last_error_message': 'Connection timed out',
 'max_connections': 40,
 'pending_update_count': 14,
 'url': 'https://y.y.y.y:443/xxxx'}

触发了webhook:

[root@rrpump bot]# curl -v -k -X POST -H "Content-Type: application/json" -H "Cache-Control: no-cache"  -d '{
> "update_id":10000,
> "message":{
>   "date":1441645532,
>   "chat":{
>      "last_name":"Test Lastname",
>      "id":1111111,
>      "type": "private",
>      "first_name":"Test Firstname",
>      "username":"Testusername"
>   },
>   "message_id":1365,
>   "from":{
>      "last_name":"Test Lastname",
>      "id":1111111,
>      "first_name":"Test Firstname",
>      "username":"Testusername"
>   },
>   "text":"/start"
> }
> }' "https://y.y.y.y/xxxx"

* About to connect() to y.y.y.y port 443 (#0)
*   Trying y.y.y.y...
* Connected to y.y.y.y (y.y.y.y) port 443 (#0)
* Initializing NSS with certpath: sql:/etc/pki/nssdb
* skipping SSL peer certificate verification
* SSL connection using TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
* Server certificate:
*       subject: CN=y.y.y.y 1,O=Example Brooklyn Company,L=Brooklyn,ST=New York,C=US
*       start date: Mar 06 13:54:03 2018 GMT
*       expire date: Mar 06 13:54:03 2019 GMT
*       common name: y.y.y.y
*       issuer: CN=y.y.y.y,O=Example Brooklyn Company,L=Brooklyn,ST=New York,C=US
> POST /xxxx HTTP/1.1
> User-Agent: curl/7.29.0
> Host: y.y.y.y
> Accept: */*
> Content-Type: application/json
> Cache-Control: no-cache
> Content-Length: 392
>
* upload completely sent off: 392 out of 392 bytes
* HTTP 1.0, assume close after body
< HTTP/1.0 200 OK
< Server: WebhookHandler/1.0 Python/3.6.4
< Date: Tue, 06 Mar 2018 16:52:55 GMT
<
* Closing connection 0

*现在..问题是:我的服务器怎么可能只为自己侦听端口443?为什么它不像我在命令lsof -i |中看到的那样监听外部地址python3.6?

此命令还显示:

2018-03-06 16:52:55,844 - telegram.utils.webhookhandler - DEBUG - Webhook triggered
2018-03-06 16:52:55,844 - telegram.utils.webhookhandler - DEBUG - 93.188.166.161 - - "POST /415365952:AAFMeP6TVyhey4D1MNAaqhhiH0lh4EUvLVE HTTP/1.1" 200 -
2018-03-06 16:52:55,844 - telegram.utils.webhookhandler - DEBUG - Webhook received data: {
"update_id":10000,
"message":{
  "date":1441645532,
  "chat":{
     "last_name":"Test Lastname",
     "id":1111111,
     "type": "private",
     "first_name":"Test Firstname",
     "username":"Testusername"
  },
  "message_id":1365,
  "from":{
     "last_name":"Test Lastname",
     "id":1111111,
     "first_name":"Test Firstname",
     "username":"Testusername"
  },
  "text":"/start"
}
}

1 个答案:

答案 0 :(得分:0)

对我有用:

updater = Updater("xxxx")    
updater.start_webhook(
    listen='0.0.0.0',
    port=8443,
    url_path='xxxx',
    key='private.key',
    cert='cert.pem',
    webhook_url='https://y.y.y.y:8443/xxxx'
)
updater.idle()

其中y.y.y.y是运行bot代码的服务器的外部IP,xxxx是令牌。 我必须在路由器中端口转发8443端口,因为上述服务器已连接到路由器并且启用了NAT。