Skip to content

Instantly share code, notes, and snippets.

@dralletje
Created December 9, 2015 03:23
Show Gist options
  • Select an option

  • Save dralletje/4e1dff7c1c664b93dff7 to your computer and use it in GitHub Desktop.

Select an option

Save dralletje/4e1dff7c1c664b93dff7 to your computer and use it in GitHub Desktop.
import fetch from 'node-fetch'
import {zipObject} from 'lodash'
const send = (method, ...args) => subject => subject[method](...args)
const get = prop => object => object[prop]
const log = message => subject => {
console.log(message, subject)
return subject
}
const formdata = data =>
Object.keys(data).map(key =>
`${encodeURIComponent(key)}=${encodeURIComponent(data[key])}`
).join('&')
const Slack = token => {
const SLACK_URL = `https://anything.slack.com/api/`
const request = (method, data = {}) =>
fetch(`${SLACK_URL}${method}`, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: formdata(Object.assign({token}, data)),
})
.then(send('json'))
.then(result => {
if (result.ok !== true) {
throw new Error(`Error in slack request! (${result.error})`)
} else {
return result
}
})
return {
invite: info => request('users.admin.invite', Object.assign(info, {set_active: true})),
users: () => request('users.list').then(get('members')),
}
}
const hours = Number(process.argv[2])
if (Number.isNaN(hours)) {
console.log('You need specify a number of hours! :D')
process.exit()
}
const relativeDate = new Date(Date.now() - hours*60*60*1000)
// const slack = Slack('xoxp-2918905251-2918905253-16230465840-095db486f6')
const slack = Slack('xoxp-16112332978-16112332994-16234953669-f2c1e2a1e8')
Promise.all([
fetch('https://api.typeform.com/v0/form/lGKKlu?key=b6c85098cf27451beeb264f646f9479d1b0f4a9f&completed=true&offset=0')
.then(send('json')).then(get('responses'))
.then(send('filter', response =>
relativeDate < (new Date(response.metadata.date_submit))
))
.then(send('map', x => ({
first_name: x.answers.textfield_14594251,
email: x.answers.email_14594387,
})))
.then(send('map', log('Inviting:')))
,
slack.users()
.then(send('map', user => [user.profile.email, true]))
.then(zipObject)
])
.then( ([infos, users]) => infos.filter(info => !users[info.email]))
.then(send('map', info => slack.invite(info).catch(x => 'Already invited!') ))
.then(promises => Promise.all(promises))
.then(results => {
console.log('')
console.log('Invited', results.length, 'people! 😊 \n')
console.log(results)
})
.catch(err => {
console.log(err.stack)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment