-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.ts
93 lines (77 loc) · 2.66 KB
/
server.ts
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
import dotenv from 'dotenv';
import crypto from 'crypto';
import express from 'express';
import { ReadsConfig, readsBot } from './bots/reads.js';
import { mask } from './utils/index.js';
import audit from 'express-requests-logger';
import SmeeClient from 'smee-client';
dotenv.config({ debug: true });
const {
DELPHI_API_BASE_URL,
DELPHI_READS_API_KEY,
DELPHI_READS_BOT_TOKEN,
DELPHI_READS_WEBHOOK_URL,
DELPHI_READS_READING_LIST_ID,
OPENAI_API_KEY,
DEV,
PORT,
} = process.env;
if (!DELPHI_READS_READING_LIST_ID) throw new Error('"DELPHI_READS_READING_LIST_ID" env var is required!');
if (!DELPHI_READS_API_KEY) throw new Error('"DELPHI_READS_API_KEY" env var is required!');
if (!DELPHI_API_BASE_URL) throw new Error('"DELPHI_API_BASE_URL" env var is required!');
if (!DELPHI_READS_BOT_TOKEN) throw new Error('"DELPHI_READS_BOT_TOKEN" env var is required!');
if (!DELPHI_READS_WEBHOOK_URL) throw new Error('"DELPHI_READS_WEBHOOK_URL" env var is required!');
if (!OPENAI_API_KEY) throw new Error('"OPENAI_API_KEY" env var is required!');
const readsBotConfiguration: ReadsConfig = {
botToken: DELPHI_READS_BOT_TOKEN,
openaiKey: OPENAI_API_KEY,
delphiApi: {
apiKey: DELPHI_READS_API_KEY,
baseUrl: DELPHI_API_BASE_URL,
readingListId: DELPHI_READS_READING_LIST_ID,
},
};
const bot = readsBot(readsBotConfiguration);
console.log('Configuration:', {
DELPHI_API_BASE_URL,
DELPHI_READS_WEBHOOK_URL,
DELPHI_READS_BOT_TOKEN: mask(DELPHI_READS_BOT_TOKEN),
DELPHI_READS_API_KEY: mask(DELPHI_READS_API_KEY),
});
const readsWebhookPath = '/webhooks/reads';
let botWebhookUrl = `${DELPHI_READS_WEBHOOK_URL}${readsWebhookPath}`;
const app = express();
const port = PORT || 5555;
if (DEV) {
botWebhookUrl = DELPHI_READS_WEBHOOK_URL;
// setup webhook proxy to local server
const smee = new SmeeClient({
source: DELPHI_READS_WEBHOOK_URL,
target: `http://localhost:${port}${readsWebhookPath}`,
logger: console,
});
await smee.start();
}
// log requests
app.use(
audit({
request: {
maskHeaders: ['x-telegram-bot-api-secret-token']
}
}));
// use this instead of createWebhook() so we can easily proxy thru smee locally
const secretToken = crypto.randomBytes(64).toString("hex");
app.use(bot.webhookCallback(readsWebhookPath, { secretToken }));
await bot.telegram.setWebhook(botWebhookUrl, { secret_token: secretToken });
// configure dev-only endpoints
if (DEV) {
// for posting reads items to localhost when running locally
app.post(`/reads`, (req, res) => {
console.log('POST /reads', req.body);
res.sendStatus(200);
});
}
// Start Express Server
app.listen(port, () => {
console.log(`Express server is listening on ${port}`);
});