Skip to content

Instantly share code, notes, and snippets.

@kmelve
Created March 31, 2017 06:38
Show Gist options
  • Select an option

  • Save kmelve/fe09a1fc7a426d9e80023c323849d1a3 to your computer and use it in GitHub Desktop.

Select an option

Save kmelve/fe09a1fc7a426d9e80023c323849d1a3 to your computer and use it in GitHub Desktop.
Webtask.io function that takes an `/command word;definition` from Slack and puts it into a graph.cool database.
require('isomorphic-fetch')
// your graph.cool project id
const projectId = 'stammesprak'
module.exports = (context, cb) => {
const text = context.body.text.split(';')
const definition = text[1]
const word = text[0]
/*
* Set permission and authentication token in graph.cool and
* add it to webtask.io secrets
*/
const token = `Bearer ${context.secrets.token}`
const query = `mutation {
createEntries(definition: "${definition}" word: "${word}") {
id
word
definition
}
}`
fetch(`https://api.graph.cool/simple/v1/${projectId}`, {
method: 'post',
headers: {
'Content-Type': 'application/json',
'Authorization': token
},
body: JSON.stringify({ query })
}).then((response) => {
return response.json()
})
.then((data) => {
console.log(data)
cb(null, data)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment