no message

This commit is contained in:
hellcat 2025-03-21 21:50:04 +08:00
parent 7608cb5670
commit 800780adbb
8 changed files with 346 additions and 62 deletions

View File

@ -6,6 +6,8 @@ use App\Middleware\Admin;
use App\Middleware\Guest;
use App\Middleware\NodeToken;
use App\Middleware\GemboxToken;
use App\Middleware\TgHookAdmin;
use App\Middleware\TgAdmin;
use App\Middleware\User;
use Slim\Routing\RouteCollectorProxy;
@ -14,6 +16,8 @@ return static function (Slim\App $app): void {
// test
$app->get('/my', App\Controllers\MyController::class . ':index');
// gembox 日后分离出去
$app->group('/gembox', static function (RouteCollectorProxy $group): void {
$group->get('', App\Controllers\Gembox\GemboxController::class . ':index');
@ -39,9 +43,12 @@ return static function (Slim\App $app): void {
})->add(new GemboxToken());
// TomApi\Tg
$app->post('/api/tg/hook', App\Controllers\TomApi\Tg\HookController::class . ':cmd')->add(new TgHookAdmin());
$app->group('/api/tg', static function (RouteCollectorProxy $group): void {
$group->any('/hook', App\Controllers\TomApi\Tg\HookController::class . ':cmd');
});
$group->put('/ticket/{id:[0-9]+}', App\Controllers\Admin\TicketController::class . ':update');
// $group->post('/ticket', App\Controllers\Admin\TicketController::class . ':update_tg');
})->add(new TgAdmin());
// test
$app->get('/test', App\Controllers\TestController::class . ':index');

87
public/tests/hook.php Normal file
View File

@ -0,0 +1,87 @@
<?php
include("include/head.php");
include("../../src/Services/TomTool/Http.php");
include("../../config/.config.php");
//use App\Services\TomTool\Http;
$oHttp = new App\Services\TomTool\Http();
$oHttp->sMethod = "post";
$oHttp->sToUrl = $_ENV['baseUrl']."/api/tg/hook";
$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' => '/reply 62 哈哈哈哈哈哈哈哈哈哈你麻痹'
]
];
$r = $oHttp->send();
var_dump($r);
exit;
$url = 'https://fuc.loc/api/tg/hook';
$data = [
'message' => [
'chat' => [
'id' => '-4668400285'
],
'text' => '/reply 11 sss'
]
];
$options = [
'http' => [
'header' => "Content-Type: application/json\r\n",
'method' => 'POST',
'content' => json_encode($data),
],
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
],
];
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result === FALSE) {
die('Error');
}
echo $result;
exit;
?>

View File

@ -0,0 +1,12 @@
<?php
error_reporting(E_ALL); // 报告所有错误
ini_set('display_errors', 1); // 显示错误
$k = $_GET['k'];
if ($k !== "wocaonide") {
exit('x');
}
?>

45
public/tests/test.php Normal file
View File

@ -0,0 +1,45 @@
<?php
include("include/head.php");
$url = 'https://fuc.loc/api/tg/hook';
$data = [
'message' => [
'chat' => [
'id' => '-4668400285'
],
'text' => '/reply 11 sss'
]
];
$options = [
'http' => [
'header' => "Content-Type: application/json\r\n",
'method' => 'POST',
'content' => json_encode($data),
],
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
],
];
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result === FALSE) {
die('Error');
}
echo $result;
exit;
?>

View File

