Reputation: 21
I am struggling with Telegram API for a while now, it seems that I don't find my problem on stackoverflow which is why I am posting this message right now.
I try to build a Telegram Bot plain and simple. I use PHP and ureal4u library to get Update objects and sendMessages https://github.com/unreal4u/telegram-api I set a webhook with a chained certificate. This is what I get when I check https://api.telegram.org/botBOTTOKEN/getWebhookInfo
{
"ok": true,
"result": {
"url": https://mydomainname/my/path/webhook.php,
"has_custom_certificate": true,
"pending_update_count": 0,
"max_connections": 40
}
}
In Webhook.php file I have this code
webhook.php
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
require 'vendor/autoload.php';
use \unreal4u\TelegramAPI\Telegram\Types\Update;
// Getting POST request body and decoding it from JSON to associative array
$updateData = json_decode(file_get_contents('php://input'), true);
$update = new Update($updateData);
print_r($update);
?>
I tested my webhook throught two different steps.
curl --tlsv1.2 -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,
"first_name":"Test",
"username":"Test"
},
"message_id":1365,
"from":{
"last_name":"Test Lastname",
"id":1111111,
"first_name":"Test",
"username":"Test"
},
"text":"/start"
}
}' "https://mydomainname/my/path.webhook.php"
RESPONSE :
Note: Unnecessary use of -X or --request, POST is already inferred.
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0* Trying 54.36.91.62:443...
* TCP_NODELAY set
* Connected to mydomainname (IP ADDRESS) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* successfully set certificate verify locations:
* CAfile: path/to/cert/ca-bundle.crt
CApath: none
} [5 bytes data]
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
} [512 bytes data]
* TLSv1.3 (IN), TLS handshake, Server hello (2):
{ [102 bytes data]
* TLSv1.2 (IN), TLS handshake, Certificate (11):
{ [2563 bytes data]
* TLSv1.2 (IN), TLS handshake, Server key exchange (12):
{ [333 bytes data]
* TLSv1.2 (IN), TLS handshake, Server finished (14):
{ [4 bytes data]
* TLSv1.2 (OUT), TLS handshake, Client key exchange (16):
} [70 bytes data]
* TLSv1.2 (OUT), TLS change cipher, Change cipher spec (1):
} [1 bytes data]
* TLSv1.2 (OUT), TLS handshake, Finished (20):
} [16 bytes data]
* TLSv1.2 (IN), TLS handshake, Finished (20):
{ [16 bytes data]
* SSL connection using TLSv1.2 / ECDHE-RSA-AES128-GCM-SHA256
* ALPN, server accepted to use h2
* Server certificate:
* subject: CN=mydomainname
* start date: Feb 14 20:49:32 2020 GMT
* expire date: May 14 20:49:32 2020 GMT
* issuer: C=US; O=Let's Encrypt; CN=Let's Encrypt Authority X3
* SSL certificate verify ok.
* Using HTTP2, server supports multi-use
* Connection state changed (HTTP/2 confirmed)
* Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0
} [5 bytes data]
* Using Stream ID: 1 (easy handle 0xf11300)
} [5 bytes data]
> POST /my/path/webhook.php HTTP/2
> Host: mydomainname
> user-agent: curl/7.67.0
> accept: */*
> content-type: application/json
> cache-control: no-cache
> content-length: 332
>
{ [5 bytes data]
* Connection state changed (MAX_CONCURRENT_STREAMS == 128)!
} [5 bytes data]
* We are completely uploaded and fine
{ [5 bytes data]
< HTTP/2 200
< date: Wed, 26 Feb 2020 06:16:52 GMT
< content-type: text/html; charset=UTF-8
< server: Apache
< x-powered-by: PHP/7.2
< vary: Accept-Encoding
< x-iplb-instance: 32871
< set-cookie: SERVERID87219=2720142|XlYNV|XlYNV; path=/
<
{ [332 bytes data]
100 664 0 332 100 332 2184 2184 --:--:-- --:--:-- --:--:-- 4397{
"update_id":10000,
"message":{
"date":1441645532,
"chat":{
"last_name":"Test Lastname",
"id":1111111,
"first_name":"Test",
"username":"Test"
},
"message_id":1365,
"from":{
"last_name":"Test Lastname",
"id":1111111,
"first_name":"Test",
"username":"Test"
},
"text":"/start"
}
}
* Connection #0 to host mydomainname left intact
On getWebhookInfo I still see "pending_update_count": 0, and on webhook.php this is what I get :
webhook.php
unreal4u\TelegramAPI\Telegram\Types\Update Object
(
[update_id] => 0
[message] =>
[edited_message] =>
[channel_post] =>
[edited_channel_post] =>
[inline_query] =>
[chosen_inline_result] =>
[callback_query] =>
[shipping_query] =>
[pre_checkout_query] =>
[poll] =>
[logger:protected] => unreal4u\Dummy\Logger Object
(
)
)
What I would like : That either one of these two methods - curl POST to webhook url or sending a message to telegram bot - sends an update object to my url that I can get in webhook.php in my Update Object.
Upvotes: 2
Views: 894
Reputation: 33
I am the author of this library but I'm not too active on StackOverflow.
The reason why you don't see anything is because Telegram is sending you the update and you process it in the same request, so that print_r() you've got is what you are sending back to Telegram.
I would recommend in this situation to save the response to a file or pass in a PSR-3 compatible logger (iirc debug level will also save the original data that came in from Telegram) so that you can see what is happening.
Happy coding!
Upvotes: 1