|
|
@@ -20,6 +20,24 @@ class TelegramChannelMessageService
|
|
|
}
|
|
|
|
|
|
public function create(array $input): array
|
|
|
+ {
|
|
|
+ return $this->createMessage($input);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function resend(int $id): array
|
|
|
+ {
|
|
|
+ $original = DB::table(self::TABLE)->find($id);
|
|
|
+ if ($original === null || !in_array($original->status, ['sent', 'failed', 'unknown', 'cancelled'], true)) {
|
|
|
+ throw ValidationException::withMessages(['id' => '该记录无法重新发送']);
|
|
|
+ }
|
|
|
+
|
|
|
+ return $this->createMessage([
|
|
|
+ 'channel_id' => $original->channel_id,
|
|
|
+ 'is_top' => (bool) $original->is_top,
|
|
|
+ ] + json_decode($original->message, true, 512, JSON_THROW_ON_ERROR), $original->target);
|
|
|
+ }
|
|
|
+
|
|
|
+ private function createMessage(array $input, ?string $originalTarget = null): array
|
|
|
{
|
|
|
$fields = Validator::make($input, [
|
|
|
'channel_id' => 'required|integer|min:1',
|
|
|
@@ -28,6 +46,10 @@ class TelegramChannelMessageService
|
|
|
])->validate();
|
|
|
$message = TelegramChannelService::message($input);
|
|
|
$channel = $this->channels->detail((int) $fields['channel_id']);
|
|
|
+ $target = $channel['telegram_chat_id'] ?? $channel['chatId'];
|
|
|
+ if ($originalTarget !== null && $target !== $originalTarget) {
|
|
|
+ throw ValidationException::withMessages(['channel_id' => '频道标识已变更,请重新创建发送记录']);
|
|
|
+ }
|
|
|
if (!$channel['enabled']) {
|
|
|
throw ValidationException::withMessages(['channel_id' => '请先启用频道']);
|
|
|
}
|
|
|
@@ -42,7 +64,7 @@ class TelegramChannelMessageService
|
|
|
$id = (int) DB::table(self::TABLE)->insertGetId([
|
|
|
'channel_id' => $channel['id'],
|
|
|
'channel_name' => $channel['name'],
|
|
|
- 'target' => $channel['telegram_chat_id'] ?? $channel['chatId'],
|
|
|
+ 'target' => $target,
|
|
|
'message' => json_encode($message, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE),
|
|
|
'is_top' => $fields['is_top'] ?? false,
|
|
|
'status' => 'scheduled',
|