【问题标题】:No pending_update_count from Telegram BotTelegram Bot 没有pending_update_count
【发布时间】:2020-06-10 00:14:54
【问题描述】:

我在 Telegram API 上苦苦挣扎了一段时间,似乎我在 stackoverflow 上没有发现我的问题,这就是我现在发布此消息的原因。

我尝试构建一个简单明了的 Telegram Bot。我使用 PHP 和 ureal4u 库来获取更新对象和 sendMessages https://github.com/unreal4u/telegram-api 我设置了一个带有链式证书的 webhook。这是我查看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
    }

}

在 Webhook.php 文件中我有这段代码

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);
?>

我通过两个不同的步骤测试了我的 webhook。

  • 第一个是使用Marvin's Marvelous Guide To All Things Webhook的 curl POST 向 webhook url 发送消息
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

  • 第二步是通过 Telegram 向我的 Bot 发送消息

在 getWebhookInfo 上,我仍然看到“pending_update_count”:0,而在 webhook.php 上,这是我得到的:

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

)

我想要什么:这两种方法中的任何一种 - curl POST 到 webhook url 或向电报机器人发送消息 - 向我的 url 发送一个更新对象,我可以在我的更新对象中的 webhook.php 中获取。

【问题讨论】:

    标签: php curl telegram-bot php-telegram-bot


    【解决方案1】:

    我是这个库的作者,但我对 StackOverflow 不太活跃。

    您看不到任何内容的原因是因为 Telegram 正在向您发送更新并且您在同一个请求中对其进行处理,因此您拥有的 print_r() 就是您要发送回 Telegram 的内容。

    在这种情况下,我建议将响应保存到文件或传入与 PSR-3 兼容的记录器(iirc 调试级别也将保存来自 Telegram 的原始数据),以便您了解正在发生的事情。

    编码愉快!

    【讨论】:

    • 感谢您的光临!我一定会尝试的。
    猜你喜欢
    • 2017-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-13
    • 2021-10-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多