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
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -9,6 +9,7 @@
},
"dependencies": {
"axios": "^0.18.1",
"dotenv": "^8.0.0",
"express": "^4.16.3",
"gh-account-exists": "^0.1.0",
"jquery": "^3.4.0",
Expand Down
49 changes: 47 additions & 2 deletions server.js
@@ -1,3 +1,4 @@
require('dotenv').config()
const express = require('express')
const app = express()
const axios = require('axios')
Expand All @@ -16,6 +17,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 @@ -48,8 +50,51 @@ app.get('/sendmail/:username/:id', (request, response, next) => {
})

// Invite to Slack
var SlackURL = `https://slack.com/api/users.admin.invite?token=${slack}&email=${id}`
axios.post(SlackURL)
const slackUrl = `https://slack.com/api/users.admin.invite?token=${slack}&email=${id}`
axios.post(slackUrl)

// Post invitation message on Slack
const time = Math.round((new Date()).getTime() / 1000)
const message = `${username} got invited to iiitv organization on GitHub and Slack`
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/orgs/iiitv/people',
'text': message,
'footer': 'Slack API',
'ts': time
}
]
}

function sendMessage () {
return new Promise((resolve, reject) => {
axios.post(webhookURL, JSON.stringify(options))
.then(response => {
return resolve('SUCCESS: Sent slack webhook', response.data)
})
.catch(error => {
return reject(new Error('FAILED: Sent slack webhook', error))
})
})
}

const loop = async () => {
for (let i = 0; i < 3; i++) {
console.log('retrying sending message ', i)
try {
const res = await sendMessage()
console.log(res)
break
} catch (err) {
console.log(err)
}
}
}
loop()

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

Expand Down