Skip to content

Commit

Permalink
feat: alert message
Browse files Browse the repository at this point in the history
uzornakovre committed May 14, 2023
1 parent d742c46 commit 7afe5f2
Showing 3 changed files with 29 additions and 4 deletions.
22 changes: 18 additions & 4 deletions app.js
Original file line number Diff line number Diff line change
@@ -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)));
}

6 changes: 6 additions & 0 deletions config.js
Original file line number Diff line number Diff line change
@@ -5,11 +5,17 @@ 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,
TELEGRAM_BOT_API_KEY,
PORT,
NODE_ENV,
DB_ADDRESS,
MESSAGE_ALERT,
MESSAGE_HISTORY,
MESSAGE_USERS,
};
5 changes: 5 additions & 0 deletions utils/constants.js
Original file line number Diff line number Diff line change
@@ -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,
};

0 comments on commit 7afe5f2

Please sign in to comment.