-
Notifications
You must be signed in to change notification settings - Fork 1
/
telegram.ts
56 lines (54 loc) · 1.27 KB
/
telegram.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
import { Handler, Context, Callback } from "aws-lambda";
import BotLogic from "./src/botLogic";
import { rateLimit, initializeRateLimiter } from "./src/api/rate-limit";
initializeRateLimiter({
name: "send",
points: 180,
duration: 60,
});
const send: Handler = async (
event: any,
context: Context,
callback: Callback
) => {
const ip = event?.requestContext?.identity?.sourceIp ?? "no-ip";
if (
await rateLimit({
name: "send",
key: ip,
})
) {
console.log("rate limit", ip);
callback(null, {
statusCode: 200,
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Credentials": true,
},
body: "error: rate limit exceeded",
});
return;
}
try {
//console.log("event", event);
const body = JSON.parse(event.body);
console.log("Bot request:", ip, body);
const botLogic = new BotLogic();
await botLogic.activate(body);
await sleep(1000);
callback(null, {
statusCode: 200,
body: "ok",
});
} catch (error) {
console.error("catch", (<any>error).toString());
callback(null, {
statusCode: 200,
body: "ok",
});
}
};
function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
export { send };