-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
135 lines (123 loc) · 3.53 KB
/
index.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
const TelegramBotApi = require("node-telegram-bot-api");
const RoamResearchPrivateApi = require("roam-research-private-api");
class RoamApi extends RoamResearchPrivateApi {
async appendBlock(text, order = 0, uid) {
const result = await this.page.evaluate(
(text, order, uid) => {
if (!window.roamAlphaAPI) {
return Promise.reject("No Roam API detected");
}
const result = window.roamAlphaAPI.createBlock({
location: { "parent-uid": uid, order },
block: { string: text },
});
return Promise.resolve(result);
},
text,
order,
uid
);
// Let's give time to sync.
await this.page.waitForTimeout(1000);
return result;
}
}
const main = async ({ token, adminId, roam: { graph, email, password } }) => {
if (typeof adminId === "string") adminId = parseInt(adminId);
const bot = new TelegramBotApi(token, { polling: true });
const validator = (message) => {
if (message.from.id == adminId) return true;
return false;
};
const roam = new RoamApi(graph, email, password, {
headless: true,
});
await roam.logIn();
console.log("Logged into Roam");
bot.onText(/\/id*(.+)/, (message) => {
bot.sendMessage(
message.chat.id,
`User id: ${message.from.id}\nChat id: ${message.chat.id}`
);
});
bot.onText(/\/add (.+)/, (message) => {
const chatId = message.chat.id;
if (validator(message)) {
const dailyNoteId = roam.dailyNoteUid();
const dailyNoteTitle = roam.dailyNoteTitle();
roam
.runQuery(
`[ :find (pull ?e [*]) :where [?e :node/title "${dailyNoteTitle}"]]`
)
.then((result) => {
try {
return result[0][0].children.length;
} catch {
return result[0].length;
}
})
.then((order) => {
roam
.appendBlock(
message.text.replace(/\/add /, ""),
order ?? 0,
dailyNoteId
)
.then((result) => {
if (result) {
bot.sendMessage(chatId, `Added text to Roam Daily Notes`);
} else {
bot.sendMessage(
chatId,
`Failed to add message to Roam Daily Notes`
);
}
})
.catch((err) => {
bot.sendMessage(
chatId,
`Failed to add message to Roam Daily Notes.\n${err.toString()}`
);
});
})
.catch((err) => {
bot.sendMessage(
chatId,
`Failed to add message to Roam Daily Notes.\n${err.toString()}`
);
});
} else {
console.log(message.from.id);
bot.sendMessage(chatId, "Invalid user.");
}
});
};
module.exports = main;
require("dotenv").config();
(async () => {
[
"ROAM_GRAPH",
"ROAM_EMAIL",
"ROAM_PASSWORD",
"TELEGRAM_BOT_TOKEN",
"TELEGRAM_ADMIN_ID",
].forEach((key) => {
if (!process.env[key]) {
console.log(`${key} not found in env file.`);
process.exit(1);
}
if (key === "TELEGRAM_ADMIN_ID") {
if (typeof process.env[key] === "string")
process.env[key] = parseInt(process.env[key]);
}
});
await main({
token: process.env.TELEGRAM_BOT_TOKEN,
adminId: process.env.TELEGRAM_ADMIN_ID,
roam: {
graph: process.env.ROAM_GRAPH,
email: process.env.ROAM_EMAIL,
password: process.env.ROAM_PASSWORD,
},
});
})().catch((err) => console.log(err));