-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
277 lines (237 loc) · 12.9 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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
const {
VERBOSITY, INPUT_API_USER,
logger, config, helpers,
convertCurrency,
InitApp, InitActual, InitBot, LaunchBot
} = require('./common/init');
const OpenAI = require('openai');
logger.info('Bot is starting up...');
// -- Initialize ---
let App = InitApp();
let Actual = InitActual();
let Bot = InitBot();
// -- Start Server --
App.listen(config.PORT, () => {
logger.info(`Successfully started server on port ${config.PORT}.`);
}).on('error', (err) => {
logger.error(`Failed to start server. ${err}`);
process.exit(1);
});
// -- Start Bot --
LaunchBot(Bot);
// -- Unified Message Handler --
Bot.on('message', async (ctx) => {
const userId = ctx.from?.id;
const chatType = ctx.chat?.type;
const messageText = ctx.message.text || ctx.message.caption;
const userName = ctx.from?.first_name;
logger.warn(userName);
logger.info(`Incoming message from user: ${userId}, chat type: ${chatType}`);
if (messageText) {
const trimmedText = messageText.trim();
// Handle /start or /help command
if (chatType === 'private') {
if (config.USER_IDS.includes(userId)) {
if (trimmedText == '/start' || trimmedText == '/help') {
logger.debug(`Sending intro message to user ${userId}.`);
return ctx.reply(config.INTRO.replace('%USER_ID%', userId));
} else {
await Actual.sync();
const categories = await Actual.getCategories();
const accounts = await Actual.getAccounts();
const payees = await Actual.getPayees();
const prompt = config.OPENAI_PROMPT
.replace('%DATE%', new Date().toISOString().split('T')[0])
.replace('%DEFAULT_ACCOUNT%', config.ACTUAL_DEFAULT_ACCOUNT)
.replace('%DEFAULT_CATEGORY%', config.ACTUAL_DEFAULT_CATEGORY)
.replace('%CURRENCY%', config.ACTUAL_CURRENCY)
.replace('%ACCOUNTS_LIST%', accounts.map(acc => acc.name).join(', '))
.replace('%CATEGORY_LIST%', categories.map(cat => cat.name).join(', '))
.replace('%PAYEE_LIST%', payees.map(payee => payee.name).join(', '))
.replace('%RULES%', config.OPENAI_RULES.join('\n'));
// CALL THE LLM AND PARSE ITS RESPONSE
let parsedResponse = null;
try {
const openai = new OpenAI({
apiKey: config.OPENAI_API_KEY,
baseURL: config.OPENAI_API_ENDPOINT,
});
logger.debug('=== LLM Request Details ===');
logger.debug('System Prompt:\n' + prompt);
logger.debug(`User Message: ${trimmedText}`);
const response = await openai.chat.completions.create({
model: config.OPENAI_MODEL,
messages: [
{ role: 'system', content: prompt },
{ role: 'user', content: trimmedText },
],
temperature: config.OPENAI_TEMPERATURE,
});
// Remove possible Markdown fences
const jsonResponse = response.choices[0].message.content
.replace(/```(?:json)?\n?|\n?```/g, '')
.trim();
logger.debug('=== LLM Response ===');
logger.debug(jsonResponse);
parsedResponse = JSON.parse(jsonResponse);
if (!Array.isArray(parsedResponse)) {
throw new Error('LLM response is not an array');
}
if (parsedResponse.length === 0) {
return ctx.reply('Failed to find any information to create transactions. Try again?', userName === INPUT_API_USER ? {} : { reply_to_message_id: ctx.message.message_id });
}
} catch (err) {
logger.error('Error obtaining/parsing LLM response:', err);
return ctx.reply('Sorry, I received an invalid or empty response from the LLM. Check the bot logs.', userName === INPUT_API_USER ? {} : { reply_to_message_id: ctx.message.message_id });
}
// CREATE TRANSACTIONS IN ACTUAL
try {
let replyMessage = '';
if (config.BOT_VERBOSITY === VERBOSITY.VERBOSE) {
replyMessage = '*[LLM ANSWER]*\n```\n';
replyMessage += helpers.prettyjson(parsedResponse);
replyMessage += '\n```\n\n';
}
replyMessage += '*[TRANSACTIONS]*\n';
let txInfo = {};
const transactions = await Promise.all(parsedResponse.map(async (tx) => {
if (!tx.account) {
tx.account = config.ACTUAL_DEFAULT_ACCOUNT;
}
if (!tx.category) {
tx.category = config.ACTUAL_DEFAULT_CATEGORY;
}
const account = accounts.find(acc => acc.name === tx.account);
const category = categories.find(cat => cat.name === tx.category);
const payee = payees.find(p => p.name === tx.payee);
if (!account) {
throw new Error(`Invalid account specified: "${tx.account}"`);
}
if (!category) {
throw new Error(`Invalid category specified: "${tx.category}"`);
}
let date = tx.date || new Date().toISOString().split('T')[0];
let apiDate = date;
let amount = tx.amount;
// If date is today, currency API may not have today's data yet due to timezone differences
if (date === new Date().toISOString().split('T')[0]) {
apiDate = 'latest';
}
if (tx.currency && tx.currency.toLowerCase() !== config.ACTUAL_CURRENCY.toLowerCase()) {
amount = await convertCurrency(tx.amount, tx.currency, config.ACTUAL_CURRENCY, apiDate, tx.exchange_rate);
} else {
tx.currency = config.ACTUAL_CURRENCY;
}
// Provide human-readable output of processed transaction data
replyMessage += '```\n';
let humanAmount = `${tx.amount} ${tx.currency}`;
if (tx.currency && tx.currency.toLowerCase() !== config.ACTUAL_CURRENCY.toLowerCase()) {
humanAmount = `${amount} ${config.ACTUAL_CURRENCY}`;
}
txInfo = {
date,
account: account.name,
category: category.name,
...(humanAmount && { amount: humanAmount }),
...(tx.payee && { payee: tx.payee }),
...(tx.notes && { notes: tx.notes })
};
if (config.BOT_VERBOSITY >= VERBOSITY.NORMAL) {
replyMessage += helpers.prettyjson(txInfo);
replyMessage += '```\n';
} else {
replyMessage = '';
}
amount = parseFloat((amount * 100).toFixed(2)); // Convert to cents
return {
account: account.id,
date,
amount,
payee_name: tx.payee || null,
category: category.id,
notes: `${config.ACTUAL_NOTE_PREFIX} ${tx.notes || ''}`,
};
}));
// Group transactions by account
const transactionsByAccount = transactions.reduce((acc, tx) => {
if (!acc[tx.account]) {
acc[tx.account] = [];
}
acc[tx.account].push(tx);
return acc;
}, {});
let added = 0;
for (const [accountId, accountTxs] of Object.entries(transactionsByAccount)) {
const transactionsText = accountTxs.map(tx =>
`Account: ${tx.account}, Date: ${tx.date}, Amount: ${tx.amount}, Payee: ${tx.payee_name}, Category: ${tx.category}, Notes: ${tx.notes}`
).join('\n');
logger.info(`Importing transactions for account ${accountId}:\n${transactionsText}`);
const result = await Actual.addTransactions(accountId, accountTxs);
if (result) {
added += accountTxs.length;
}
}
replyMessage += '\n*[ACTUAL]*\n';
if (!added) {
replyMessage += 'no changes';
} else {
replyMessage += `added: ${added}`;
await Actual.sync();
}
logger.info(`Added ${added} transactions to Actual Budget.`);
if (config.BOT_VERBOSITY > VERBOSITY.SILENT) {
return ctx.reply(replyMessage, { parse_mode: 'Markdown', ...(userName !== INPUT_API_USER && { reply_to_message_id: ctx.message.message_id }) });
}
} catch (err) {
logger.error('Error creating transactions in Actual Budget:', err);
if (err.message && err.message.includes('convert currency')) {
return ctx.reply('Sorry, there was an error converting the currency. Check the bot logs.', userName === INPUT_API_USER ? {} : { reply_to_message_id: ctx.message.message_id });
}
return ctx.reply('Sorry, I encountered an error creating the transaction(s). Check the bot logs.', userName === INPUT_API_USER ? {} : { reply_to_message_id: ctx.message.message_id });
}
}
} else {
return ctx.reply(INTRO_DEFAULT, userName === INPUT_API_USER ? {} : { reply_to_message_id: ctx.message.message_id });
}
}
}
});
// Webhook endpoint for Telegram
App.post('/webhook', (req, res) => {
try {
Bot.handleUpdate(req.body);
res.sendStatus(200);
} catch (error) {
logger.error('Error handling update:', error);
res.sendStatus(500);
}
});
// API endpoint for custom input outside Telegram
App.post('/input', (req, res) => {
const userAgent = req.headers['user-agent'];
const ip = req.headers['x-forwarded-for']?.split(',')[0] || req.ip || req.socket.remoteAddress;
logger.debug(`Custom input request received [IP: ${ip}, User-Agent: ${userAgent}]`);
try {
const apiKey = req.headers['x-api-key'];
if (!apiKey || apiKey !== config.INPUT_API_KEY || !config.INPUT_API_KEY || config.INPUT_API_KEY.length < 16) {
logger.debug('Custom input request denied: invalid API key');
return res.status(401).send('Unauthorized');
}
const { user_id, text } = req.body;
if (config.USER_IDS.includes(user_id)) {
Bot.handleUpdate(helpers.createUpdateObject(user_id, INPUT_API_USER, text));
logger.debug('Custom input request handled successfully.');
return res.json({ status: 'OK' });
} else {
logger.debug('Custom input request denied: invalid user ID');
return res.status(403).send('Forbidden');
}
} catch (error) {
logger.error('Error handling custom input request. ', error);
return res.status(500).json({ error: 'Failed to handle message' });
}
});
// Health check endpoint
App.get('/health', (req, res) => {
res.send('OK');
});