node-red-contrib-chatbot
Version:
REDBot a Chat bot for a full featured chat bot for Telegram, Facebook Messenger and Slack. Almost no coding skills required
127 lines (94 loc) • 3.54 kB
JavaScript
/* eslint-disable no-console */
const marked = require('marked');
const clc = require('cli-color');
const fs = require('fs');
const { Client } = require('@notionhq/client')
const { NotionToMarkdown } = require('notion-to-md');
const green = clc.greenBright;
// const white = clc.white;
const grey = clc.blackBright;
// const orange = clc.xterm(214);
const nodeDefinitions = require('./nodes');
// notion api key
let notionAuthToken = process.env.NOTION_API_KEY;
if (fs.existsSync(`${__dirname}/../.env-notion`)) {
notionAuthToken = fs.readFileSync(`${__dirname}/../.env-notion`);
}
if (notionAuthToken == null || notionAuthToken === '') {
console.log('Missing notion token, skipping');
process.exit();
}
// Initializing a client
const notion = new Client({ auth: notionAuthToken });
const n2m = new NotionToMarkdown({ notionClient: notion });
const extractNotionId = url => {
const matched = url.match(/\-([a-z0-9]{32,32})$/);
if (matched != null) {
let notionId = String(matched[1]);
return notionId.substring(0, 8) + '-'
+ notionId.substring(8, 12) + '-'
+ notionId.substring(12, 16) + '-'
+ notionId.substring(16, 20) + '-'
+ notionId.substring(20, 32);
}
};
const getPage = async function(url) {
// parse url
const notionId = extractNotionId(url);
// TODO check if valid
const mdblocks = await n2m.pageToMarkdown(notionId);
const mdString = n2m.toMarkdownString(mdblocks);
const html = marked.parse(mdString);
return html;
};
const getMarkdownPage = async function(url) {
// parse url
const notionId = extractNotionId(url);
// TODO check if valid
const mdblocks = await n2m.pageToMarkdown(notionId);
const mdString = n2m.toMarkdownString(mdblocks);
return mdString;
};
const runner = async function() {
console.log('# ' + grey('Building changelog:'));
console.log('');
const mdChangeLog = await getMarkdownPage('https://www.notion.so/redbot/Change-log-b46a94ab6bbc4c7d8a586cbc21af7d78');
const mdReleases = mdChangeLog.match(/\|(.*)\|/gm);
let changelog = '';
mdReleases.forEach(row => {
const splitted = row.split(' | ');
if (splitted.length >= 2 && splitted[0].indexOf('------') === -1) {
const version = splitted[0].replace(/^\|/, '').trim();
const description = splitted[1].replace(/\\n$/, '').replace(/\|$/, '').trim();
changelog += `- **${version}** - ${description}\n`;
}
});
fs.writeFileSync(
__dirname + '/../CHANGELOG.md',
changelog,
'utf8'
);
console.log('# ' + grey('Downloading nodes help:'));
console.log('');
// download all nodes documentation from notion
let idx = 0;
for(idx = 0; idx < nodeDefinitions.length; idx++) {
const node = nodeDefinitions[idx];
console.log('- ' + grey(node.notionUrl) + ' (' + node.nodeType + ')');
const htmlSource = await getPage(node.notionUrl);
let nodeSource;
try {
nodeSource = fs.readFileSync(__dirname + '/../nodes/' + node.nodeFile, 'utf8');
} catch(e) {
console.log(`Unable to find file ${node.nodeFile}`);
}
const newDoc = '<script type="text\/x-red" data-help-name="' + node.nodeType + '">' + htmlSource + '</script>';
const regexp = new RegExp('<script type=\"text\/x-red\" data-help-name=\"' + node.nodeType + '\">[\\s\\S]*?<\/script>', 'g');
nodeSource = nodeSource.replace(regexp, newDoc);
fs.writeFileSync(__dirname + '/../nodes/' + node.nodeFile, nodeSource, 'utf8');
}
// end
console.log(green('All done.'));
console.log('');
};
runner();