\n\n" . $response . "
\n\nVARDUMP:\n
";
var_dump($response);
return;
}
//if(empty($updates['result']))
//return;
//Itera pelos Updates
foreach ($updates['result'] as &$update) {
$offset = $update['update_id'];
$msg = $update['message'];
//texto: $msg['text']
//datahora em Epoch: $msg['date']
//autor: $msg['from']['id'] | $msg['from']['first_name'] | $msg['from']['username']
//chat com pessoa: $msg['chat']['id'] | $msg['chat']['first_name'] | $msg['chat']['username']
//chat com grupo: $msg['chat']['id'] | $msg['chat']['title']
//aviso de novo usuario no chat: $msg['new_chat_participant']['first_name'] | $msg['new_chat_participant']['username'] (usar em conjunto com $msg['from'][...] pra saber quem convidou)
//forward: $msg['text'] $msg['forward_from']['id'] $msg['forward_from']['username'] $msg['forward_date']
//Teste simples de "/echo BLABLABLA"
if(isset($msg['text']) ) {
if(startsWith($msg['text'], '/echo ')) {
$echo = str_replace("/echo ", "", $msg['text']);
$chat_id = $msg['chat']['id'];
sendMessage($chat_id, $echo);
}
//Comando /PLAYERS
if(startsWith($msg['text'], '/players')) {
//$echo = str_replace("/echo ", "", $msg['text']);
$rcon = new Rcon($host, $port, $password, $timeout);
if ($rcon->connect())
{
$texto = $rcon->send_command("list");
$chat_id = $msg['chat']['id'];
sendMessage($chat_id, $texto);
}
}
//Comandos RCON diretos
if(startsWith($msg['text'], '/rcon ')) {
$from = $msg['from']['username'];
$chat_id = $msg['chat']['id'];
if (issudo($from, $chat_id)) {
$comando = str_replace("/rcon ", "", $msg['text']);
$rcon = new Rcon($host, $port, $password, $timeout);
if ($rcon->connect())
{
$texto = $rcon->send_command($comando);
sendMessage($chat_id, $texto);
}
}
}
//envia pro minecraft
if(!empty($msg['text'] && startsWith($msg['text'], '>'))) {
//$chat_id = $msg['chat']['id'];
//sendMessage($chat_id, $echo);
$mensagem = "\xc2\xa71" . $msg['from']['first_name'] . ": \xc2\xa7r" . $msg['text'];
$rcon = new Rcon($host, $port, $password, $timeout);
if ($rcon->connect())
{
$rcon->send_command("say " . $mensagem);
//echo $rcon->send_command("list");
}
}
}
$chat_id = $msg['chat']['id'];
if(!chat_liberado($chat_id)) {
//O SILÊNCIO É A MELHOR RESPOSTA
}
else {
}
}
//Salva o offset do ultimo update
setStoredValue($offset, "offset_mcbot.txt");
//pool de mensagens do minecraft
$chatmsgs_cache = getStoredValue("mcbot_chatcache.txt");
//$chatmsgs = file_get_contents('http://keyo.me/minecraft/chat.php');
//https://dl.dropboxusercontent.com/u/2710481/minecraft_server/plugins/Chat_Saver/22-02-2016.log
$hoje = date('d-m-Y', strtotime('-6 hours'));
$chatmsgs = utf8_encode(tail(@file_get_contents('/srv/minecraft/logs/chat/logs.txt'),2));
if(isset($chatmsgs) && $chatmsgs !== "" && $chatmsgs_cache !== $chatmsgs ) {
//error_log("EH DIFERENTE");
//atualiza o cache
setStoredValue($chatmsgs, "mcbot_chatcache.txt");
$arr = explode(":", explode("]", $chatmsgs)[1] );
if(sizeof($arr) > 1 ) {
$nome = $arr[0];
$texto = $arr[1];
//$ultima = shell_exec("tail /var/www/keyo.me/files/minecraft/mcbot_chatcache.txt -n 1");
sendMessageMarkdown($chat_do_lab, "\xF0\x9F\x92\xAC *" . $nome . ":* " . $texto);
}
}
//else
//error_log("NEM EH DIFERENTE");
//Salva o offset do ultimo update
setStoredValue($offset, "offset_mcbot.txt");
echo "OK";
/* ========= CRIME SCENE - DO NOT CROSS ========== */
function sendMessageMarkdown($chat_id, $text, $disablePreview = true) {
global $_BOT_TOKEN;
global $_API_URL;
$url = $_API_URL . "sendMessage";
$post_data = array(
'chat_id' => $chat_id,
'text' => $text,
'parse_mode' => 'Markdown',
'disable_web_page_preview' => $disablePreview
);
postRequest($url, $post_data);
}
function tail($string, $n = 1) {
$lines = explode("\n", $string);
$lines = array_slice($lines, -$n);
return implode("\n", $lines);
}
function sendPhoto($chat_id, $file) {
global $_BOT_TOKEN;
global $_API_URL;
$url = $_API_URL . "sendPhoto";
/*$post_data = array(
'chat_id' => (int)$chat_id
);
error_log("enviando foto em: " . $file . " para chat: '" . $chat_id . "'");
postRequest($url, $post_data, $file); */
$code = shell_exec('curl -s -X POST -o /dev/null -w "%{http_code}" ' . $url . ' -F chat_id=' . $chat_id . ' -F photo="@' . $file . '"');
error_log("executei: " . "curl -s -X POST -o /dev/null -w \"%{http_code}\" " . $url . " -F chat_id=" . $chat_id . " -F photo=\"" . $file . "\"");
error_log("a resposta foi " . ((string)$code));
return $code;
}
function sendDocument($chat_id, $file) {
global $_BOT_TOKEN;
global $_API_URL;
$url = $_API_URL . "sendDocument";
$code = shell_exec('curl -s -X POST -o /dev/null -w "%{http_code}" ' . $url . ' -F chat_id=' . $chat_id . ' -F document="@' . $file . '"');
error_log("executei: " . "curl -s -X POST -o /dev/null -w \"%{http_code}\" " . $url . " -F chat_id=" . $chat_id . " -F document=\"" . $file . "\"");
error_log("a resposta foi " . ((string)$code));
return $code;
}
function sendVoice($chat_id, $file) {
global $_BOT_TOKEN;
global $_API_URL;
$url = $_API_URL . "sendVoice";
/*$post_data = array(
'chat_id' => (int)$chat_id
);
error_log("enviando foto em: " . $file . " para chat: '" . $chat_id . "'");
postRequest($url, $post_data, $file); */
$code = shell_exec('curl -s -X POST -o /dev/null -w "%{http_code}" ' . $url . ' -F chat_id=' . $chat_id . ' -F voice="@' . $file . '"');
error_log("executei: " . "curl -s -X POST -o /dev/null -w \"%{http_code}\" " . $url . " -F chat_id=" . $chat_id . " -F voice=\"" . $file . "\"");
//error_log("a resposta foi " . ((string)$code));
return $code;
}
function issudo($user, $chat_id) {
if(!startsWith($user, '@'))
$user = '@' . $user;
$sudoers = json_decode(file_get_contents("./sudoers.json"), true);
if(!in_array($user, $sudoers)) {
sendMessage($chat_id, "\xF0\x9F\x94\x90 Você não tem permissão pra isso.");
return false;
}
else
return true;
}
function chat_liberado($chat_id) {
$chats = json_decode(file_get_contents("./chats.json"), true);
return in_array($chat_id, $chats);
}
function sendMessage($chat_id, $text, $disablePreview = false) {
global $_BOT_TOKEN;
global $_API_URL;
$url = $_API_URL . "sendMessage";
$post_data = array(
'chat_id' => $chat_id,
'text' => $text,
'disable_web_page_preview' => $disablePreview
);
postRequest($url, $post_data);
}
function replyMessage($chat_id, $text, $disablePreview = false, $idreply) {
global $_BOT_TOKEN;
global $_API_URL;
$url = $_API_URL . "sendMessage";
$post_data = array(
'chat_id' => $chat_id,
'text' => $text,
'disable_web_page_preview' => $disablePreview,
'reply_to_message_id' => $idreply
);
postRequest($url, $post_data);
}
function sendChatAction($chat_id, $action) {
global $_BOT_TOKEN;
global $_API_URL;
$url = $_API_URL . "sendChatAction";
$post_data = array(
'chat_id' => $chat_id,
'action' => $action
);
postRequest($url, $post_data);
}
function forwardMessage($chat_id, $from_chat_id, $message_id) {
global $_BOT_TOKEN;
global $_API_URL;
$url = $_API_URL . "forwardMessage";
$post_data = array(
'chat_id' => $chat_id,
'from_chat_id' => $from_chat_id,
'message_id' => $message_id
);
postRequest($url, $post_data);
}
function sendMessageReply($chat_id, $text, $reply_to_message_id) {
}
function getUpdates($offset) {
global $_BOT_TOKEN;
global $_API_URL;
$url = $_API_URL . "getUpdates";
$post_data = array(
'offset' => $offset
);
$array = postRequest($url, $post_data);
return $array;
}
function getStoredValue($filename) {
$fp = fopen($filename, "r");
$value = fread($fp, 1024);
fclose($fp);
return $value;
}
function setStoredValue($value, $filename) {
$fp = fopen($filename, "w");
fwrite($fp, $value);
fclose($fp);
}
function contains($haystack,$needle) {
return strpos($haystack,$needle);
}
function startsWith($haystack, $needle) {
return $needle === "" || strrpos($haystack, $needle, -strlen($haystack)) !== FALSE;
}
function endsWith($haystack, $needle) {
return $needle === "" || (($temp = strlen($haystack) - strlen($needle)) >= 0 && strpos($haystack, $needle, $temp) !== FALSE);
}
function postRequest($url, $data, $file='', $referer='') {
// Convert the data array into URL Parameters like a=b&foo=bar etc.
$data = http_build_query($data);
// parse the given URL
$url2 = parse_url($url);
$host = $url2['host'];
$path = $url2['path'];
$port = 443;
//$header = array('Content-Type: multipart/form-data');
// POST data, formatted just like a GET query string
$request = $data;
// the client certificate path MUST be a physical path, not url
$certificate = "/etc/pki/tls/private/test.crt"; // unix/linux example
$password = 'mypassword'; // client certificate's key
// more details at http://php.net/curl
$ch = curl_init();
//Tem file? coloca aqui
if($file != ''){
$request = $request . '&photo=@' . $file;
}
//error_log("request:::: " . $request);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_PORT, $port);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
//curl_setopt($ch, CURLOPT_SSLCERT, $certificate);
//curl_setopt($ch, CURLOPT_SSLCERTPASSWD, $password);
// fetch response and close the socket
$response = curl_exec($ch);
curl_close($ch);
//error_log("response================== " . $response);
return $response;
}
function var_error_log( $object=null ){
ob_start(); // start buffer capture
var_dump( $object ); // dump the values
$contents = ob_get_contents(); // put the buffer into a variable
ob_end_clean(); // end capture
error_log( $contents ); // log contents of the result of var_dump( $object )
}
//user saiu
// {"ok":true,"result":[{"update_id":846975114,\n"message":{"message_id":47704,"from":{"id":36737915,"first_name":"Eicho","username":"Eicho"},"chat":{"id":-25770128,"title":"Serra do Cantareira"},"date":1441304969,"left_chat_participant":{"id":36737915,"first_name":"Eicho","username":"Eicho"}}}]}
?>