|
#!/usr/bin/env node |
|
|
|
const readline = require('readline'); |
|
const https = require('https'); |
|
|
|
const rl = readline.createInterface({ |
|
input: process.stdin, |
|
output: process.stdout |
|
}); |
|
|
|
function askForTitle( |
|
) { |
|
return new Promise((resolve) => { |
|
rl.question('Enter note title: ', (title) => { |
|
if (validateInput(title, 'title', /^[a-zA-Z0-9\s]{1,50}$/)) { |
|
resolve(title); |
|
} |
|
}); |
|
}); |
|
} |
|
|
|
function askForDescription(title) { |
|
return new Promise((resolve) => { |
|
rl.question('Enter note description: ', (description) => { |
|
if (validateInput(description, 'description', /^[a-zA-Z0-9\s]{1,255}$/)) { |
|
resolve({ title, description }); |
|
} |
|
}); |
|
}); |
|
} |
|
|
|
function askForOthers(noteData) { |
|
return new Promise((resolve) => { |
|
rl.question('Any other information: ', (others) => { |
|
noteData.others = others; |
|
noteData.when = new Date(); |
|
resolve(noteData); |
|
}); |
|
}); |
|
} |
|
|
|
const generateSlackUiTemplate = ({ title, description, when, others }) => { |
|
const template = { |
|
"blocks": [ |
|
{ |
|
"type": "section", |
|
"text": { |
|
"type": "mrkdwn", |
|
"text": "You have added a new note:\n" |
|
} |
|
}, |
|
{ |
|
"type": "section", |
|
"fields": [ |
|
{ |
|
"type": "mrkdwn", |
|
"text": `*Note:*\n${title}` |
|
}, |
|
{ |
|
"type": "mrkdwn", |
|
"text": `*When:*\n${when}` |
|
}, |
|
{ |
|
"type": "mrkdwn", |
|
"text": `*Description:*\n${description}` |
|
}, |
|
{ |
|
"type": "mrkdwn", |
|
"text": `*Others:*\n${others}` |
|
} |
|
] |
|
} |
|
] |
|
} |
|
return JSON.stringify(template, null, 2) |
|
} |
|
|
|
function generateSlackTemplate(noteData) { |
|
return generateSlackUiTemplate(noteData); |
|
} |
|
|
|
const postInSlack = (slackTemplate) => { |
|
const url = process.env.SLACK_URL |
|
|
|
const requestOptions = { |
|
method: 'POST', |
|
headers: { |
|
'Content-Type': 'text/html' |
|
} |
|
}; |
|
|
|
const request = https.request(url, requestOptions, function (response) { |
|
if (response.statusCode !== 200) { |
|
console.error('Network response status code:', response.statusCode); |
|
return; |
|
} |
|
|
|
response.on('data', function (data) { |
|
console.log('Success:', data.toString()); |
|
}); |
|
}); |
|
|
|
request.on('error', function (error) { |
|
console.error('Error:', error); |
|
}); |
|
|
|
request.write(slackTemplate); |
|
request.end(); |
|
} |
|
|
|
function closeReadlineInterface(data) { |
|
console.log(data); |
|
rl.close(); |
|
} |
|
|
|
function handleError(error) { |
|
console.error('Error posting to Slack:', error); |
|
} |
|
|
|
function validateInput(input, message, regex) { |
|
if (!input || !regex.test(input)) { |
|
console.error(`Invalid ${message}. Please try again.`); |
|
return false; |
|
} |
|
return true; |
|
} |
|
|
|
function createNote( |
|
) { |
|
askForTitle() |
|
.then(askForDescription) |
|
.then(askForOthers) |
|
.then(generateSlackTemplate) |
|
.then(postInSlack) |
|
.then(closeReadlineInterface) |
|
.catch(handleError); |
|
} |
|
|
|
createNote() |