Created
July 21, 2020 23:32
-
-
Save luizmarcus/804220a9ba4297c82661dc478bd0a113 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 | |
define('BOT_TOKEN', '000000000:0000000000000000000000000000'); | |
define('API_URL', 'https://api.telegram.org/bot'.BOT_TOKEN.'/'); | |
function processMessage($message) { | |
// processa a mensagem recebida | |
$message_id = $message['message_id']; | |
$chat_id = $message['chat']['id']; | |
if (isset($message['text'])) { | |
$text = $message['text'];//texto recebido na mensagem | |
if (strpos($text, "/start") === 0) { | |
//envia a mensagem ao usuário | |
sendMessage("sendMessage", array('chat_id' => $chat_id, "text" => 'Olá, '. $message['from']['first_name']. | |
'! Bem-vindo à nossa pesquisa de TCC. Se gostou de nossa apresentação, clique abaixo em "Gostei". Caso não tenha gostado, clique em "Não Gostei".','reply_markup' => array('inline_keyboard' => array( | |
array( | |
array('text'=>'Gostei','callback_data'=>'gostei','show_alert'=>'true'), //botão 1 | |
array('text'=>'Não Gostei','callback_data'=>'nao-gostei','show_alert'=>'true')//botão 2 | |
) | |
) | |
))); | |
} else { | |
sendMessage("sendMessage", array('chat_id' => $chat_id, "text" => 'Desculpe, mas sou um robô e ainda não estou programado para conversar')); | |
} | |
} else { | |
sendMessage("sendMessage", array('chat_id' => $chat_id, "text" => 'Desculpe, mas sou um robô e ainda não fui programado para conversar')); | |
} | |
} | |
function sendMessage($method, $parameters) { | |
$options = array( | |
'http' => array( | |
'method' => 'POST', | |
'content' => json_encode($parameters), | |
'header'=> "Content-Type: application/json\r\n" . | |
"Accept: application/json\r\n" | |
) | |
); | |
$context = stream_context_create( $options ); | |
file_get_contents(API_URL.$method, false, $context ); | |
} | |
$update_response = file_get_contents("php://input"); | |
$update = json_decode($update_response, true); | |
if (isset($update["message"])) { | |
processMessage($update["message"]); | |
}else if (isset($update["callback_query"])) { //Nessa linha você checa se é uma resposta ao clique dos botões 1 e 2 | |
/* | |
Botões 1 e 2. Quando você obtem o callback_query (https://core.telegram.org/bots/api#callbackquery), | |
você precisa responder com um answercallbackquery (https://core.telegram.org/bots/api#answercallbackquery). | |
Isso serve para o telegram entender que a soliticação do usuário foi recebida e respondida. | |
*/ | |
sendMessage("answerCallbackQuery", array('callback_query_id' => $update["callback_query"]["id"], 'text' => '')); | |
/* | |
Aqui nessa segunda linha seu bot responde de acordo com o que o usuário enviou através do clique no botão. O dado | |
ficar armazenado no parâmetro 'data' do objeto 'callback_query'. | |
*/ | |
if (($update["callback_query"]["data"])=="gostei"){ | |
sendMessage("sendMessage", array('chat_id' => $update["callback_query"]["from"]["id"], "text" => 'Ficamos felizes com o seu voto')); | |
$conteudo = $update["callback_query"]["from"]["id"]; | |
$file = 'gostei.txt'; | |
$fh = fopen($file, "w+"); | |
fwrite($fh, $conteudo); | |
fclose($fh); | |
}else { | |
sendMessage("sendMessage", array('chat_id' => $update["callback_query"]["from"]["id"], "text" => 'Que pena. Agradecemos sua participação')); | |
$conteudo = $update["callback_query"]["from"]["id"]; | |
$file = 'nao_gostei.txt'; | |
$fh = fopen($file, "w+"); | |
fwrite($fh, $conteudo); | |
fclose($fh); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment