Files
MAX/n8n-code-add-menu-buttons.js

68 lines
2.4 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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.

// Code node (Run once for each item)
// Смотрит на answer_text (payload нажатой кнопки). Если "complaint" — экран "Подать жалобу" (текст + Назад).
// Иначе — главное меню (выбор действий + контакт + Главное меню).
return items.map(item => {
const payload = (item.json?.answer_text || '').trim();
let text;
let message_body;
if (payload === 'complaint') {
// Человек нажал "Подать жалобу" — показываем текст шага и кнопку "Назад"
text = "**Подача жалобы**\n\nОпишите ситуацию и приложите документы, если есть. Мы рассмотрим обращение в кратчайшие сроки.\n\nНапишите сообщение с описанием или нажмите «Назад», чтобы вернуться в меню.";
const backRow = [ { type: "callback", text: "◀️ Назад", payload: "menu" } ];
message_body = {
text,
format: "markdown",
attachments: [
{
type: "inline_keyboard",
payload: { buttons: [ backRow ] }
}
]
};
return {
json: {
...item.json,
text,
message_body,
screen: "complaint"
}
};
}
// Главное меню
text = "Вас давно не было. Выберите, чем хотите заняться:";
const buttons = [
{ title: " О сервисе", payload: "about" },
{ title: "📝 Подать жалобу", payload: "complaint" },
{ title: "📋 Мои обращения", payload: "my_tickets" },
{ title: "💬 Поддержка", payload: "support" }
];
const callbackRows = buttons.map(b => [ { type: "callback", text: b.title, payload: b.payload } ]);
const menuButtonRow = [ { type: "message", text: "📋 Главное меню" } ];
const contactButtonRow = [ { type: "request_contact", text: "📱 Отправить номер телефона" } ];
message_body = {
text,
format: "markdown",
attachments: [
{
type: "inline_keyboard",
payload: {
buttons: [ ...callbackRows, contactButtonRow, menuButtonRow ]
}
}
]
};
return {
json: {
...item.json,
text,
buttons,
message_body,
screen: "main"
}
};
});