-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use fifo queue to receive Telegram webhook
- Loading branch information
1 parent
4bb2fcf
commit 22a332c
Showing
2 changed files
with
64 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,33 @@ | ||
import { EventBridgeEvent, Handler } from "aws-lambda"; | ||
import { Handler, SQSEvent, SQSRecord } from "aws-lambda"; | ||
import { Config } from "sst/node/config"; | ||
|
||
import { | ||
HandleTelegramWebhookInput, | ||
handleTelegramWebhook, | ||
kv, | ||
} from "@bubby/core/3rdparty"; | ||
import { handleTelegramWebhook, kv } from "@bubby/core/3rdparty"; | ||
import { replyTextChat } from "@bubby/core/handlers"; | ||
|
||
export const handler: Handler< | ||
EventBridgeEvent<"telegram.webhook", HandleTelegramWebhookInput["body"]> | ||
> = async (event) => { | ||
const secretToken = Config.TELEGRAM_WEBHOOK_SECRET_TOKEN; | ||
if (event.source !== secretToken) { | ||
console.error("event.source !== secretToken", { event }); | ||
export const handler: Handler<SQSEvent> = async ({ Records }) => { | ||
for (const record of Records) { | ||
await recordHandler(record); | ||
} | ||
}; | ||
|
||
async function recordHandler(record: SQSRecord) { | ||
const expectedSecretToken = Config.TELEGRAM_WEBHOOK_SECRET_TOKEN; | ||
const actualSecretToken = | ||
record.messageAttributes["XTelegramBotApiSecretToken"]; | ||
if ( | ||
typeof actualSecretToken !== "object" || | ||
actualSecretToken.stringValue !== expectedSecretToken | ||
) { | ||
console.warn("Unrecognized secret token", { | ||
record: JSON.stringify(actualSecretToken), | ||
}); | ||
return; | ||
} | ||
|
||
console.log(JSON.stringify(event.detail, null, 2)); | ||
handleTelegramWebhook({ | ||
body: event.detail, | ||
const body = JSON.parse(record.body); | ||
console.log(JSON.stringify(body, null, 2)); | ||
await handleTelegramWebhook({ | ||
body, | ||
onText: (chat) => replyTextChat({ chat, kv }), | ||
}); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters