Created
July 2, 2022 18:03
-
-
Save MarcosSantosDev/df2087a015dd11d8679c1e49eed34723 to your computer and use it in GitHub Desktop.
A simple application with integration to the slack messaging API.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
const axios = require('axios'); | |
require('dotenv').config(); | |
const getData = async () => { | |
const result = await axios.get('https://next.json-generator.com/api/json/get/4JJYrbRxu'); | |
return result.data.map(person => ({ | |
age: person.age, | |
email: person.email, | |
firstname: person.name.first, | |
lastname: person.name.last, | |
})); | |
} | |
( | |
async () => { | |
try { | |
// Get the data | |
const peoples = await getData(); | |
// Body slack | |
const slackBody = { | |
mkdwn: true, | |
text: "Lista de usuários!", | |
attachments: peoples.map(person => ({ | |
color: "good", | |
text: `*Usuário ${person.firstname} ${person.lastname}, possui endereço de email ${person.email} e idade de ${person.age} anos.` | |
})) | |
}; | |
const { SLACK_KEY1, SLACK_KEY2, SLACK_KEY3 } = process.env; | |
// Post slack | |
const response = await axios({ | |
method: "post", | |
url: `https://hooks.slack.com/services/${SLACK_KEY1}/${SLACK_KEY2}/${SLACK_KEY3}`, | |
data: slackBody | |
}); | |
} catch (error) { | |
throw error; | |
} | |
} | |
)(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment