|
|
@@ -334,31 +334,64 @@ class TelegramChannelService
|
|
|
}
|
|
|
|
|
|
public function privateMenu(int $id, string $language): array
|
|
|
+ {
|
|
|
+ return $this->privateMenus($id)[$language] ?? ['reply' => '', 'buttons' => [], 'type' => 'text', 'image' => '', 'video' => ''];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function privateMenus(int $id): array
|
|
|
{
|
|
|
$menus = json_decode($this->findBot($id)->private_menus ?? '{}', true);
|
|
|
- return $menus[$language] ?? ['reply' => '', 'buttons' => []];
|
|
|
+ foreach ($menus as &$menu) {
|
|
|
+ $menu += ['type' => 'text', 'image' => '', 'video' => ''];
|
|
|
+ }
|
|
|
+ return $menus;
|
|
|
}
|
|
|
|
|
|
public function savePrivateMenu(int $id, string $language, array $input): array
|
|
|
{
|
|
|
- $menu = Validator::make($input, [
|
|
|
- 'reply' => 'required|string|max:4096',
|
|
|
- 'buttons' => 'present|array|list|max:20',
|
|
|
- 'buttons.*' => 'required|array|list',
|
|
|
- 'buttons.*.*' => 'required|string|max:128',
|
|
|
- ])->validate();
|
|
|
+ return $this->savePrivateMenus($id, [$language => $input])[$language];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function savePrivateMenus(int $id, array $input): array
|
|
|
+ {
|
|
|
+ $menus = [];
|
|
|
+ foreach ($input as $language => $menu) {
|
|
|
+ $menus[$language] = $this->validatePrivateMenu($menu);
|
|
|
+ }
|
|
|
|
|
|
- return DB::transaction(function () use ($id, $language, $menu) {
|
|
|
+ DB::transaction(function () use ($id, $menus) {
|
|
|
$bot = DB::table('telegram_bots')->where('id', $id)->lockForUpdate()->first()
|
|
|
?? throw ValidationException::withMessages(['id' => '机器人不存在']);
|
|
|
- $menus = json_decode($bot->private_menus ?? '{}', true);
|
|
|
- $menus[$language] = $menu;
|
|
|
+ $existing = json_decode($bot->private_menus ?? '{}', true);
|
|
|
DB::table('telegram_bots')->where('id', $id)->update([
|
|
|
- 'private_menus' => json_encode($menus, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE),
|
|
|
+ 'private_menus' => json_encode(array_replace($existing, $menus), JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE),
|
|
|
'updated_at' => now(),
|
|
|
]);
|
|
|
- return $menu;
|
|
|
}, 3);
|
|
|
+ return $menus;
|
|
|
+ }
|
|
|
+
|
|
|
+ private function validatePrivateMenu(array $input): array
|
|
|
+ {
|
|
|
+ $menu = Validator::make($input, [
|
|
|
+ 'buttons' => 'present|array|list|max:20',
|
|
|
+ 'buttons.*' => 'required|array|list',
|
|
|
+ 'buttons.*.*' => 'required|string|max:128',
|
|
|
+ ])->validate();
|
|
|
+ $content = self::message([
|
|
|
+ 'type' => $input['type'] ?? 'text',
|
|
|
+ 'text' => $input['reply'] ?? null,
|
|
|
+ 'image' => $input['image'] ?? null,
|
|
|
+ 'video' => $input['video'] ?? null,
|
|
|
+ ]);
|
|
|
+ $menu += [
|
|
|
+ 'type' => $content['type'],
|
|
|
+ 'reply' => $content['text'],
|
|
|
+ 'image' => $content['type'] === 'image' ? $content['image'] : '',
|
|
|
+ 'video' => $content['type'] === 'video' ? $content['video'] : '',
|
|
|
+ ];
|
|
|
+
|
|
|
+ return $menu;
|
|
|
}
|
|
|
|
|
|
public function registerPrivateWebhook(int $id): array
|
|
|
@@ -456,9 +489,8 @@ class TelegramChannelService
|
|
|
$keyboard = $menu['buttons'] === []
|
|
|
? ['remove_keyboard' => true]
|
|
|
: ['keyboard' => $menu['buttons'], 'resize_keyboard' => true];
|
|
|
- $telegram->sendMessage([
|
|
|
+ $this->send($telegram, ['text' => $menu['reply']] + $menu + ['type' => 'text'], [
|
|
|
'chat_id' => $chatId,
|
|
|
- 'text' => $menu['reply'],
|
|
|
'reply_markup' => json_encode($keyboard, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE),
|
|
|
]);
|
|
|
}
|