284 lines
9.7 KiB
PHP
Executable File
284 lines
9.7 KiB
PHP
Executable File
<?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;
|
|
|
|
}
|
|
|
|
}
|