|
|
@@ -225,6 +225,9 @@ class TelegramChannelService
|
|
|
}
|
|
|
|
|
|
$messageId = $response['message_id'] ?? null;
|
|
|
+ if (isset($response['body_error'])) {
|
|
|
+ return ['sent' => null, 'pinned' => null, 'message_id' => $messageId, 'error' => $response['body_error']];
|
|
|
+ }
|
|
|
if ($messageId === null) {
|
|
|
return ['sent' => null, 'pinned' => null, 'message_id' => null, 'error' => '未能确认发布结果,请先检查频道'];
|
|
|
}
|
|
|
@@ -252,10 +255,31 @@ class TelegramChannelService
|
|
|
return $telegram->post('sendMessage', $params + ['text' => $channel['text']])->getResult() ?: [];
|
|
|
}
|
|
|
|
|
|
+ $text = $channel['text'];
|
|
|
+ $separateBody = mb_strlen($text) > 1024;
|
|
|
+ $response = $this->sendMedia($telegram, $channel, $params + ['caption' => $separateBody ? '' : $text]);
|
|
|
+ if ($separateBody && isset($response['message_id'])) {
|
|
|
+ try {
|
|
|
+ $body = $telegram->post('sendMessage', [
|
|
|
+ 'chat_id' => $params['chat_id'],
|
|
|
+ 'text' => $text,
|
|
|
+ ])->getResult();
|
|
|
+ if (isset($body['message_id'])) {
|
|
|
+ return $response;
|
|
|
+ }
|
|
|
+ } catch (\Exception) {
|
|
|
+ // The media was already sent. Do not retry the entire message.
|
|
|
+ }
|
|
|
+ $response['body_error'] = '媒体已发送,正文发送结果未确认,请在 Telegram 中核对';
|
|
|
+ }
|
|
|
+ return $response;
|
|
|
+ }
|
|
|
+
|
|
|
+ private function sendMedia(Api $telegram, array $channel, array $params): array
|
|
|
+ {
|
|
|
$image = $channel['type'] === 'image';
|
|
|
$media = $channel[$channel['type']];
|
|
|
$field = $image ? 'photo' : 'video';
|
|
|
- $params['caption'] = $channel['text'];
|
|
|
|
|
|
// Only read files produced by the admin upload endpoints on the configured public disk.
|
|
|
$disk = Storage::disk('public');
|
|
|
@@ -489,10 +513,13 @@ class TelegramChannelService
|
|
|
$keyboard = $menu['buttons'] === []
|
|
|
? ['remove_keyboard' => true]
|
|
|
: ['keyboard' => $menu['buttons'], 'resize_keyboard' => true];
|
|
|
- $this->send($telegram, ['text' => $menu['reply']] + $menu + ['type' => 'text'], [
|
|
|
+ $response = $this->send($telegram, ['text' => $menu['reply']] + $menu + ['type' => 'text'], [
|
|
|
'chat_id' => $chatId,
|
|
|
'reply_markup' => json_encode($keyboard, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE),
|
|
|
]);
|
|
|
+ if (isset($response['body_error'])) {
|
|
|
+ Log::warning('Telegram 私聊菜单正文发送结果未确认', ['message_id' => $response['message_id']]);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
private function findBot(int $id): object
|
|
|
@@ -593,7 +620,7 @@ class TelegramChannelService
|
|
|
{
|
|
|
$data = Validator::make($input, [
|
|
|
'type' => 'required|in:text,image,video',
|
|
|
- 'text' => 'nullable|required_if:type,text|string|max:' . (($input['type'] ?? null) === 'text' ? 4096 : 1024),
|
|
|
+ 'text' => 'nullable|required_if:type,text|string|max:4096',
|
|
|
'image' => 'nullable|required_if:type,image|string|max:2048|url:http,https',
|
|
|
'video' => 'nullable|required_if:type,video|string|max:2048|url:http,https',
|
|
|
'button' => 'sometimes|array|list',
|