-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
210 lines (190 loc) · 5.65 KB
/
index.js
File metadata and controls
210 lines (190 loc) · 5.65 KB
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
import { Telegraf } from 'telegraf';
import { Client } from '@notionhq/client';
import express from 'express';
import { MongoClient } from 'mongodb';
import { ChatGPTAPI } from 'chatgpt';
import { Analytics } from '@segment/analytics-node';
const bot = new Telegraf(process.env.BOT_TOKEN);
const server = express();
const mongodb = new MongoClient(process.env.MONGODB_URI);
const gpt = new ChatGPTAPI({
apiKey: process.env.OPENAI_API_KEY,
});
const analytics = new Analytics({ writeKey: process.env.SEGMENT_WRITE_KEY });
// health check
server.get('/', (req, res) => {
res.send('Noted Bot - OK');
});
server.listen(8080);
function pageTitle(text) {
const maxLength = 80;
if (text.length <= maxLength) {
return text;
}
return `${text.slice(0, maxLength - 3)}...`;
}
function getPrompt(subpageTitles, noteText) {
return `I have the following list of pages, each page contains notes on one topic:
${subpageTitles.map((title) => ` - ${title}`).join('\n')}
And the following note text (in quotes):
"${noteText}"
Which of the pages is the note most relevant to?
Respond with page title only.`;
}
async function chooseParentPage(text, rootPage, notionClient) {
const response = await notionClient.blocks.children.list({
block_id: rootPage,
page_size: 1000,
});
const subpages = response.results.filter((result) => result.type === 'child_page');
const subpageTitles = subpages.map((page) => page.child_page.title);
try {
const prompt = getPrompt(subpageTitles, text);
const res = await gpt.sendMessage(prompt);
const mostRelevantPageTitle = res.text;
let page = subpages
.find((p) => p.child_page.title.toLowerCase() === mostRelevantPageTitle.toLowerCase());
if (!page) {
page = subpages.find((p) => p.child_page.title.toLowerCase() === 'general');
}
if (!page) {
throw new Error('Failed to identify subpage or find a page named General');
}
return page;
} catch (e) {
return {
id: rootPage,
child_page: { title: 'Root Page' },
};
}
}
function makePageCreateData(text, rootPage) {
return {
parent: {
page_id: rootPage,
},
properties: {
title: [{
text: {
content: pageTitle(text),
},
}],
},
children: [{
object: 'block',
type: 'paragraph',
paragraph: {
text: [{
type: 'text',
text: {
content: text,
},
}],
},
}],
};
}
bot.start((ctx) => ctx.reply(`
Hi there! Noted Bot saves all messages it receives into your Notion.
Configure:
1. Go to https://www.notion.so/my-integrations, create a new integration and copy the token
2. Add your token to Noted Bot: /notionToken <your_token>
3. Share the page that all your notes will go under with your new integration
4. Copy that page's ID from URL and add it to Noted Bot: /notionPage <your_page_id>
`));
bot.command('helloworld', (ctx) => {
ctx.reply('Hello World!');
});
bot.command('notionToken', async (ctx) => {
const userId = ctx.message.from.id;
const notionToken = ctx.message.text.substring(13);
try {
analytics.identify({
userId: userId.toString(),
traits: {
firstName: ctx.message.from.firstName,
lastName: ctx.message.from.lastName,
username: ctx.message.from.username,
},
});
} catch (e) {
console.log(`Error Segment identifying user: ${userId}`);
}
try {
await mongodb.connect();
const users = mongodb.db('defaultDb').collection('users');
const updateDoc = {
$set: {
id: userId,
notionToken,
},
};
await users.updateOne({ id: userId }, updateDoc, { upsert: true });
} finally {
await mongodb.close();
}
});
bot.command('notionPage', async (ctx) => {
const userId = ctx.message.from.id;
const notionRoot = ctx.message.text.substring(12);
try {
analytics.identify({
userId: userId.toString(),
traits: {
firstName: ctx.message.from.firstName,
lastName: ctx.message.from.lastName,
username: ctx.message.from.username,
},
});
} catch (e) {
console.log(`Error Segment identifying user: ${userId}`);
}
try {
await mongodb.connect();
const users = mongodb.db('defaultDb').collection('users');
const updateDoc = {
$set: {
id: userId,
notionRoot,
},
};
await users.updateOne({ id: userId }, updateDoc, { upsert: true });
} finally {
await mongodb.close();
}
});
bot.on('message', async (ctx) => {
const userId = ctx.message.from.id;
try {
analytics.track({
userId: userId.toString(),
event: 'Note',
});
} catch (e) {
console.log(`Error Segment track: ${userId}`);
}
try {
await mongodb.connect();
const users = mongodb.db('defaultDb').collection('users');
const user = await users.findOne({ id: ctx.message.from.id });
if (user) {
const notion = new Client({ auth: user.notionToken });
const parentPage = await chooseParentPage(ctx.message.text, user.notionRoot, notion);
const page = makePageCreateData(ctx.message.text, parentPage.id);
try {
await notion.pages.create(page);
ctx.reply(`Saved in ${parentPage.child_page.title}`);
} catch (e) {
ctx.reply(`ERROR: Tried to save in ${parentPage.child_page.title} but Notion API returned an error.
Did you share your root page with the integration?`);
}
} else {
ctx.reply('Notion integration not configured. Use /notionToken and /notionRoot commands.');
}
} finally {
await mongodb.close();
}
});
bot.launch();
process.once('SIGINT', () => bot.stop('SIGINT'));
process.once('SIGTERM', () => bot.stop('SIGTERM'));