diff --git a/app/routes.php b/app/routes.php index 26938c32..a49a17ea 100755 --- a/app/routes.php +++ b/app/routes.php @@ -50,7 +50,12 @@ return static function (Slim\App $app): void { })->add(new TgAdmin()); // test - $app->get('/test', App\Controllers\TestController::class . ':index'); + $app->get('/test1', App\Controllers\TestController::class . ':test1'); + $app->get('/test2', App\Controllers\TestController::class . ':test2'); + $app->get('/test3', App\Controllers\TestController::class . ':test3'); + $app->get('/test4', App\Controllers\TestController::class . ':test4'); + $app->get('/test5', App\Controllers\TestController::class . ':test5'); + $app->get('/test6', App\Controllers\TestController::class . ':test6'); // doc $app->group('/doc', static function (RouteCollectorProxy $group): void { diff --git a/public/tests/hook.php b/public/tests/hook.php index 7be81901..30057612 100644 --- a/public/tests/hook.php +++ b/public/tests/hook.php @@ -12,6 +12,7 @@ $oHttp = new App\Services\TomTool\Http(); $oHttp->sMethod = "post"; $oHttp->sToUrl = $_ENV['baseUrl']."/api/tg/hook"; +//$oHttp->sToUrl = 'https://fuc.loc/test3'; $oHttp->bIsJson = true; //$oHttp->aData[] $oHttp->aData = [ @@ -34,7 +35,7 @@ $oHttp->aData = [ 'type' => 'private' ], 'date' => 1610000000, - 'text' => '/reply 62 哈哈哈哈哈哈哈哈哈哈你麻痹' + 'text' => '222' ] ]; @@ -47,6 +48,7 @@ exit; $url = 'https://fuc.loc/api/tg/hook'; +$url = 'https://fuc.loc/test3'; $data = [ 'message' => [ 'chat' => [ diff --git a/src/Controllers/TestController.php b/src/Controllers/TestController.php index d68ac54f..cc417f88 100755 --- a/src/Controllers/TestController.php +++ b/src/Controllers/TestController.php @@ -16,9 +16,73 @@ use App\Models\UserCoupon; use App\Services\Cron as CronService; use App\Services\Reward; +use App\Services\TomTool\Http; + + +session_start(); + final class TestController extends BaseController { - public function index(ServerRequest $request, Response $response, array $args): ResponseInterface + + public function test4 () + { +// session_start(); + + $oHttp = new Http(); + + $oHttp->sMethod = "get"; + $oHttp->sToUrl = $_ENV['baseUrl']."/api/tg/hook"; + $oHttp->sToUrl = 'https://fuc.loc/test3'; +// $oHttp->bIsJson = true; + //$oHttp->aData[] + $oHttp->aData = [ + 'update_id' => 123456789, + 'message' => [ + 'message_id' => 1, + 'from' => [ + 'id' => 987654321, + 'is_bot' => false, + 'first_name' => 'John', + 'last_name' => 'Doe', + 'username' => 'johndoe', + 'language_code' => 'en' + ], + 'chat' => [ + 'id' => -4668400285, + 'first_name' => 'John', + 'last_name' => 'Doe', + 'username' => 'johndoe', + 'type' => 'private' + ], + 'date' => 1610000000, + 'text' => '22' + ] + ]; + + $r = $oHttp->send(); + + var_dump($r); + exit; + } + + public function test2() + { + + $_SESSION['test'] = 333; + var_dump($_SESSION); + + http_response_code(200);exit; + } + + public function test3() + { + + var_dump($_SESSION); + + http_response_code(200);exit; + } + + public function test1(ServerRequest $request, Response $response, array $args): ResponseInterface { error_reporting(E_ALL); // 报告所有错误 diff --git a/src/Controllers/TomApi/Tg/HookController.php b/src/Controllers/TomApi/Tg/HookController.php index bcb230c5..53207f2a 100755 --- a/src/Controllers/TomApi/Tg/HookController.php +++ b/src/Controllers/TomApi/Tg/HookController.php @@ -1,6 +1,6 @@ sApiToken = Config::obtain('telegram_token'); $this->sApiChatId = Config::obtain('telegram_chatid'); @@ -38,9 +41,10 @@ final class HookController $aUpdate = json_decode($sUpdate, true); - file_put_contents('/www/_tg/bot_log.txt', json_encode($aUpdate, JSON_PRETTY_PRINT)); + @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); @@ -59,47 +63,68 @@ final class HookController http_response_code(200);exit; } - if (strpos($sText, '/reply') === 0) { - // 去掉 /reply 字符串 - $responseText = str_replace('/reply', '', $sText); // 直接去掉 /reply - $responseText = trim($responseText); // 去掉前后的空格 - - // 使用空格分割内容 - $responseParts = explode(' ', $responseText, 2); // 分割成两部分 - if (count($responseParts) == 2) { - - $number = $responseParts[0]; // 提取数字部分 - $variableText = $responseParts[1]; // 提取用户输入的文本 - - $oHttp = new Http(); - - $oHttp->sMethod = "put"; - $oHttp->sToUrl = $_ENV['baseUrl']."/api/tg/ticket/".$number; - $oHttp->bIsJson = true; - $aData['comment'] = $variableText; - $aData['admin_name'] = $aUpdate["message"]["from"]["first_name"]; - $oHttp->aData = $aData; - - $r = @json_decode($oHttp->send(), true); - - if ($r["ret"] == 1) { - $sMessage = "成功"; - } else { - $sMessage = "失败"; - } - - $this->sendMessage($sMessage); - - } + if ($sText === "/break") { + + $this->sendMessage("ok"); http_response_code(200);exit; } - 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 ($oTgStep->fromIdHit($aData['fromId'])) { + + $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); + + if ($r["ret"] == 1) { + $sMessage = "成功"; + } else { + $sMessage = "失败"; + Http::response(500, json_encode($aData)); + } + + $this->sendMessage($sMessage); + + Http::response(200, 'ok'); + } + + } + + } + + Http::response(200, 'ok'); } - // 发送消息的函数 public function sendMessage($sText) { if ($_ENV['is_loc'] === true) { diff --git a/src/Models/TgStep.php b/src/Models/TgStep.php new file mode 100755 index 00000000..85ab17cd --- /dev/null +++ b/src/Models/TgStep.php @@ -0,0 +1,65 @@ +from_id = $iFromId; + $oTgStep->cmd = $sCmd; + $oTgStep->step = $iStep; + $oTgStep->cache = json_encode($sCache); + + $oTgStep->save(); + + } + + public function fromIdHit($iFromId) + { + return TgStep::where('from_id', $iFromId)->exists(); + } + + public function find($iFromId, $iStep = false, $sCmd = false) + { + $oTgStep = new TgStep(); + + $oTgStep->where("from_id", $iFromId); + + if ($iStep) { + $oTgStep->where("step", $iStep); + } + + if ($sCmd) { + $oTgStep->where("cmd", $sCmd); + } + + $aData = $oTgStep->orderBy("step", "desc")->first()->toArray(); + + $aData['cache'] = json_decode($aData['cache'], true); + + return $aData; + } + + public function clearByFromId($iFromId) + { + $oTgStep = new TgStep(); + + return $oTgStep->where("from_id", $iFromId)->delete(); + } + + public function clearAll() + { + return TgStep::delete(); + } +} diff --git a/src/Models/Zhuanfas.php b/src/Models/Zhuanfas.php index f3320ca6..9a5bf827 100755 --- a/src/Models/Zhuanfas.php +++ b/src/Models/Zhuanfas.php @@ -5,7 +5,6 @@ declare(strict_types=1); namespace App\Models; use Illuminate\Database\Query\Builder; -use function time; final class Zhuanfas extends Model { diff --git a/src/Services/TomTool/Http.php b/src/Services/TomTool/Http.php index d06dc63d..298da54d 100755 --- a/src/Services/TomTool/Http.php +++ b/src/Services/TomTool/Http.php @@ -73,4 +73,11 @@ class Http // 返回响应 return $response; } + + public static function response($iCode, $sMsg = '') + { + http_response_code($iCode); + echo $sMsg; + exit; + } }