我启用了Webhook来处理传入的电报更新。我注意到,当我向电报机器人写入1条消息时,它会同时向Webhook发送几条消息。为什么会这样?
请说明其工作原理。也许我需要明确地返回一些东西来避免这种情况?现在,当您直接转到Web挂钩时,将返回状态为200的空白页面。
答案 0 :(得分:0)
问题是Content-Encoding
标头,或者更确切地说是压缩方法。服务器使用Brotli算法自动压缩响应,并返回Content-Encoding: br
标头。
我得出的结论是,Telegram服务器正在等待gzip压缩的响应。我没有机会在服务器上配置压缩算法,因此我不得不手动压缩响应:
function compress($data) {
$supportsGzip = strpos( $_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip' ) !== false;
if ( $supportsGzip ) {
$content = gzencode( trim( preg_replace( '/\s+/', ' ', $data ) ), 9);
header('Content-Encoding: gzip');
} else {
$content = $data;
}
$offset = 60 * 60;
$expire = "expires: " . gmdate("D, d M Y H:i:s", time() + $offset) . " GMT";
header("content-type: text/html; charset: UTF-8");
header("cache-control: must-revalidate");
header( $expire );
header( 'Content-Length: ' . strlen( $content ) );
header('Vary: Accept-Encoding');
echo $content;
}
compress(""); //Compress an empty answer in gzip
此更新之后,他们开始以一个副本的形式出现。
P.S。这是指text/html
格式。如果Webhook应该返回application/json
,那么一切都无需压缩即可工作。