no message

This commit is contained in:
hellcat 2025-04-13 22:09:03 +08:00
parent e988fdaa83
commit 2b277a2d86
5 changed files with 22 additions and 372 deletions

View File

@ -1,283 +0,0 @@
<?php
declare(strict_types=1);
namespace App\Http\Controllers\Api\Tg;
use App\Models\Article;
//use App\Models\TgStep;
//use App\Models\Ticket;
//use App\Models\Node;
//use App\Models\User;
//use App\Models\Order;
//use App\Models\Ducks;
//use App\Models\Zhuanfas;
//use App\Services\Tg\ReportUser as ServiceTgReportUser;
//use GuzzleHttp\Exception\GuzzleException;
//use Psr\Http\Client\ClientExceptionInterface;
//use Psr\Http\Message\ResponseInterface;
//use Slim\Http\Response;
//use Slim\Http\ServerRequest;
//use App\Utils\Tools;
use App\Services\TomTool\Http;
final class HookController
{
public $sApiToken;
public $sApiChatId;
public function __construct()
{
// $this->middleware('csrf')->except(['cmd']);
// 或者 $this->middleware('csrf')->only(['specificMethod']);
}
public function cmd()
{
try {
$this->sApiToken = env('telegram_token');
$this->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'] = $aUpdate["message"]["chat"]["id"];
$aData['fromId'] = $aUpdate["message"]["from"]["id"];
if (!isset($aUpdate["message"]["text"])) {
http_response_code(200);
echo "not have text";
exit;
}
$sText = $aUpdate["message"]["text"];
// 打断
if ($sText === "/break") {
$oTgStep->clearByFromId($aData["fromId"]);
$this->sendMessage("ok");
Http::response(200, 'ok');
}
// 命令列表
// /cmdList
// if (strpos($sText, '/cmdList') === 0 || strpos($sText, '\/cmdList') === 0) {
//
// $aCmdList = [
// '优惠码list' => '/saleCodeList',
// '优惠码find' => '/saleCodeFind xxhh',
// '优惠码rate' => '/saleCodeSetRate__xxzz 6.7',
// // '优惠码add' => '/saleCodeAdd xxhh',
// // '优惠码del' => '/saleCodeDel__xxhh',
// // '优惠码set' => '/saleCodeSet__xxhh__field set',
// '文章list' => '/articleList',
// '文章like' => '/articleLike__field like',
// '文章add' => '/articleAdd__name',
// '文章del' => '/articleDel__id',
// '文章set' => '/articleSet__id__fild set',
// ];
//
// $this->sendMessage(json_encode($aCmdList, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
//
// Http::response(200, 'ok');
}
// 优惠设置幅度(批量)
// if (strpos($sText, '/saleCodeSetRate') === 0 || strpos($sText, '\/saleCodeSetRate') === 0) {
//
// list($sCmd, $iRate) = explode(" ", $sText);
// list(, $sCode) = explode("__", $sCmd);
//
// $updatedRows = Article::where('sale_code', $sCode)
// ->update(['sale_rate' => $iRate]);
//
// $this->sendMessage(json_encode($updatedRows, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
//
// Http::response(200, 'ok');
// }
// 优惠list
// if (strpos($sText, '/saleCodeList') === 0 || strpos($sText, '\/saleCodeList') === 0) {
//
// $aSaleCodeList = Article::select('sale_code', 'sale_rate')
// ->distinct()
// ->orderBy("sale_code")
// ->get()
// ->toArray();
//
// $this->sendMessage(json_encode($aSaleCodeList, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
//
// Http::response(200, 'ok');
// }
// 优惠find
// if (strpos($sText, '/saleCodeFind') === 0 || strpos($sText, '\/saleCodeFind') === 0) {
//
// list(, $sSaleCode) = explode(" ", $sText);
//
// $aSaleCodeList = Article::select('sale_code', 'sale_rate')
// ->where("sale_code", $sSaleCode)
// ->distinct()
// ->orderBy("sale_code")
// ->get()
// ->toArray();
//
// $this->sendMessage(json_encode($aSaleCodeList, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
//
// Http::response(200, 'ok');
//
// }
// 文章list
// if (strpos($sText, '/ArticleList') === 0 || strpos($sText, '\/ArticleList') === 0) {
//
// $aArticleList = Article::orderBy("user_name")->get()->toArray();
//
// foreach ($aArticleList as $row) {
// $this->sendMessage(json_encode($row, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
// }
//
// Http::response(200, 'ok');
// }
// 文章 like
// /ArticleLike__user_name xxx
// if (strpos($sText, '/ArticleLike') === 0 || strpos($sText, '\/ArticleLike') === 0) {
//
// list($sCmd, $sLike) = explode(" ", $sText);
// list(, $sField) = explode("__", $sCmd);
//
// $aArticleList = Article::where($sField, 'like', '%' . $sLike . '%')
// ->orderBy("user_name")
// ->get()
// ->toArray();
//
//
// foreach ($aArticleList as $row) {
// $this->sendMessage(json_encode($row, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
// }
//
// Http::response(200, 'ok');
// }
// 文章 add
// if (strpos($sText, '/articleAdd') === 0 || strpos($sText, '\/articleAdd') === 0) {
//
// list(, $sUserName) = explode(" ", $sText);
//
// $newArticle = new Article();
// $newArticle->user_name = $sUserName;
// $r = $newArticle->save();
//
// $this->sendMessage(json_encode($r, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
//
// Http::response(200, 'ok');
//
// }
// 文章 del
// if (strpos($sText, '/articleDel') === 0 || strpos($sText, '\/articleDel') === 0) {
//
// list(, $iId) = explode(" ", $sText);
//
// $article = Article::find($iId);
//
// if ($article) {
// $r = $article->delete();
// } else {
// $r = false;
// }
//
// $this->sendMessage(json_encode($r, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
//
// Http::response(200, 'ok');
//
// }
// 文章 set
// /ArticleSet__id__user_name xxx
// if (strpos($sText, '/ArticleSet') === 0 || strpos($sText, '\/ArticleSet') === 0) {
//
// list($sCmd, $sSet) = explode(" ", $sText);
// list(, $iId, $sField) = explode("__", $sCmd, 3);
// $sField = $this->cmdGood($sField);
//
// $article = Article::find($iId);
//
// if ($article) {
// $article->$sField = $sSet;
//
// $r = $article->save();
// } else {
// // 处理未找到的情况
// throw new Exception("Article not found with ID: $iId");
// }
//
// $this->sendMessage(json_encode($r, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
//
// Http::response(200, 'ok');
// }
} catch (\Exception $e) {
// 处理异常
$errorMessage = $e->getMessage();
$this->sendMessage(json_encode(['error' => $errorMessage], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
Http::response(200, '不对');
}
}
private function sendMessage($sText = '?') {
if (env('is_loc') === true) {
echo "<pre>";
var_dump($sText);
} else {
$url = "https://api.telegram.org/bot$this->sApiToken/sendMessage?chat_id=$this->sApiChatId&text=" . urlencode($sText);
file_get_contents($url);
}
}
private function cmdGood($sCmd) {
$sCmdNew = "";
switch ($sCmd) {
case "user":
$sCmdNew = "user_name";
break;
case "project":
$sCmdNew = "project_name";
break;
case "code":
$sCmdNew = "sale_code";
break;
case "condition":
$sCmdNew = "cron_condition";
break;
default:
$sCmdNew = $sCmd;
break;
}
return $sCmdNew;
}
}

View File

@ -43,12 +43,12 @@ final class HookController
$sText = $aUpdate["message"]["text"]; $sText = $aUpdate["message"]["text"];
// test // test
$sText = "\\//Article#List ni__aaa__bbb 111 222"; // $sText = "\\//Article#List ni__aaa__bbb 111 222";
$sText = "///\\\SaleCode#SetRate__zzxx 2"; // $sText = "///\\\SaleCode#SetRate__zzxx 2";
$sText = "///\\\SaleCode#List"; // $sText = "///\\\SaleCode#List";
$sText = "///\\\SaleCode#Find zzxx"; // $sText = "///\\\SaleCode#Find zzxx";
$sText = "/Article#Like__sale_code zzx"; // $sText = "/Article#Like__sale_code zzx";
$sText = "////Article#Add jxxjjjj"; // $sText = "////Article#Add jxxjjjj";
// $sText = "//\\\//Article#Del 9"; // $sText = "//\\\//Article#Del 9";
// $sText = "/Article#Set__1__user_name xxx"; // $sText = "/Article#Set__1__user_name xxx";

View File

@ -13,6 +13,19 @@ class TestsController
public function hook() public function hook()
{ {
@$k = $_GET["k"];
@$s = $_GET["s"];
if ($k !== "wocaonide") {
exit("?");
}
$s = '/'.$s;
//test
// $s = "\/AticleSet__1115zz__sale_rate 1118.118";
$s = "\/Cmd#List";
$oHttp = new Http(); $oHttp = new Http();
$oHttp->sMethod = "post"; $oHttp->sMethod = "post";
$oHttp->sToUrl = env("APP_URL")."/api/tg/hook"; $oHttp->sToUrl = env("APP_URL")."/api/tg/hook";
@ -37,7 +50,7 @@ class TestsController
'type' => 'private' 'type' => 'private'
], ],
'date' => 1610000000, 'date' => 1610000000,
"text" => "\/ArticleSet__5__sale_rate 1118.118", "text" => $s,
] ]
]; ];

