forked from dannysmith/send-to-notion-from-slack-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
226 lines (199 loc) · 5.55 KB
/
app.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
const { App, LogLevel } = require("@slack/bolt");
const { Client } = require('@notionhq/client');
const app = new App({
token: process.env.SLACK_BOT_TOKEN,
signingSecret: process.env.SLACK_SIGNING_SECRET,
logLevel: LogLevel.DEBUG
});
const notion = new Client({
auth: process.env.NOTION_TOKEN,
});
app.view('save_notion_card', async ({ ack, body, view, client }) => {
// Acknowledge shortcut request
await ack();
// CGet data from form submission
const databaseId = body.view.state.values.notion_database['notion_database-action'].selected_option.value
const cardTitle = body.view.state.values.card_title['card_title-action'].value
const message = body.view.state.values.message_text['message_text-action'].value
const [channelID, messageTS] = body.view.private_metadata.split('+')
// Get permalink to message
const permalinkResult = await client.chat.getPermalink({
channel: channelID,
message_ts: messageTS,
});
// Get title field of Database
const notionDatabase = await notion.databases.retrieve({ database_id: databaseId });
function findTitleField(props) {
const index = Object.values(props).findIndex((el) => el.id === 'title')
return Object.keys(props)[index]
}
const titleField = findTitleField(notionDatabase.properties)
// Create notion page
const response = await notion.pages.create({
parent: {
database_id: databaseId,
},
properties: {
[titleField]: {
title: [
{
text: {
content: cardTitle,
},
},
],
},
},
children: [
{
object: 'block',
type: 'paragraph',
paragraph: {
text: [
{
type: 'text',
text: {
content: message,
},
},
],
},
},
{
object: 'block',
type: 'paragraph',
paragraph: {
text: [
{
type: 'text',
text: {
content: "Original Slack Message",
link: {
url: permalinkResult.permalink
},
},
},
],
},
},
],
});
const url = 'https://notion.so/' + response.id.split('-').join('')
const result = await app.client.chat.postMessage({
token: process.env.SLACK_BOT_TOKEN,
channel: channelID,
thread_ts: messageTS,
text: "Card Created in Notion :notion:\r\r" + url
});
console.log('Done')
})
app.shortcut('create_notion_record', async ({ ack, payload, client }) => {
try {
// Acknowledge shortcut request
await ack();
// Get abailable databases
const listOfDatabases = await notion.databases.list();
const databases = listOfDatabases.results.map((db) => {
return {id: db.id, name: db.title[0].text.content}
})
const databaseOptionsForModal = databases.map((db) => {
return {
text: {
type: "plain_text",
text: db.name,
emoji: true
},
value: db.id
}
})
console.log(databases)
// Show Modal
const modalResult = await client.views.open({
trigger_id: payload.trigger_id,
view: {
type: "modal",
callback_id: 'save_notion_card',
private_metadata: `${payload.channel.id}+${payload.message.ts}`,
title: {
type: "plain_text",
text: "Add Card to Notion",
emoji: true
},
submit: {
type: "plain_text",
text: "Add Card",
emoji: true
},
close: {
type: "plain_text",
text: "Cancel",
emoji: true
},
blocks: [
{
type: "section",
text: {
type: "plain_text",
text: "Create a record in Notion from this message.",
emoji: true
}
},
{
type: "input",
block_id: "notion_database",
element: {
type: "static_select",
placeholder: {
type: "plain_text",
text: "Choose an Database",
emoji: true
},
options: databaseOptionsForModal,
action_id: "notion_database-action"
},
label: {
type: "plain_text",
text: "Notion Database",
emoji: true
}
},
{
type: "input",
block_id: "card_title",
element: {
type: "plain_text_input",
action_id: "card_title-action"
},
label: {
type: "plain_text",
text: "Card Title",
emoji: true
}
},
{
type: "input",
block_id: "message_text",
element: {
type: "plain_text_input",
initial_value: payload.message.text,
multiline: true,
action_id: "message_text-action"
},
label: {
type: "plain_text",
text: "Details",
emoji: true
}
}
]
}});
}
catch (error) {
console.error(error);
}
});
(async () => {
// Start your app
await app.start(process.env.PORT || 3000);
console.log('⚡️ Bolt app is running!');
})();