Skip to content

Commit

Permalink
feat: mongoose user schema
Browse files Browse the repository at this point in the history
  • Loading branch information
uzornakovre committed May 13, 2023
1 parent b1b7611 commit 5242e0b
Show file tree
Hide file tree
Showing 6 changed files with 276 additions and 8 deletions.
22 changes: 18 additions & 4 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
/* eslint-disable no-restricted-syntax */
const mongoose = require('mongoose');
const TelegramApi = require('node-telegram-bot-api');
const { Configuration, OpenAIApi } = require('openai');
const { OPENAI_API_KEY, TELEGRAM_BOT_API_KEY } = require('./config');
const { OPENAI_API_KEY, TELEGRAM_BOT_API_KEY, DB_ADDRESS } = require('./config');
const User = require('./models/user');
const {
commands,
history,
messages,
toolTips,
errorMessages,
images,
users,
} = require('./utils/constants');

const bot = new TelegramApi(TELEGRAM_BOT_API_KEY, { polling: true });
const configuration = new Configuration({ apiKey: OPENAI_API_KEY });
const openai = new OpenAIApi(configuration);

mongoose.connect(DB_ADDRESS);

function start() {
bot.setMyCommands(commands);
}
Expand All @@ -31,6 +34,7 @@ bot.on('message', async (msg) => {
const { text, chat, from } = msg;
const chatId = chat.id;
const firstName = from.first_name;
const lastName = from.last_name;
const userName = from.username;
let waitMessageId;

Expand All @@ -48,15 +52,25 @@ bot.on('message', async (msg) => {
return bot.sendMessage(chatId, toolTips.clear);
}
if (text === '/start') {
users.push({ [chatId]: userName, date: Date() });
User.findOne({ chatId }).then((res) => {
if (!res) {
User.create({
chatId,
userName: userName || 'noName',
firstName: firstName || '',
lastName: lastName || '',
date: Date(),
});
}
});
await bot.sendSticker(chatId, images.welcomeSticker);
return bot.sendMessage(chatId, toolTips.start(firstName));
}
if (text === '/history') {
return bot.sendMessage(chatId, JSON.stringify(history[chatId]));
}
if (text === '/users') {
return bot.sendMessage(chatId, JSON.stringify(users));
return User.find({}).then((users) => bot.sendMessage(chatId, JSON.stringify(users)));
}

pushMessages(chatId);
Expand Down
2 changes: 2 additions & 0 deletions config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ const { OPENAI_API_KEY } = process.env;
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;

module.exports = {
OPENAI_API_KEY,
TELEGRAM_BOT_API_KEY,
PORT,
NODE_ENV,
DB_ADDRESS,
};
22 changes: 22 additions & 0 deletions models/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const mongoose = require('mongoose');

const userSchema = new mongoose.Schema({
chatId: {
type: String,
required: true,
},
userName: {
type: String,
},
firstName: {
type: String,
},
lastName: {
type: String,
},
date: {
type: String,
},
});

module.exports = mongoose.model('user', userSchema);
Loading

0 comments on commit 5242e0b

Please sign in to comment.