Last active
April 7, 2020 19:43
-
-
Save salvatorecordiano/8fbe96008b9a344c259ee73b11e370a3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'] : ""; | |
$messageId = isset($message['message_id']) ? $message['message_id'] : ""; | |
$chatId = isset($message['chat']['id']) ? $message['chat']['id'] : ""; | |
$firstname = isset($message['chat']['first_name']) ? $message['chat']['first_name'] : ""; | |
$lastname = isset($message['chat']['last_name']) ? $message['chat']['last_name'] : ""; | |
$username = isset($message['chat']['username']) ? $message['chat']['username'] : ""; | |
$date = isset($message['date']) ? $message['date'] : ""; | |
$text = isset($message['text']) ? $message['text'] : ""; | |
// pulisco il messaggio ricevuto togliendo eventuali spazi prima e dopo il testo | |
$text = trim($text); | |
// converto tutti i caratteri alfanumerici del messaggio in minuscolo | |
$text = strtolower($text); | |
// 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 è il metodo per l'invio di un messaggio (cfr. API di Telegram) | |
$parameters["method"] = "sendMessage"; | |
// imposto la inline keyboard | |
$keyboard = ['inline_keyboard' => [[['text' => 'myText', 'callback_data' => 'myCallbackText']]]]; | |
$parameters["reply_markup"] = json_encode($keyboard, true); | |
// converto e stampo l'array JSON sulla response | |
echo json_encode($parameters); |
Stesso problema, risolto?
// imposto la inline keyboard
$keyboard = ['inline_keyboard' => [[['text' => 'myText', 'callback_data' => 'myCallbackText']]]];
$parameters["reply_markup"] = json_encode($keyboard, true);
// converto e stampo l'array JSON sulla response
echo json_encode($parameters);
Ciao @salvatorecordiano!
Grazie per il tuo aiuto, mi sono serviti molto questi tuoi esempi!
Ho provato a realizzare la inlineKeyboard ed il bottone appare, solo che non vuole sapere di scomparire..ed inoltre non ho capito come si usino le CallBackQuery..sarei grato se tu mi potessi illuminare al riguardo.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ciao e grazie per gli ottimi esempi.
Ho un problema/necessità che vorrei risolvere se possibile.
Ho necessità, a seguito di un controllo "if",di inviare due messaggi distinti, con un codice identico all'esempio seguente, ma viene inviato solo il primo messaggio! Dove sbaglio?
Grazie ancora