Save all currently accumulated repository changes as a backup snapshot for Gitea so no local work is lost.
73 lines
2.4 KiB
JavaScript
73 lines
2.4 KiB
JavaScript
// ============================================
|
||
// Code нода: Извлечение данных пользователя для записи в БД
|
||
// ============================================
|
||
// Эта нода должна идти ПОСЛЕ первой Code ноды, которая обрабатывает raw_update
|
||
// Входные данные: результат первой Code ноды с полями telegram_id, chat_id, answer_text, answer_type, channel, raw_update
|
||
|
||
const input = $input.item.json;
|
||
const rawUpdate = input.raw_update;
|
||
|
||
// Инициализируем результат
|
||
const result = {
|
||
telegram_id: input.telegram_id,
|
||
chat_id: input.chat_id,
|
||
username: null,
|
||
real_first_name: null,
|
||
real_last_name: null,
|
||
language_code: null,
|
||
is_premium: null,
|
||
last_activity_at: null,
|
||
message_id: null,
|
||
answer_text: input.answer_text,
|
||
answer_type: input.answer_type,
|
||
channel: input.channel || 'telegram',
|
||
raw_update: rawUpdate
|
||
};
|
||
|
||
// Определяем источник данных (message или callback_query)
|
||
let userData = null;
|
||
let messageData = null;
|
||
let dateValue = null;
|
||
|
||
if (rawUpdate.callback_query) {
|
||
// Данные из callback_query
|
||
userData = rawUpdate.callback_query.from;
|
||
messageData = rawUpdate.callback_query.message;
|
||
dateValue = messageData?.date;
|
||
result.message_id = messageData?.message_id;
|
||
} else if (rawUpdate.message) {
|
||
// Данные из message
|
||
userData = rawUpdate.message.from;
|
||
messageData = rawUpdate.message;
|
||
dateValue = messageData?.date;
|
||
result.message_id = messageData?.message_id;
|
||
}
|
||
|
||
// Извлекаем данные пользователя
|
||
if (userData) {
|
||
result.username = userData.username || null;
|
||
result.real_first_name = userData.first_name || null;
|
||
result.real_last_name = userData.last_name || null;
|
||
result.language_code = userData.language_code || null;
|
||
result.is_premium = userData.is_premium !== undefined ? userData.is_premium : null;
|
||
}
|
||
|
||
// Извлекаем дату (Unix timestamp)
|
||
if (dateValue) {
|
||
result.last_activity_at = dateValue;
|
||
} else if (messageData?.date) {
|
||
result.last_activity_at = messageData.date;
|
||
}
|
||
|
||
// Проверки обязательных полей
|
||
if (!result.telegram_id) {
|
||
throw new Error('Не удалось извлечь telegram_id');
|
||
}
|
||
if (!result.chat_id) {
|
||
throw new Error('Не удалось извлечь chat_id');
|
||
}
|
||
|
||
// Возвращаем результат
|
||
return [{ json: result }];
|
||
|