Skip to content

Instantly share code, notes, and snippets.

@nekufa
Last active October 25, 2021 16:33
Show Gist options
  • Save nekufa/3d8b40a09d212c052af6f497d37ddd75 to your computer and use it in GitHub Desktop.
Save nekufa/3d8b40a09d212c052af6f497d37ddd75 to your computer and use it in GitHub Desktop.
import fetch from 'node-fetch';
const { Client } = require('@notionhq/client');
const TelegramBot = require('node-telegram-bot-api');
const logger = require('pino')();
export default async function (req, res) {
var response = await fetch('https://isdayoff.ru/today');
var isdayoff = await response.text();
if (isdayoff == "1") {
logger.info('day off');
return res.status(200).json({ success: true, message: 'day off' });
}
const notion = new Client({ auth: process.env.NOTION_TOKEN });
const search = await notion.search({ query: 'Todo' });
const database = search.results[0];
if (!database) {
logger.info('no database');
return res.status(404).json({ success: false, message: 'no database' });
}
const query = await notion.databases.query({
database_id: database.id,
filter: {
or: [
{ property: 'Статус', select: { equals: 'Готово' } },
{ property: 'Статус', select: { equals: 'В работе' } },
{ property: 'Статус', select: { equals: 'Дальше' } },
],
},
sorts: [
{ property: 'Обновлено', direction: 'descending' },
],
});
let map = {
'Готово': [],
'В работе': [],
'Дальше': [],
};
query.results.forEach(page => {
let status = page.properties['Статус'].select.name;
if (!map[status]) {
map[status] = [];
}
let title = page.properties['Task'].title[0].plain_text;
map[status].push(' - <a href="' + page.url + '">' + title + '</a>');
})
if (!map['Готово'].length) {
logger.info('nothing is ready');
return res.status(200).json({ success: true, message: 'no ready' });
}
let result = [];
Object.keys(map).forEach(status => {
result = result.concat(status.toLowerCase() + ':', map[status], '');
});
const bot = new TelegramBot(process.env.TELEGRAM_BOT_TOKEN);
await bot.sendMessage(process.env.TELEGRAM_STANDUP_CHAT_ID, result.join("\n\r"), {
disable_web_page_preview: true,
parse_mode: 'html',
});
await Promise.all(
query.results
.filter(page => page.properties['Статус'].select.name == 'Готово')
.map(page => {
return notion.pages.update({
page_id: page.id,
properties: {
'Статус': { select: { name: 'Архив'}}
}
});
})
);
logger.info({ msg: 'complete', map });
res.status(200).json({ success: true, map });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment