github/app/Http/Controllers/Api/TG/HookController.php
2025-04-14 16:49:38 +08:00

112 lines
3.1 KiB
PHP
Executable File

<?php
declare(strict_types=1);
namespace App\Http\Controllers\Api\TG;
use App\Models\Article;
use App\Services\TomTool\Http;
use App\Services\TG\Http as TGHttp;
final class HookController
{
public function __construct()
{
// $this->middleware('csrf')->except(['cmd']);
// 或者 $this->middleware('csrf')->only(['specificMethod']);
}
public function cmd()
{
try {
$oTGHttp = new TGHttp();
$oTGHttp->sApiToken = env('telegram_token');
$oTGHttp->sApiChatId = env('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(200, 'not have text');
}
$sText = $aUpdate["message"]["text"];
// test
// $sText = "\\//Article#List ni__aaa__bbb 111 222";
// $sText = "///\\\SaleCode#SetRate__zzxx 2";
// $sText = "///\\\SaleCode#List";
// $sText = "///\\\SaleCode#Find zzxx";
// $sText = "/Article#Like__sale_code zzx";
// $sText = "////Article#Add jxxjjjj";
// $sText = "//\\\//Article#Del 9";
// $sText = "/Article#Set__1__user_name xxx";
// $sText = "$ cmd#list__si__sk 234";
// 拆分后直接
@$aArg = explode(" ", $sText);
if ($aArg[0] != '$') {
exit();
}
unset($aArg[0]); // 丢掉$
$aArg = array_values($aArg);
@$aOption = explode("__", $aArg[0]);
$sCmd = $this->cleanCmd($aOption[0]);
list($sClass, $sFunc) = explode("#", $sCmd);
unset($aOption[0]);
unset($aArg[0]);
$aParams['aOption'] = $aOption;
$aParams['aArg'] = $aArg;
$sClassPath = "App\\Services\\TG\\Hook\\".$sClass;
if (method_exists($sClassPath, $sFunc)) {
$oInstans = new $sClassPath();
$r = $oInstans->$sFunc($aParams);
} else {
$oTGHttp->sendToTG("没有这个方法");
}
} catch (\Exception $e) {
$sMessage = "错误: " . $e->getMessage();
$oTGHttp->sendToTG($sMessage);
Http::response(200, 'error');
}
}
private function cleanCmd($input) {
// 去掉所有的反斜杠
$input = str_replace('\\', '', $input);
// 去掉所有的正斜杠
$input = str_replace('/', '', $input);
return $input;
}
}