DispelEvilBot/sql/V21__seed_welcome_sequence.sql

38 lines
1.8 KiB
SQL
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

-- Переносим текущее приветствие в последовательность `welcome`:
-- 1) короткая подпись к фото, 2) остальной текст одним сообщением с кнопкой «Канал».
INSERT INTO bot.message_sequences (key, title, description)
VALUES ('welcome', 'Приветствие', 'Сообщения при входе участника в чат')
ON CONFLICT (key) DO NOTHING;
INSERT INTO bot.message_sequence_items (sequence_key, position, text, image_name)
SELECT 'welcome', 0, split_part(t.text, E'\n', 1), 'welcome_image.jpg'
FROM bot.message_templates AS t
WHERE t.key = 'welcome.text'
ON CONFLICT (sequence_key, position) DO NOTHING;
INSERT INTO bot.message_sequence_items (sequence_key, position, text)
SELECT
'welcome',
1,
trim(both E'\n' from (
substr(src.text, strpos(src.text, E'\n') + 1)
|| E'\n\n' || coalesce(t2.text, '')
|| E'\n\n' || coalesce(t3.text, '')
))
FROM bot.message_templates AS src
LEFT JOIN bot.message_templates AS t2 ON t2.key = 'welcome.text2'
LEFT JOIN bot.message_templates AS t3 ON t3.key = 'welcome.text3'
WHERE src.key = 'welcome.text'
ON CONFLICT (sequence_key, position) DO NOTHING;
INSERT INTO bot.message_sequence_buttons (item_id, position, text, url)
SELECT i.id, 0, 'Канал', 'https://t.me/+DQk8MyN2y6QzNTM6'
FROM bot.message_sequence_items AS i
WHERE i.sequence_key = 'welcome'
AND i.position = 1
AND NOT EXISTS (SELECT 1 FROM bot.message_sequence_buttons AS b WHERE b.item_id = i.id);
-- Старые шаблоны приветствия больше не редактируются: источник правды — последовательность.
DELETE FROM bot.message_templates
WHERE key IN ('welcome.text', 'welcome.text2', 'welcome.text3');