Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Post a message to slack #90

Merged
merged 7 commits into from Jul 9, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 27 additions & 0 deletions server.js
Expand Up @@ -16,6 +16,7 @@ const b17 = process.env.B17
const b18 = process.env.B18
const outs = process.env.OUTS
const slack = process.env.SLACK_TOKEN
const webhookURL = process.env.INVITE_CHANNEL_WEBHOOK
AmanRaj1608 marked this conversation as resolved.
Show resolved Hide resolved

// Get transporter services
const emailHost = process.env.EMAIL_HOST || 'smtp.gmail.com'
Expand Down Expand Up @@ -51,6 +52,32 @@ app.get('/sendmail/:username/:id', (request, response, next) => {
var SlackURL = `https://slack.com/api/users.admin.invite?token=${slack}&email=${id}`
AmanRaj1608 marked this conversation as resolved.
Show resolved Hide resolved
axios.post(SlackURL)

// Post invitation message on Slack
const time = Math.round((new Date()).getTime() / 1000)
const message = `Hello ${username}`
AmanRaj1608 marked this conversation as resolved.
Show resolved Hide resolved
const options = {
'text': 'Welcome to IIITV',
'attachments': [
{
'color': '#36a64f',
AmanRaj1608 marked this conversation as resolved.
Show resolved Hide resolved
'title': 'Invitation from IIITV',
'title_link': 'https://github.com/iiitv',
AmanRaj1608 marked this conversation as resolved.
Show resolved Hide resolved
'text': message,
'footer': 'Slack API',
'ts': time
}
]
}

axios.post(webhookURL, JSON.stringify(options))
.then(response => {
console.log('SUCCEEDED: Sent slack webhook: \n', response.data)
AmanRaj1608 marked this conversation as resolved.
Show resolved Hide resolved
})
.catch(error => {
console.log('FAILED: Send slack webhook', error)
// reject(new Error('FAILED: Send slack webhook'))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case of failure, we can work on max_retry logic. Sometimes what happens is that when slack gets more load, it puts the requests in queue and sometimes those requests are also dropped. So to ensure correctness from our side we can rely on max-retry logic and can store the value in some constant.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I will read about this and fix this issue.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case of failure, we can redirect error to another channel too, if we want to.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, even after themax_retry we can send a single request to post error on a different slack channel. No need to use max_retry logic there because it won't benefit to waste resources on successfully sending of error 😉

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yashLadha
I don't know how to do it using axios 😅
If you could send some reference , It will be very helpful

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try to look for the retry params, a quick google search will get you to the solution. If you are still not able to find out, ping me I will tell you.

})

const verificationurl = `https://${request.get('host')}/verify/${base64}`

var transporter = nodemailer.createTransport({
Expand Down