115 lines
3.3 KiB
PHP
Executable File
115 lines
3.3 KiB
PHP
Executable File
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Controllers\TomApi\Tg;
|
|
|
|
//use App\Controllers\BaseController;
|
|
//use Psr\Http\Message\ResponseInterface;
|
|
//use Slim\Http\Response;
|
|
//use Slim\Http\ServerRequest;
|
|
|
|
use App\Models\Config;
|
|
//use GuzzleHttp\Psr7\ServerRequest;
|
|
//use GuzzleHttp\Psr7\Utils;
|
|
//use Psr\Http\Message\ResponseInterface;
|
|
//use Slim\Http\Response;
|
|
use Exception;
|
|
use GuzzleHttp\Exception\GuzzleException;
|
|
use Psr\Http\Client\ClientExceptionInterface;
|
|
use Psr\Http\Message\ResponseInterface;
|
|
use Slim\Http\Response;
|
|
use Slim\Http\ServerRequest;
|
|
|
|
use App\Services\TomTool\Http;
|
|
|
|
final class HookController
|
|
{
|
|
|
|
public $sApiToken;
|
|
public $sApiChatId;
|
|
|
|
public function cmd()
|
|
{
|
|
$this->sApiToken = Config::obtain('telegram_token');
|
|
$this->sApiChatId = Config::obtain('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"];
|
|
|
|
if (!isset($aUpdate["message"]["text"])) {
|
|
http_response_code(200);
|
|
echo "not have text";
|
|
exit;
|
|
}
|
|
|
|
$sText = $aUpdate["message"]["text"];
|
|
|
|
if ($sText === "/test") {
|
|
|
|
$serverTime = date("Y-m-d H:i:s");
|
|
|
|
$this->sendMessage("服务器时间: " . $serverTime);
|
|
|
|
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'] = $aData["message"]["from"]["first_name"];
|
|
$oHttp->aData = $aData;
|
|
|
|
$r = @json_decode($oHttp->send(), true);
|
|
|
|
if ($r["ret"] == 1) {
|
|
$sMessage = "成功";
|
|
} else {
|
|
$sMessage = "失败";
|
|
}
|
|
|
|
$this->sendMessage($sMessage);
|
|
|
|
}
|
|
|
|
http_response_code(200);exit;
|
|
}
|
|
|
|
http_response_code(200);exit;
|
|
|
|
}
|
|
|
|
// 发送消息的函数
|
|
public function sendMessage($sText) {
|
|
|
|
if ($_ENV['is_loc'] === true) {
|
|
var_dump($sText);
|
|
} else {
|
|
$url = "https://api.telegram.org/bot$this->sApiToken/sendMessage?chat_id=$this->sApiChatId&text=" . urlencode($sText);
|
|
file_get_contents($url);
|
|
}
|
|
|
|
}
|
|
|
|
}
|