title: PHP Telegram Bot sends same message many times
body: <p>I have a Telegram Bot which worked fine for a few months. About 3-4 weeks ago the Telegram Bot sends sometimes (!) the same messages a few times - not just one time! </p>
  
  <p>I have already tried many things to fix that:</p>
  
  <ol>
  <li><p>I save the update_id in the database. If the new update_id is higher than the saved one, the bot sends the message. If not, an error occurs. (Found out yesterday that every duplicate message has got a new (higher) update_id - so this method is useless)</p></li>
  <li><p>I tried to add exit(); to every PHP Function - didn't work.</p></li>
  <li><p>I checked the whole code, if there is a not closed loop. But everything is fine.</p></li>
  <li><p>I added a ?limit=1 to the function, but also no chance.</p></li>
  </ol>
  
  <p>Slowly I am at the end of my ideas. Maybe someone has got a good answer for that.</p>
  
  <p>I have always the $update and the chat ID:</p>
  
  <pre><code>define('api', 'https://api.telegram.org/bot'.token.'/');
  
  $data = file_get_contents("php://input");
  $update = json_decode($data, true);
  $cbid = $update["callback_query"]["from"]["id"];
  </code></pre>
  
  <p>My function looks like this:</p>
  
  <pre><code>function callback($up){
      return $up["callback_query"];
  }
  
  function tg_send_message($id, $text) {
  
      $params=[
          'chat_id' =&gt; $id,
          'text' =&gt; $text,
          'parse_mode' =&gt; 'Markdown',
      ];
  
      $ch = curl_init('https://api.telegram.org/bot'.token.'/sendMessage?limit=1');
      curl_setopt($ch, CURLOPT_HEADER, false);
      curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, 3500);
      curl_setopt($ch, CURLOPT_TIMEOUT_MS, 3500);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch, CURLOPT_POST, 1);
      curl_setopt($ch, CURLOPT_POSTFIELDS, ($params));
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
      $result = curl_exec($ch);
      curl_close($ch);
  
  }
  </code></pre>
  
  <p>If I want to send a message if someone clicks on the inline button Top 10:</p>
  
  <pre><code>if(callback($update) and $cbdata == "top_10"){
      tg_send_message($cbid, "This is my message!");  
  }
  </code></pre>
  
  <p>Everything worked fine and is working now as well - the bot just sends sometime a message multiple times.</p>
  
  <p>The extra messages have always a new update_id! And if there is a variable value from another API (like Bitcoin price), it is also changing. It's like you clicked 22 times on the button (but you just clicked once)</p>
  
  <p>I am very thankful for every helpful answer! Thank you very much.</p>
  
  <p>Best regards.</p>
  
  <p>EDIT: I have also tried the following:</p>
  
  <pre><code>function tg_btn_click_send_message($id, $text) {
  
      $response = $update["callback_query"];
      $botUrl = "https://api.telegram.org/bot" . BOT_TOKEN . "/answerCallbackQuery";
      $postFields = array('callback_query_id' =&gt;  $callback_query_id, 'text' =&gt; $response);
      $ch = curl_init(); 
      curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:multipart/form-data"));
      curl_setopt($ch, CURLOPT_URL, $botUrl); 
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
      curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
      $output = curl_exec($ch);
  
      //send Text
      header("Content-Type: application/json");
      $parameters = array('chat_id' =&gt; $id, "text" =&gt; $text);
      $parameters["method"] = "sendMessage";
      echo json_encode($parameters);
  
  }
  </code></pre>
  
  <p>Like suggested here: <a href="https://stackoverflow.com/questions/44142978/php-telegram-answercallbackquery-sendmessage">php telegram answercallbackquery sendmessage</a></p>
  
  <p>But also not working.</p>