Reputation: 477
Please, someone help me with this, I'am really struggling for hours by now. Don't know what to do anymore, searched every page everywhere, still no solution.
I have a telegram BOT which I'am migrating from getUpdates() to a webhook. Here goes the code I'am executing (it works, you will see ahead):
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()
The Webhook starts as the DEBUG shows:
[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.
But when I send a /start command to my bot, nothing arrives.
Then, I openned the 443 port on my server:
iptables -A INPUT -p tcp -m tcp --dport 443 -j ACCEPT
And verified if my bot script was listening to that port:
[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)
Yes, it was. But still, my bot was not responding to /start commands through telegram.
Then, I tryied POSTMAN and simulated a /start command sent directly to my webhook just like explained here:
No success. The webhook still don't receive anything. Something is clear until now: It is not a problem with api.telegram.org servers. When I do a updater.bot.getWebhookInfo(), I can see there are 14 pending updates, all my 14 '/starts' I sent to my bot through telegram:
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'}
Then I tried to send a message with '/start' to my webhook directly from inside my server to itself like here:
[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
And the webhook was triggered:
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"
}
}
*Now.. the question is: how could my server be listening on port 443 only for itself? Why isn't it listening to external addresses just like I saw in the command lsof -i | python3.6?
This command also shows that:
[root@rrpump bot]# lsof -i:443
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
python3.6 30644 root 8u IPv4 2785755157 0t0 TCP y.y.y.y:https (LISTEN)
Upvotes: 1
Views: 3340
Reputation: 171
That worked for me:
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()
where y.y.y.y is external IP of server running the bot code and xxxx is the token. I had to port forward 8443 port in my router, because the mentioned server is connected to the router and NAT is enabled.
Upvotes: 0