143 lines
4.5 KiB
PHP
Executable File
143 lines
4.5 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;
|
||
use App\Models\Hook as ModelHook;
|
||
|
||
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'] = (string) $aUpdate["message"]["chat"]["id"];
|
||
$aData['fromId'] = (string) $aUpdate["message"]["from"]["id"];
|
||
|
||
if (!isset($aUpdate["message"]["text"])) {
|
||
throw new \Exception("?");
|
||
}
|
||
|
||
$sText = &$aUpdate["message"]["text"];
|
||
|
||
// hook优先
|
||
// 非is_hook,并且,首字符非/
|
||
if ((!isset($aUpdate["is_hook"]) || $aUpdate["is_hook"] == false) && substr($sText, 0, 1) !== "/") {
|
||
|
||
$sHookKey = ModelHook::where("tg_user_id", $aData["fromId"])->first()?->key;
|
||
|
||
if ($sHookKey) {
|
||
if ($sText == "run") {
|
||
$sText = $sHookKey;
|
||
} else {
|
||
$sText = $sHookKey.$sText;
|
||
}
|
||
$aUpdate["is_hook"] = true;
|
||
}
|
||
|
||
}
|
||
|
||
@$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] ?? false;
|
||
$sFunc = $aCmd[1] ?? false;
|
||
// }
|
||
|
||
if (strpos($sClass, "dove$") !== false) {
|
||
$sClass = "dove";
|
||
$sFunc = "go";
|
||
}
|
||
|
||
$sUClass = ucfirst($sClass);
|
||
$sUFunc = ucfirst($sFunc);
|
||
|
||
unset($aOption[0]);
|
||
unset($aArg[0]);
|
||
|
||
$aParams = [];
|
||
$aParams['aOption'] = $aOption;
|
||
$aParams['aArg'] = $aArg;
|
||
|
||
$sClassPath = "App\\Services\\TG\\Hook\\Mod\\".$sClass;
|
||
$sUClassPath = "App\\Services\\TG\\Hook\\Mod\\".$sUClass;
|
||
|
||
$oInstans = new $sClassPath($aUpdate);
|
||
$r = $oInstans->$sFunc($aParams);
|
||
|
||
// 兼容大小写,自己挖的坑
|
||
// if (method_exists($sClassPath, $sFunc)) {
|
||
// $oInstans = new $sClassPath($aUpdate);
|
||
// $r = $oInstans->$sFunc($aParams, $aUpdate);
|
||
// } else if (method_exists($sUClassPath, $sUFunc)) {
|
||
// $oInstans = new $sUClassPath($aUpdate);
|
||
// $r = $oInstans->$sUFunc($aParams, $aUpdate);
|
||
// }
|
||
//// else if ($sClass == "book" || $sClass == "Book") {
|
||
//// $sFunc = "main";
|
||
//// $oInstans = new $sUClassPath();
|
||
//// $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, $aUpdate);
|
||
}
|
||
} else {
|
||
$oTGHttp->sendToTG($r, $aUpdate);
|
||
}
|
||
|
||
Http::response(200);
|
||
} else {
|
||
throw new \Exception("?!?!?!?!");
|
||
}
|
||
|
||
} catch (\Throwable $e) {
|
||
$sMessage = "错误(文件: " . $e->getFile() . ",行: " . $e->getLine() . "): " . $e->getMessage();
|
||
|
||
$oTGHttp->sendToTG($sMessage, $aUpdate);
|
||
|
||
Http::response(200, 'error');
|
||
}
|
||
}
|
||
|
||
private function cleanCmd($input) {
|
||
|
||
$input = str_replace('\\', '', $input);
|
||
$input = str_replace('/', '', $input);
|
||
|
||
return $input;
|
||
}
|
||
|
||
}
|