/* eslint-disable canonical/id-match */ /* eslint-disable consistent-return */ import { env as environment } from './config/env'; import { commandsList, KEYBOARD_REMOVE, KEYBOARD_SHARE_PHONE } from './message'; import { createOrUpdateClient, createOrUpdateUser, getCustomer } from '@repo/graphql/api'; import { Enum_Customer_Role } from '@repo/graphql/types'; import { Telegraf } from 'telegraf'; import { message } from 'telegraf/filters'; const bot = new Telegraf(environment.BOT_TOKEN); bot.start(async (context) => { const customer = await getCustomer({ telegramId: context.from.id }); if (customer) { return context.reply( `Приветствуем снова, ${customer.name} 👋. Чтобы воспользоваться сервисом, откройте приложение.` + commandsList, KEYBOARD_REMOVE, ); } return context.reply( 'Добро пожаловать! Пожалуйста, поделитесь своим номером телефона.', KEYBOARD_SHARE_PHONE, ); }); bot.command('addcontact', async (context) => { const customer = await getCustomer({ telegramId: context.from.id }); if (!customer) { return context.reply( 'Чтобы добавить контакт, сначала поделитесь своим номером телефона.', KEYBOARD_SHARE_PHONE, ); } return context.reply('Отправьте контакт клиента, которого вы хотите добавить'); }); bot.on(message('contact'), async (context) => { const customer = await getCustomer({ telegramId: context.from.id }); const isRegistration = !customer; const { contact } = context.message; const name = (contact.first_name || '') + ' ' + (contact.last_name || '').trim(); const phone = contact.phone_number; if (isRegistration) { const response = await createOrUpdateUser({ name, phone, telegramId: context.from.id, }).catch((error) => { context.reply('Произошла ошибка.\n' + error); }); if (response) { return context.reply( `Спасибо! Мы сохранили ваш номер телефона. Теперь можете открыть приложение или воспользоваться командами бота.` + commandsList, KEYBOARD_REMOVE, ); } } else { if (customer.role !== Enum_Customer_Role.Master) { return context.reply( 'Только мастер может добавлять контакты. \nСтать мастером можно на странице профиля в приложении.', ); } const masterId = customer.documentId; const response = await createOrUpdateClient({ masterId, name, phone }).catch((error) => { context.reply('Произошла ошибка.\n' + error); }); if (response) { return context.reply( `Добавили контакт ${name}. Пригласите пользователя в приложение и тогда вы сможете добавлять записи с этим контактом.`, ); } } }); bot.launch(); // Enable graceful stop process.once('SIGINT', () => bot.stop('SIGINT')); process.once('SIGTERM', () => bot.stop('SIGTERM'));