fuc/src/Controllers/TomApi/Tg/HookController.php
hellcat 881b82316c 1
2025-03-25 21:05:54 +08:00

295 lines
9.3 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 App\Models\Node;
//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 === "/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, '/ry') === 0) {
$sText = $this->trimText($sText, '/ry');
list($iTicketId, $sText) = explode(" ", $sText, 2);
$oHttp = new Http();
$oHttp->sMethod = "put";
$oHttp->sToUrl = $_ENV['baseUrl']."/api/tg/ticket/".$iTicketId;
$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 (strpos($sText, '/nodeLike_') === 0) {
list($sCmd, $sLike) = explode(" ", $sText, 2);
list(, $sField) = explode("_", $sCmd, 2);
if ($sField == "port") {
$sField = "custom_config";
}
$aNodeList = Node::where($sField, "like", "%".$sLike."%")->get()->toArray();
$sMsg = '';
foreach ($aNodeList as $row) {
$sMsg .= "\n\n";
$sMsg .= "名字:".$row['name']."\n";
$sMsg .= "转发:".$row['zhuanfa_code']."\n";
$sMsg .= "机器:".$row['duck_code']."\n";
$sMsg .= "备注:".$row['memo']."\n";
$sMsg .= "目标:".$row['server']."\n";
$sMsg .= "设置:".$row['custom_config']."\n";
}
$this->sendMessage($sMsg);
Http::response(200, 'ok');
}
// 节点搜索 name
if (strpos($sText, '/nodeNameLike') === 0) {
$sNameLike = $this->trimText($sText, '/nodeNameLike');
$aNodeList = Node::where("name", "like", "%".$sNameLike."%")->toArray();
$sMsg = '';
foreach ($aNodeList as $row) {
$sMsg .= "\n\n";
$sMsg .= "名字:".$row['name']."\n";
$sMsg .= "转发:".$row['zhuanfa_code']."\n";
$sMsg .= "机器:".$row['duck_code']."\n";
$sMsg .= "备注:".$row['memo']."\n";
$sMsg .= "目标:".$row['server']."\n";
$sMsg .= "设置:".$row['custom_config']."\n";
}
$this->sendMessage($sMsg);
Http::response(200, 'ok');
}
// 节点搜索 port
if (strpos($sText, '/nodePortLike') === 0) {
$sPortLike = $this->trimText($sText, '/nodePortLike');
$aNodeList = Node::where("custom_config", "like", "%".$sPortLike."%")->toArray();
$sMsg = '';
foreach ($aNodeList as $row) {
$sMsg .= "\n\n";
$sMsg .= "名字:".$row['name']."\n";
$sMsg .= "转发:".$row['zhuanfa_code']."\n";
$sMsg .= "机器:".$row['duck_code']."\n";
$sMsg .= "备注:".$row['memo']."\n";
$sMsg .= "目标:".$row['server']."\n";
$sMsg .= "设置:".$row['custom_config']."\n";
}
$this->sendMessage($sMsg);
Http::response(200, 'ok');
}
// 工单查找
if (strpos($sText, '/ticketFind') === 0) {
$iTicketId = $this->trimText($sText, '/ticket_find');
$aTicket = Ticket::where("id", $iTicketId)->first()->toArray();
$sMsg = "title".$aTicket['title']."\n";
$aTicketContent = json_decode($aTicket['content'], true);
if (!is_array($aTicketContent[0])) {
$aTicketContent[] = $aTicketContent;
}
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);
}
}
}