157 lines
5.3 KiB
PHP
Executable File
157 lines
5.3 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 __construct()
|
||
{
|
||
// $this->middleware('csrf')->except(['cmd']);
|
||
// 或者 $this->middleware('csrf')->only(['specificMethod']);
|
||
}
|
||
|
||
|
||
public function cmd()
|
||
{
|
||
|
||
try {
|
||
|
||
$oTGHttp = new TGHttp();
|
||
$oTGHttp->sApiToken = env('telegram_token');
|
||
$oTGHttp->sApiChatId = env('telegram_chatid');
|
||
|
||
// 获取Telegram发送的JSON数据
|
||
$content = file_get_contents("php://input");
|
||
$update = json_decode($content, true);
|
||
|
||
// 检查是否是inline查询
|
||
if (isset($update['inline_query'])) {
|
||
$inline_query_id = $update['inline_query']['id'];
|
||
$query = $update['inline_query']['query'];
|
||
|
||
// 处理查询并生成结果
|
||
$results = [];
|
||
if (!empty($query)) {
|
||
// 这里可以根据查询生成结果
|
||
$results[] = [
|
||
'type' => 'article',
|
||
'id' => '1',
|
||
'title' => '结果 1',
|
||
'input_message_content' => [
|
||
'message_text' => '你查询的内容是: ' . htmlspecialchars($query)
|
||
],
|
||
'description' => '这是第一个结果的描述',
|
||
// 'thumb_url' => 'https://example.com/image1.jpg' // 可选缩略图
|
||
];
|
||
$results[] = [
|
||
'type' => 'article',
|
||
'id' => '2',
|
||
'title' => '结果 2',
|
||
'input_message_content' => [
|
||
'message_text' => '这是另一个结果: ' . htmlspecialchars($query)
|
||
],
|
||
'description' => '这是第二个结果的描述',
|
||
// 'thumb_url' => 'https://example.com/image2.jpg' // 可选缩略图
|
||
];
|
||
}
|
||
|
||
// 发送结果回Telegram
|
||
$response = [
|
||
'inline_query_id' => $inline_query_id,
|
||
'results' => json_encode($results, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT)
|
||
];
|
||
|
||
// 使用file_get_contents发送请求
|
||
// $url = "https://api.telegram.org/bot<YOUR_BOT_TOKEN>/answerInlineQuery?" . http_build_query($response);
|
||
// file_get_contents($url);
|
||
|
||
$oTGHttp->sendToTG(json_encode($response));
|
||
|
||
Http::response(200, 'error');
|
||
|
||
}
|
||
|
||
|
||
exit;
|
||
$oTGHttp = new TGHttp();
|
||
$oTGHttp->sApiToken = env('telegram_token');
|
||
$oTGHttp->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"])) {
|
||
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();
|
||
$r = $oInstans->$sFunc($aParams);
|
||
|
||
} else {
|
||
throw new \Exception("没有这个方法");
|
||
}
|
||
|
||
} catch (\Throwable $e) {
|
||
$sMessage = "错误: " . $e->getMessage();
|
||
|
||
$oTGHttp->sendToTG($sMessage);
|
||
|
||
Http::response(200, 'error');
|
||
}
|
||
}
|
||
|
||
private function cleanCmd($input) {
|
||
// 去掉所有的反斜杠
|
||
$input = str_replace('\\', '', $input);
|
||
// 去掉所有的正斜杠
|
||
$input = str_replace('/', '', $input);
|
||
|
||
return $input;
|
||
}
|
||
|
||
}
|