View File

@ -4,7 +4,7 @@ use Illuminate\Http\Request;
define('LARAVEL_START', microtime(true)); define('LARAVEL_START', microtime(true));
function tomd($d, $type) function tomd($d, $type = 0)
{ {
echo "<pre>"; echo "<pre>";
var_dump($d); var_dump($d);

View File

@ -36,35 +36,7 @@ $oHttp->aData = [
"text" => "\/saleCodeList", "text" => "\/saleCodeList",
] ]
]; ];
//echo 1111;exit;
//{
// "update_id": 761180998,
// "message": {
// "message_id": 1159,
// "from": {
// "id": 7740568271,
// "is_bot": false,
// "first_name": "sakai",
// "username": "gemcode_business",
// "language_code": "zh-hans"
// },
// "chat": {
// "id": -4668400285,
// "title": "FUUU",
// "type": "group",
// "all_members_are_administrators": true
// },
// "date": 1743503707,
// "text": "\/userFind 1",
// "entities": [
// {
// "offset": 0,
// "length": 9,
// "type": "bot_command"
// }
// ]
// }
//}
$r = $oHttp->send(); $r = $oHttp->send();
@ -72,56 +44,4 @@ echo($r);
exit; exit;
$url = 'https://fuc.loc/api/tg/hook';
$url = 'https://fuc.loc/test3';
$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;
?> ?>