1

I enabled webhook to handle incoming telegram updates. I noticed that when I write 1 message to the telegram bot, it sends several messages to the webhook at the same time. Why is this happening?

Please explain how this works. Maybe I need to explicitly return something to avoid this? Now, when you go directly to the web hook, a blank page with a status of 200 is returned.

Aggravator
  • 119
  • 4
  • You should return a status 200 response for telegram to expire those updates and not send them anymore. If Telegram doesn't receive a 200 status code it will think your bot is not responding and will send the requests until it does. – Ali Padida Mar 25 '20 at 21:01

1 Answers1

0

The problem was the Content-Encoding header, or rather, the compression method. The server automatically compressed the response using the Brotli algorithm and returned the Content-Encoding: br header.

I came to the conclusion that the Telegram server is waiting for a response with gzip compression. I did not have the opportunity to configure the compression algorithm on the server, so I had to compress the response manually:

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

After this update, they began to come in a single copy.

P.S. This refers to the text/html format. If a webhook should return application/json, then everything works without compression.

Aggravator
  • 119
  • 4