140 lines
3.3 KiB
PHP
Executable File
140 lines
3.3 KiB
PHP
Executable File
<?php
|
||
|
||
declare(strict_types=1);
|
||
|
||
namespace App\Services\TomTool\Telegram;
|
||
|
||
use App\Services\HttpQueue as ServiceHttpQueue;
|
||
use App\Services\TomTool\Flow;
|
||
use App\Services\TomTool\Telegram\Sdk as ServiceTelegramSdk;
|
||
use App\Models\TelegramKey as ModelTelegramKey;
|
||
|
||
/**
|
||
|
||
注意是master服务器端专用的,这里会对code找map,slave不会。
|
||
|
||
new:
|
||
start(?$sBodeCode, ?$sChatCode)
|
||
|
||
set:
|
||
setBotCode()
|
||
setChatCode()
|
||
setBotToken()
|
||
setChatId()
|
||
setMsgS()
|
||
|
||
action:
|
||
send()
|
||
|
||
*/
|
||
|
||
final class Master
|
||
{
|
||
public string $sBotCode = "base";
|
||
public string $sChatCode = "base";
|
||
public string $sBotToken = '';
|
||
public int $iChatId = 0;
|
||
|
||
public string $sMsg = '';
|
||
|
||
public bool $enableQueue = false; // 应该用不到了
|
||
|
||
public bool $bKeyBegin = false;
|
||
|
||
public static function start($sBotCode = 'base', $sChatCode = 'base'): self
|
||
{
|
||
$oInstance = new self();
|
||
$oInstance->sBotCode = $sBotCode;
|
||
$oInstance->sChatCode = $sChatCode;
|
||
return $oInstance;
|
||
}
|
||
|
||
public function setBotCode(string $sBotCode): self
|
||
{
|
||
$this->sBotCode = $sBotCode;
|
||
return $this;
|
||
}
|
||
|
||
public function setChatCode(string $sChatCode): self
|
||
{
|
||
$this->sChatCode = $sChatCode;
|
||
return $this;
|
||
}
|
||
|
||
public function setBotToken(string $sBotToken): self
|
||
{
|
||
$this->sBotToken = $sBotToken;
|
||
return $this;
|
||
}
|
||
|
||
public function setChatId(int $iChatId): self
|
||
{
|
||
$this->iChatId = $iChatId;
|
||
return $this;
|
||
}
|
||
|
||
public function setMsgS(string $sMsg): self
|
||
{
|
||
$this->sMsg = $sMsg;
|
||
return $this;
|
||
}
|
||
|
||
public function send($sMsg = ''): Flow
|
||
{
|
||
if ($sMsg) {
|
||
$this->sMsg = $sMsg;
|
||
}
|
||
|
||
if (!$this->sMsg) {
|
||
throw new \Exception("未设置msg");
|
||
}
|
||
|
||
$this->keyBegin();
|
||
|
||
if ($this->enableQueue) {
|
||
$aData = $this->buildDataTom();
|
||
$oFlow = ServiceHttpQueue::start("telegram_local")->arg($aData)->save();
|
||
} else {
|
||
$oFlow = ServiceTelegramSdk::start()
|
||
->setBotToken($this->sBotToken)
|
||
->setChatId($this->iChatId)
|
||
->setMsgS($this->sMsg)
|
||
->send();
|
||
}
|
||
|
||
return $oFlow;
|
||
}
|
||
|
||
private function keyBegin()
|
||
{
|
||
if (!$this->bKeyBegin) {
|
||
if (!$this->sBotToken) {
|
||
$this->sBotToken = ModelTelegramKey::botTokenByCode($this->sBotCode);
|
||
}
|
||
|
||
if (!$this->iChatId) {
|
||
$this->iChatId = ModelTelegramKey::chatIdByCode($this->sChatCode);
|
||
}
|
||
|
||
if (!$this->sBotToken || !$this->iChatId) {
|
||
throw new \RuntimeException("botToken或chatId没找到,botCode={$this->sBotCode},chatCode={$this->sChatCode}");
|
||
}
|
||
|
||
$this->bKeyBegin = true;
|
||
}
|
||
}
|
||
|
||
private function buildDataTom(): array
|
||
{
|
||
return [
|
||
"sBotCode" => $this->sBotCode ?? '',
|
||
"sChatCode" => $this->sChatCode ?? '',
|
||
"sBotToken" => $this->sBotToken ?? '',
|
||
"iChatId" => $this->iChatId ?? '',
|
||
"sMsg" => $this->sMsg ?? '',
|
||
];
|
||
}
|
||
|
||
|
||
}
|