From 7afe5f2d359e7dc9ab5bbe855fbcf849112ba3df Mon Sep 17 00:00:00 2001 From: Konstantin Filyaev Date: Sun, 14 May 2023 06:00:15 +0300 Subject: [PATCH] feat: alert message --- app.js | 22 ++++++++++++++++++---- config.js | 6 ++++++ utils/constants.js | 5 +++++ 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/app.js b/app.js index 2402877..12909a6 100644 --- a/app.js +++ b/app.js @@ -2,7 +2,14 @@ const mongoose = require('mongoose'); const TelegramApi = require('node-telegram-bot-api'); const { Configuration, OpenAIApi } = require('openai'); -const { OPENAI_API_KEY, TELEGRAM_BOT_API_KEY, DB_ADDRESS } = require('./config'); +const { + OPENAI_API_KEY, + TELEGRAM_BOT_API_KEY, + DB_ADDRESS, + MESSAGE_ALERT, + MESSAGE_HISTORY, + MESSAGE_USERS, +} = require('./config'); const User = require('./models/user'); const { commands, @@ -11,6 +18,7 @@ const { toolTips, errorMessages, images, + alerts, } = require('./utils/constants'); const bot = new TelegramApi(TELEGRAM_BOT_API_KEY, { polling: true }); @@ -42,10 +50,16 @@ bot.on('message', async (msg) => { history[chatId] = []; messages[chatId] = []; } - if (text === '/help') { return bot.sendMessage(chatId, toolTips.help(userName)); } + if (text === MESSAGE_ALERT) { + return User.find({}).then((users) => { + users.forEach((user) => { + bot.sendMessage(user.chatId, alerts.TEST); + }); + }); + } if (text === '/clear') { history[chatId] = []; messages[chatId] = []; @@ -66,10 +80,10 @@ bot.on('message', async (msg) => { await bot.sendSticker(chatId, images.welcomeSticker); return bot.sendMessage(chatId, toolTips.start(firstName)); } - if (text === '/history') { + if (text === MESSAGE_HISTORY) { return bot.sendMessage(chatId, JSON.stringify(history[chatId])); } - if (text === '/users') { + if (text === MESSAGE_USERS) { return User.find({}).then((users) => bot.sendMessage(chatId, JSON.stringify(users))); } diff --git a/config.js b/config.js index a542cfa..e696dba 100644 --- a/config.js +++ b/config.js @@ -5,6 +5,9 @@ const { TELEGRAM_BOT_API_KEY } = process.env; const { PORT = '3000' } = process.env; const { NODE_ENV } = process.env; const { DB_ADDRESS = 'mongodb://localhost:27017/gptbot' } = process.env; +const { MESSAGE_ALERT } = process.env; +const { MESSAGE_USERS } = process.env; +const { MESSAGE_HISTORY } = process.env; module.exports = { OPENAI_API_KEY, @@ -12,4 +15,7 @@ module.exports = { PORT, NODE_ENV, DB_ADDRESS, + MESSAGE_ALERT, + MESSAGE_HISTORY, + MESSAGE_USERS, }; diff --git a/utils/constants.js b/utils/constants.js index 21a506a..ae05fd9 100644 --- a/utils/constants.js +++ b/utils/constants.js @@ -61,6 +61,10 @@ Cлишком много запросов на сервер в данный мо подождите немного и отправьте ваш вопрос заново`, }; +const alerts = { + TEST: 'Тестовое оповещение от @uzornakovre_official. Приношу извинения за беспокойство.', +}; + const images = { welcomeSticker: 'https://tlgrm.ru/_/stickers/22c/b26/22cb267f-a2ab-41e4-8360-fe35ac048c3b/7.webp', }; @@ -72,4 +76,5 @@ module.exports = { toolTips, errorMessages, images, + alerts, };