@ -21,6 +21,8 @@ use Psr\Http\Message\ResponseInterface;
use Slim\Http\Response;
use Slim\Http\ServerRequest;
use App\Services\TomTool\Http;
final class HookController
{
@ -31,31 +33,23 @@ final class HookController
{
$this->sApiToken = Config::obtain('telegram_token');
$this->sApiChatId = Config::obtain('telegram_chatid');
$aData['chatId'] = $this->sApiChatId;
// 获取更新
@$update = json_decode(file_get_contents("php://input"), true);
$update = [
'message' => [
'chat' => [
'id' => '-4668400285'
],
'text' => '/reply 62 sss'
]
];
// 获取聊天 ID
// $chatId = $update["message"]["chat"]["id"];
// 获取消息文本
$message = $update["message"]["text"];
// 检查消息是否为 /test
if ($message === "/test") {
// 获取服务器时间
$serverTime = date("Y-m-d H:i:s");
// 发送消息到 Telegram
$this->sendMessage("服务器时间: " . $serverTime);
http_response_code(200);exit;
@ -69,40 +63,28 @@ final class HookController
// 使用空格分割内容
$responseParts = explode(' ', $responseText, 2); // 分割成两部分
if (count($responseParts) == 2) {
$number = $responseParts[0]; // 提取数字部分
$variableText = $responseParts[1]; // 提取用户输入的文本
// 构造回复内容
// $replyMessage = $number . $variableText; // 组合成 "11你好"
// 发送消息到 Telegram
// $this->sendMessage($replyMessage);
// use GuzzleHttp\Psr7\ServerRequest;
// use GuzzleHttp\Psr7\Utils;
// 创建一个包含请求体的 ServerRequest 实例
// $body = ['comment' => $variableText];
// $request = new ServerRequest('POST', '/ticket/$number', ['Content-Type' => 'application/json'], json_encode($body));
//
// // 创建一个 Response 实例
// $response = new Response(200, []);
//
// // 调用 update 方法
// $controller = new TicketController();
// $controller->update($request, $response, []);
$oHttp = new Http();
$oHttp->sMethod = "put";
$oHttp->sToUrl = $_ENV['baseUrl']."/api/tg/ticket/".$number;
$oHttp->bIsJson = true;
$aData['comment'] = 'hahahahah';
$oHttp->aData = $aData;
$r = @json_decode($oHttp->send(), true);
if ($r["ret"] == 1) {
$sMessage = "成功";
} else {
$sMessage = "失败";
}
$this->sendMessage($sMessage);
$request = new ServerRequest('POST', new \Slim\Http\Uri('http', 'localhost', null, '/comments/1'), [], [], [], $serverParams);
// $request = new ServerRequest();
$response = new Response();
// 设置参数
$request->merge(['comment' => $responseText]);
// 实例化控制器
$controller = new CommentController();
// 调用 update 方法
$response = $controller->update($request, $response, ['id' => $number]);
}
@ -110,21 +92,6 @@ final class HookController
http_response_code(200);exit;
}
// if ($message === "/reply") {
//
// // 创建一个包含请求体的 ServerRequest 实例
// $body = ['comment' => 'This is a comment.'];
// $request = new ServerRequest('PUT', '/ticket/', ['Content-Type' => 'application/json'], json_encode($body));
//
// // 创建一个 Response 实例
// $response = new Response();
//
// // 调用 update 方法
// $controller = new YourController();
// $controller->update($request, $response, []);
//
// }
http_response_code(200);exit;
}
@ -132,8 +99,12 @@ final class HookController
// 发送消息的函数
public function sendMessage($message) {
$url = "https://api.telegram.org/bot$this->sApiToken/sendMessage?chat_id=$this->sApiChatId&text=" . urlencode($message);
file_get_contents($url);
if ($_ENV['is_loc'] === true) {
var_dump($message);
} else {
$url = "https://api.telegram.org/bot$this->sApiToken/sendMessage?chat_id=$this->sApiChatId&text=" . urlencode($message);
file_get_contents($url);
}
}

40
src/Middleware/TgAdmin.php Executable file
View File

@ -0,0 +1,40 @@
<?php
declare(strict_types=1);
namespace App\Middleware;
//use App\Models\Node;
//use App\Services\RateLimit;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Slim\Http\ServerRequest;
//use RedisException;
use Slim\Factory\AppFactory;
use voku\helper\AntiXSS;
use App\Models\Config;
final class TgAdmin implements MiddlewareInterface
{
/**
* @throws RedisException
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$sChatId = $request->getParam('chatId');
$sChatIdConfig = Config::obtain('telegram_chatid');
if ($sChatIdConfig != $sChatId) {
echo json_encode([
'status' => 0,
'data' => 'key error'
]);exit;
}
return $handler->handle($request);
}
}

46
src/Middleware/TgHookAdmin.php Executable file
View File

@ -0,0 +1,46 @@
<?php
declare(strict_types=1);
namespace App\Middleware;
//use App\Models\Node;
//use App\Services\RateLimit;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
//use RedisException;
use Slim\Factory\AppFactory;
use voku\helper\AntiXSS;
use App\Models\Config;
final class TgHookAdmin implements MiddlewareInterface
{
/**
* @throws RedisException
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
@$update = json_decode(file_get_contents("php://input"), true);
$sChatIdConfig = Config::obtain('telegram_chatid');
$sChatId = @$update["message"]["chat"]["id"];
// if (@$update['chatId']) {
// $sChatId = @$update['chatId'];
// } else {
// $sChatId = @$update["message"]["chat"]["id"];
// }
if ($sChatIdConfig != $sChatId) {
echo json_encode([
'status' => 0,
'data' => 'key error'
]);exit;
}
return $handler->handle($request);
}
}

76
src/Services/TomTool/Http.php Executable file
View File

@ -0,0 +1,76 @@
<?php
declare(strict_types=1);
namespace App\Services\TomTool;
class Http
{
public $sMethod;
public $aData = [];
public $sToUrl;
public $bIsJson = false; // 默认值为 false
public function send(): string
{
// 初始化 cURL 会话
$ch = curl_init();
// 设置请求 URL
curl_setopt($ch, CURLOPT_URL, $this->sToUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 返回响应而不是输出
// 根据请求方法设置 cURL 选项
switch (strtolower($this->sMethod)) {
case 'post':
curl_setopt($ch, CURLOPT_POST, true); // 设置为 POST 请求
if ($this->bIsJson) {
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($this->aData)); // 设置 POST 数据为 JSON
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json', // 设置内容类型为 JSON
'Content-Length: ' . strlen(json_encode($this->aData)) // 设置内容长度
]);
} else {
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($this->aData)); // 设置 POST 数据为普通表单格式
}
break;
case 'put':
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); // 设置为 PUT 请求
if ($this->bIsJson) {
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($this->aData)); // 设置 PUT 数据为 JSON
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json', // 设置内容类型为 JSON
'Content-Length: ' . strlen(json_encode($this->aData)) // 设置内容长度
]);
} else {
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($this->aData)); // 设置 PUT 数据为普通表单格式
}
break;
default:
// 默认使用 GET 请求
curl_setopt($ch, CURLOPT_HTTPGET, true); // 设置为 GET 请求
curl_setopt($ch, CURLOPT_URL, $this->sToUrl . '?' . http_build_query($this->aData)); // 设置请求 URL
break;
}
// 禁用 SSL 验证
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 不验证对等证书
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 不验证主机名
// 执行 cURL 请求
$response = curl_exec($ch);
// 检查是否有错误
if (curl_errno($ch)) {
return 'Error: ' . curl_error($ch);
}
// 关闭 cURL 会话
curl_close($ch);
// 返回响应
return $response;
}
}