const Logger = require('logdna');
let logger = require('../services/logger');
const dayjs = require('dayjs');
const UserModel = require('../models/Users')
const Calc = require('./calc');
const Parse = require('./parse');
const calc = new Calc();
const parse = new Parse();
let tomorrow = {
day: dayjs().add(1, 'day').get('date'),
month: dayjs().add(1, 'day').get('month') + 1,
year: dayjs().add(1, 'day').get('year')
}
async function messageHandler(bot, chatId, matrixHoroscope) {
await bot.telegram.sendMessage(chatId, matrixHoroscope, { parse_mode: 'Markdown' })
.then(() => {
UserModel.findOneAndUpdate({ chatId: chatId }, { sent: true }, { new: true },
function (err, data) {
if (err) return console.error(err);
});
console.log('message sent');
})
.catch(err => {
if (err.response && err.response.error_code === 403) {
console.log(err.response.description)
UserModel.deleteOne({ chatId: chatId }, function (err, user) {
if (err) return logger.error(err);
console.log('User ' + chatId + ' remove from db');
});
}
});
}
async function sendMatrix(bot) {
try {
const usersDb = await UserModel.find()
usersDb.forEach(async (user) => {
let matrixBirthDay = calc.zoneCalculate(user);
let matrixTomorrow = calc.zoneCalculate(tomorrow);
let matrixHoroscope = calc.sumMatrixZone(matrixBirthDay, matrixTomorrow);
let horoscope = await parse.getHoroscope(matrixHoroscope);
let parseMessage = parse.parseMarkdown(horoscope, user.sent, tomorrow)
await messageHandler(bot, user.chatId, parseMessage)
});
} catch (err) {
console.log(err)
return Promise.reject(err);
}
}
module.exports = sendMatrix
да, так всё работает корректно, спасибо вам!