-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregister-webhook.ts
More file actions
56 lines (51 loc) · 1.62 KB
/
register-webhook.ts
File metadata and controls
56 lines (51 loc) · 1.62 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
import { z } from "zod";
import type { CloudFormationCustomResourceEvent } from "aws-lambda";
import { MiniTelegramClient } from "../client/telegram";
import { GetParameterCommand, SSMClient } from "@aws-sdk/client-ssm";
import constants from "../constants";
const ResourceProperties = z.object({
webhookUrl: z.string(),
});
async function getBotToken() {
const ssmClient = new SSMClient();
const response = await ssmClient.send(
new GetParameterCommand({
Name: constants.BOT_TOKEN_PARAMETER_NAME,
WithDecryption: true,
})
);
if (!response.Parameter?.Value) {
throw new Error("Bot token not found from SSM");
}
return response.Parameter.Value;
}
async function setWebhook(webhookUrl: string) {
const botToken = await getBotToken();
const client = new MiniTelegramClient(botToken);
return await client.setWebhook(webhookUrl);
}
async function deleteWebhook() {
const botToken = await getBotToken();
const client = new MiniTelegramClient(botToken);
return await client.deleteWebhook();
}
export async function handler(event: CloudFormationCustomResourceEvent) {
const { webhookUrl } = ResourceProperties.parse(event.ResourceProperties);
switch (event.RequestType) {
case "Create":
console.log("Setting webhook to", webhookUrl);
console.log(await setWebhook(webhookUrl));
return {
PhysicalResourceId: webhookUrl,
};
case "Update":
console.log("Updating custom resource");
return {
PhysicalResourceId: webhookUrl,
};
case "Delete":
console.log("Deleting webhook");
console.log(await deleteWebhook());
return;
}
}