EIOS/whtg/tg8hook.php

97 lines
3.3 KiB
PHP

<?php
//Скрипт, который ждет сообщения от Телеграм и обрабатывает их
include 'config.php';
include 'function.php';
$data = file_get_contents('php://input');
$arrDataAnswer = json_decode($data, true);
//writeLogFile($arrDataAnswer);
//ЧТо обычно приходит{
// "update_id": 803290892,
// "message": {
// "message_id": 41,
// "from": {
// "id": 1424646511,
// "is_bot": false,
// "first_name": "Илья",
// "last_name": "Лящук",
// "username": "iliyalyachuk",
// "language_code": "ru"
// },
// "chat": {
// "id": 1424646511,
// "first_name": "Илья",
// "last_name": "Лящук",
// "username": "iliyalyachuk",
// "type": "private"
// },
// "date": 1659098034,
// "text": "Новое тестовое сообщение"
// }
//}
//
$textMessage = mb_strtolower($arrDataAnswer["message"]["text"]);
$chatId = $arrDataAnswer["message"]["chat"]["id"];
$username = $arrDataAnswer["message"]["from"]["username"];
$first_name = $arrDataAnswer["message"]["from"]["first_name"];
$last_name = $arrDataAnswer["message"]["from"]["last_name"];
if(!empty($arrDataAnswer["message"]["photo"])) {
$documentData = array_pop($arrDataAnswer["message"]["photo"]);
}
else if(!empty($arrDataAnswer["message"]["document"])) {
$documentData = array_pop($arrDataAnswer["message"]["document"]);
}
$newFilerPath='';
//writeLogFile(array('$documentData'=>$documentData));
if(!empty($documentData)) {
$arrayQuery['getQuery'] = array(
"file_id" => $documentData["file_id"],
);
$resultQuery = TG_getFile($arrayQuery);
/* записываем ответ в формате PHP массива */
$arrDataResult = json_decode($resultQuery, true);
/* записываем URL необходимого изображения */
$fileUrl = $arrDataResult["result"]["file_path"];
/* формируем полный URL до файла */
$photoPathTG = "https://api.telegram.org/file/bot". $data['token'] ."/" . $fileUrl;
/* забираем название файла */
$arrFilePath = explode("/", $fileUrl);
//writeLogFile(array('$fileUrl'=>$fileUrl,'$arrFilePath'=>$arrFilePath));
$newFilerPath = __DIR__ . "/files/" . $arrFilePath[1];
/* сохраняем файл на сервер */
file_put_contents($newFilerPath , file_get_contents($photoPathTG));
$arrayQuery['getQuery'] = array(
'chat_id' => $chatId,
'text' => $conf['text']['file_get'],
'parse_mode' => "html",
);
sendMessage($arrayQuery);
}
//writeLogFile(array('text'=>$textMessage,'chatid'=>$chatId));
if($textMessage == '/start') {
$arrayQuery['getQuery'] = array(
'chat_id' => $chatId,
'text' => $conf['text']['start'],
'parse_mode' => "html",
);
sendMessage($arrayQuery);
exit();
}
if($textMessage != '') {
$arrayQuery['getQuery'] = array(
'chat_id' => $chatId,
'text' => $conf['text']['message_get'],
'parse_mode' => "html",
);
sendMessage($arrayQuery);
}
$info['username'] = $username;
$info['first_name'] = $first_name;
$info['last_name'] = $last_name;
$info['chatId'] = $chatId;
$info['textMessage'] = $textMessage;
if($newFilerPath!='') $info['file'] = $newFilerPath;
writeLogFile($info);