-
Notifications
You must be signed in to change notification settings - Fork 0
/
add.js
95 lines (89 loc) · 2.97 KB
/
add.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
const axios = require('axios')
const db = require('./db')
const formatter = require('./formatter')
const qs = require('querystring')
module.exports = {
addCommand: async (triggerID) => {
console.debug('Opening dialog for adding word')
const dialog = {
token: process.env.SLACK_ACCESS_TOKEN,
trigger_id: triggerID,
dialog: JSON.stringify({
title: 'Add word to STINA',
callback_id: 'add-word',
submit_label: 'Add',
elements: [
{
label: 'Swedish',
type: 'text',
name: 'swedish',
hint: 'The word, phrase, or idiom in Swedish'
},
{
label: 'English',
type: 'textarea',
name: 'english',
hint: 'The word, phrase, or idiom translated and/or explained in English'
},
{
label: 'Type',
type: 'select',
name: 'type',
options: [
{ label: 'Word', value: 'word' },
{ label: 'Phrase', value: 'phrase' },
{ label: 'Idiom', value: 'idiom' }
]
},
{
label: 'Difficulty',
type: 'select',
name: 'difficulty',
options: [
{ label: 'Beginner', value: 'beginner' },
{ label: 'Intermediate', value: 'intermediate' },
{ label: 'Hard', value: 'hard' }
]
}
]
})
}
// open the dialog by calling the Slack API dialogs.open method
await axios.post('https://slack.com/api/dialog.open', qs.stringify(dialog))
console.debug('Opened dialog for adding word.')
},
handleResponse: async (body) => {
try {
const newWord = body.submission
newWord.author = body.user.id
await db.addWord(newWord)
// Success! Tell the user!
const wordAttachment = formatter.wordAttachment(newWord)
const statAttachment = formatter.statAttachment()
const successMessage = {
token: process.env.SLACK_ACCESS_TOKEN,
as_user: false,
text: 'Your word was added successfully. Thanks for your contribution :heart:',
attachments: JSON.stringify([wordAttachment, statAttachment]),
channel: body.channel.id,
user: body.user.id
}
await axios.post('https://slack.com/api/chat.postEphemeral', qs.stringify(successMessage))
console.debug('Posted success message')
} catch (e) {
console.error('Failed to add new word. Got error:', e)
try {
const errorMessage = {
token: process.env.SLACK_ACCESS_TOKEN,
as_user: false,
text: ':x: I\'m sorry, but an unknown error occurred while adding your word :cry:',
channel: body.channel.id,
user: body.user.id
}
await axios.post('https://slack.com/api/chat.postEphemeral', qs.stringify(errorMessage))
} catch (e) {
console.error('Failed to notify user about failure...')
}
}
}
}