116 lines
3.5 KiB
PHP
Executable File
116 lines
3.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;
|
||
|
||
final class HookController
|
||
{
|
||
public function cmd()
|
||
{
|
||
|
||
$oTGHttp = new TGHttp();
|
||
|
||
$oTGHttp->sApiToken = env('telegram_token');
|
||
$oTGHttp->sApiChatId = env('telegram_chatid');
|
||
|
||
try {
|
||
|
||
$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"];
|
||
|
||
// $mSendType = false;
|
||
//
|
||
// if (isset($aUpdate["is_dove"])) {
|
||
// $mSendType = "dove";
|
||
// }
|
||
|
||
// tomd($mSendType, 1);
|
||
|
||
// $mSendType = $aUpdate[]
|
||
|
||
if (!isset($aUpdate["message"]["text"])) {
|
||
throw new \Exception("?");
|
||
}
|
||
|
||
$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";
|
||
|
||
@$aArg = explode(" ", $sText);
|
||
@$aOption = explode("__", $aArg[0]);
|
||
|
||
$sCmd = $this->cleanCmd($aOption[0]);
|
||
|
||
list($sClass, $sFunc) = explode("#", $sCmd);
|
||
$sClass = ucfirst($sClass);
|
||
$sFunc = ucfirst($sFunc);
|
||
|
||
unset($aOption[0]);
|
||
unset($aArg[0]);
|
||
|
||
$aParams['aOption'] = $aOption;
|
||
$aParams['aArg'] = $aArg;
|
||
|
||
$sClassPath = "App\\Services\\Tg\\Hook\\Mod\\".$sClass;
|
||
|
||
// if (method_exists($sClassPath, $sFunc)) {
|
||
$oInstans = new $sClassPath($aUpdate);
|
||
$r = $oInstans->$sFunc($aParams);
|
||
|
||
// 可以不返回,里面直接send
|
||
if (isset($r)) {
|
||
if (empty($r)) { $r = 'hook的mod返回空'; }
|
||
// 返回可以是数组
|
||
if (is_array($r)) {
|
||
foreach ($r as $row) {
|
||
$oTGHttp->sendToTG($row, $aUpdate);
|
||
}
|
||
} else {
|
||
$oTGHttp->sendToTG($r, $aUpdate);
|
||
}
|
||
|
||
Http::response(200);
|
||
}
|
||
|
||
// } else {
|
||
// throw new \Exception("没有这个方法:".$sClass.'#'.$sFunc);
|
||
// }
|
||
|
||
} catch (\Throwable $e) {
|
||
$sMessage = "错误(line:".$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;
|
||
}
|
||
|
||
}
|