dd/app/Http/Controllers/Api/TG/HookController.php
2025-05-11 10:37:57 +08:00

144 lines
4.6 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\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 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"])) {
throw new \Exception("");
}
$sText = $aUpdate["message"]["text"];
@$aArg = explode(" ", $sText);
@$aOption = explode("__", $aArg[0]);
$sCmd = $this->cleanCmd($aOption[0]);
// 大于3个说明是hook或dove之类
$aCmd = explode("#", $sCmd);
if (count($aCmd) >= 3) {
$sHook = $aCmd[0];
$sClass = $sHook;
$sFunc = "go";
} else {
$sClass = $aCmd[0];
$sFunc = $aCmd[1];
}
// if ($sClass == "book" || $sClass == "Book") {
// if (strpos($sFunc, '/') !== false) {
// echo "字符串中包含斜杠。";
// }
// }
$sUClass = ucfirst($sClass);
$sUFunc = ucfirst($sFunc);
unset($aOption[0]);
unset($aArg[0]);
$aParams['aOption'] = $aOption;
$aParams['aArg'] = $aArg;
$sClassPath = "App\\Services\\TG\\Hook\\Mod\\".$sClass;
$sUClassPath = "App\\Services\\TG\\Hook\\Mod\\".$sUClass;
// 兼容大小写,自己挖的坑
if (method_exists($sClassPath, $sFunc)) {
$oInstans = new $sClassPath();
$r = $oInstans->$sFunc($aParams, $aUpdate);
} else if (method_exists($sUClassPath, $sUFunc)) {
$oInstans = new $sUClassPath();
$r = $oInstans->$sUFunc($aParams, $aUpdate);
} else if ($sClass == "book" || $sClass == "Book") {
$sFunc = "cd";
$r = $oInstans->$sFunc($aParams, $aUpdate);
} else {
throw new \Exception("没有这个方法:".$sClass.'#'.$sFunc);
}
if (isset($r)) {
// 返回可以是数组
if (is_array($r)) {
foreach ($r as $row) {
$oTGHttp->sendToTG($row);
}
} else {
$oTGHttp->sendToTG($r);
}
Http::response(200, 'ok');
} else {
throw new \Exception("?!?!?!?!");
}
// if (method_exists($sClassPath, $sFunc)) {
// $oInstans = new $sClassPath();
// $r = $oInstans->$sFunc($aParams, $aUpdate);
//
// // 可以不返回里面直接send
// if (isset($r)) {
//// if (empty($r)) { $r = 'hook的mod返回空'; }
// // 返回可以是数组
// if (is_array($r)) {
// foreach ($r as $row) {
// $oTGHttp->sendToTG($row);
// }
// } else {
// $oTGHttp->sendToTG($r);
// }
//
// Http::response(200, 'ok');
// }
//
// } else {
// throw new \Exception("没有这个方法:".$sClass.'#'.$sFunc);
// }
} catch (\Throwable $e) {
$sMessage = "错误line:".$e->getLine().": " . $e->getMessage();
$oTGHttp->sendToTG($sMessage);
Http::response(200, 'error');
}
}
private function cleanCmd($input) {
$input = str_replace('\\', '', $input);
$input = str_replace('/', '', $input);
return $input;
}
}