Skip to content

Instantly share code, notes, and snippets.

@salvatorecordiano
Last active March 10, 2023 10:27
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save salvatorecordiano/5db0dc73baf7e6750305068753212120 to your computer and use it in GitHub Desktop.
Save salvatorecordiano/5db0dc73baf7e6750305068753212120 to your computer and use it in GitHub Desktop.
Sample Telegram Bot (3)
<?php
// recupero il contenuto inviato da Telegram
$content = \file_get_contents('php://input');
// converto il contenuto da JSON ad array PHP
$update = \json_decode($content, true);
// se la richiesta è null interrompo lo script
if(!$update) {
exit;
}
// assegno alle seguenti variabili il contenuto ricevuto da Telegram
$message = isset($update['message']) ? $update['message'] : null;
$messageId = isset($message['message_id']) ? $message['message_id'] : null;
$chatId = isset($message['chat']['id']) ? $message['chat']['id'] : null;
$firstname = isset($message['chat']['first_name']) ? $message['chat']['first_name'] : null;
$lastname = isset($message['chat']['last_name']) ? $message['chat']['last_name'] : null;
$username = isset($message['chat']['username']) ? $message['chat']['username'] : null;
$date = isset($message['date']) ? $message['date'] : null;
$text = isset($message['text']) ? $message['text'] : null;
// mi preparo a restitutire al chiamante la mia risposta che è un oggetto JSON
// imposto l'header della risposta
header('Content-Type: application/json');
// la mia risposta è un array JSON composto da chat_id, text, method
// chat_id mi consente di rispondere allo specifico utente che ha scritto al bot
// text è il testo della risposta
$parameters = array('chat_id' => $chatId, 'text' => $text, 'method' => 'sendMessage');
// converto e stampo l'array JSON sulla response
echo \json_encode($parameters);
@ilvikingo
Copy link

Ciao ragazzi, mi dareste una mano per favore? Ho creato un bot su Telegram e vorrei farlo interagire con una Dashboard che fornisce statistiche , ho le api di questa Dashboard ma come faccio a farli interagire?

@endriuu
Copy link

endriuu commented Mar 10, 2023

Ciao @salvatorecordiano,
mi potresti aiutare nel capire come posso fare per recuperare l'id dell'ultimo messaggio appena inviato dal bot?

Caso pratico:

  • il bot invia un sondaggio ad una chat (su questo ci sono riuscito a fare con il seguente codice)
    `

      $options =  array("Devendra","noneofthese","Sourabh") ;
      $data = [      'chat_id' => '$chatId',   'question' => 'This is whose number 12345 ?',  'options' => $options ];
    
      header('Content-Type: application/json');
      // la mia risposta è un array JSON composto da chat_id, text, method
      // chat_id mi consente di rispondere allo specifico utente che ha scritto al bot
      // text è il testo della risposta
      //$parameters = array('chat_id' => $chatId, 'text' => $messageBack, 'method' => 'sendMessage');
      $parameters = array('chat_id' => $chatId, "question" => "This is whose number 12345 ?",  "options" => $options, "is_anonymous" => "False", "parse_mode" => "markdown");
      $parameters["method"] = "sendPoll";
      echo json_encode($parameters); `
    
  • a questo punto mi servirebbe recuperare l'ID di questo poll, per poterlo poi successivamente recuperare e terminarlo/collezionare le risposte date

Non ne vengo fuori per capire come fare a recuperare l'ID del poll. Mi puoi aiutare?

Grazie mille!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment