Created
March 31, 2017 06:38
-
-
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.
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
| 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