fuc/src/Controllers/TomApi/Tg/HookController.php
2025-03-24 22:41:58 +08:00

183 lines
5.4 KiB
PHP
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
//declare(strict_types=1);
namespace App\Controllers\TomApi\Tg;
//use App\Controllers\BaseController;
//use Psr\Http\Message\ResponseInterface;
//use Slim\Http\Response;
//use Slim\Http\ServerRequest;
use App\Models\Config;
use App\Models\TgStep;
use App\Models\Ticket;
//use GuzzleHttp\Psr7\ServerRequest;
//use GuzzleHttp\Psr7\Utils;
//use Psr\Http\Message\ResponseInterface;
//use Slim\Http\Response;
//use Exception;
use GuzzleHttp\Exception\GuzzleException;
use Psr\Http\Client\ClientExceptionInterface;
use Psr\Http\Message\ResponseInterface;
use Slim\Http\Response;
use Slim\Http\ServerRequest;
use App\Services\TomTool\Http;
final class HookController
{
public $sApiToken;
public $sApiChatId;
public function cmd()
{
$oTgStep = new TgStep();
$this->sApiToken = Config::obtain('telegram_token');
$this->sApiChatId = Config::obtain('telegram_chatid');
$sUpdate = file_get_contents("php://input");
$aUpdate = json_decode($sUpdate, true);
@file_put_contents('/www/_tg/bot_log.txt', json_encode($aUpdate, JSON_PRETTY_PRINT));
$aData['chatId'] = $aUpdate["message"]["chat"]["id"];
$aData['fromId'] = $aUpdate["message"]["from"]["id"];
if (!isset($aUpdate["message"]["text"])) {
http_response_code(200);
echo "not have text";
exit;
}
$sText = $aUpdate["message"]["text"];
// 先检查step
if (strpos($sText, "/s ") === 0 && $oTgStep->fromIdHit($aData['fromId'])) {
$sText = ltrim($sText, '/s');
$sText = trim($sText);
$aTgStepFirst = $oTgStep->find($aData["fromId"]);
if ($aTgStepFirst["cmd"] == "reply") {
if ($aTgStepFirst['step'] == 1) {
$oHttp = new Http();
$oHttp->sMethod = "put";
$oHttp->sToUrl = $_ENV['baseUrl']."/api/tg/ticket/".$aTgStepFirst['cache']['ticketId'];
$oHttp->bIsJson = true;
$aData['comment'] = $sText;
$aData['admin_name'] = $aUpdate["message"]["from"]["first_name"];
$oHttp->aData = $aData;
$r = @json_decode($oHttp->send(), true);
// 不论失败成功都清除step
$oTgStep->clearByFromId($aUpdate["message"]["from"]["id"]);
if ($r["ret"] == 1) {
$sMessage = "成功";
} else {
$sMessage = "失败";
Http::response(500, json_encode($aData));
}
$this->sendMessage($sMessage);
Http::response(200, 'ok');
}
}
}
// 命令
// if ($sText === "/test") {
//
// $serverTime = date("Y-m-d H:i:s");
//
// $this->sendMessage("服务器时间: " . $serverTime);
//
// http_response_code(200);exit;
// }
if ($sText === "/break") {
$oTgStep->clearByFromId($aData["fromId"]);
$this->sendMessage("ok");
http_response_code(200);exit;
}
if (strpos($sText, '/reply') === 0) {
// 先清除一遍,只进行一个进程
$oTgStep->clearByFromId($aData["fromId"]);
$iTicketId = str_replace('/reply', '', $sText);
$iTicketId = (int) trim($iTicketId);
$oTgStep->add($aData['fromId'], 'reply', 1, ["ticketId" => $iTicketId]);
$sMessage = "将回复工单【".$iTicketId."】,请输入回复内容。";
$this->sendMessage($sMessage);
Http::response(200, 'ok');
}
if (strpos($sText, '/ticket_find') === 0) {
$iTicketId = $this->trimText($sText, '/ticket_find');
$aTicket = Ticket::where("id", $iTicketId)->first()->toArray();
$sMsg = "title".$aTicket['title']."\n";
$aTicketContent = json_decode($aTicket, true);
foreach ($aTicketContent as $row) {
$sMsg .= "\n";
$sMsg .= "name".$row['commenter_name']."\n";
$sMsg .= "comment".$row['comment']."\n";
}
$this->sendMessage($sMsg);
Http::response(200, 'ok');
}
Http::response(200, '无');
}
private function trimText($sText, $sTrim)
{
$sText = ltrim($sText, $sTrim);
$sText = trim($sText);
return $sText;
}
private function sendMessage($sText) {
if ($_ENV['is_loc'] === true) {
var_dump($sText);
} else {
$url = "https://api.telegram.org/bot$this->sApiToken/sendMessage?chat_id=$this->sApiChatId&text=" . urlencode($sText);
file_get_contents($url);
}
}
}