Skip to content

Instantly share code, notes, and snippets.

@jakazzy
Created September 26, 2024 15:03
Show Gist options
  • Save jakazzy/2db72fc03b418f5b98d2d75082ee0855 to your computer and use it in GitHub Desktop.
Save jakazzy/2db72fc03b418f5b98d2d75082ee0855 to your computer and use it in GitHub Desktop.
A utility to send notes to slack channel from the terminal

Send Note To Slack

This is a utility to enable one send a message to a slack channel specific channel using slack webhook URL.

SETUP

  1. Generate slack channel URL. Follow the steps outlined in the documentation here: https://api.slack.com/messaging/webhooks
  2. add the slack url to your .zsh or .bashrc file i. nano ~/.zshrc add the following line to the .zsh or .bashrc file replacing the url with the exact webhook URL ii. export SLACK_URL=https://hooks.slack.com/services/sakfdknkd/akdjkfjalhdjhljhl
  3. Pull the code send-slack-note.js locally
  4. Covert it into an executable script: i. Open your terminal and navigate to the directory containing send-slack-note.js ii. Run the command chmod +x send-slack-note.js to make it an executable.
  5. Move the script into /usr/local/bin/send-slack-note with the command sudo mv send-slack-note.js /usr/local/bin/send-slack-note

TEST SCRIPT

  1. Having setup the project following the steps outlined above run the command below in your terminal,
  2. Run send-slack-note in the terminal to send message to slack
#!/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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment