-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
28 additions
and
22 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,2 +1,2 @@ | ||
TOKEN=2tZZ9rnH # random string | ||
BOT_TOKEN=xxxxxx:xxxxxx | ||
SECRET_HASH=2tZZ9rnH # random string |
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
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { TOKEN } from '~/src/env' | ||
|
||
export default defineEventHandler((event) => { | ||
const query = getQuery(event) | ||
if (query.token !== TOKEN) { | ||
throw createError({ | ||
status: 401, | ||
statusMessage: 'Unauthorized', | ||
message: 'Invalid token', | ||
}) | ||
} | ||
}) |
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,20 +1,15 @@ | ||
import process from 'node:process' | ||
import { bot } from '../src' | ||
|
||
const SECRET_HASH = process.env.SECRET_HASH! | ||
const TOKEN = process.env.TOKEN! | ||
|
||
export default eventHandler(async (evt) => { | ||
const query = getQuery(evt) | ||
if (!process.dev && query.secret_hash !== SECRET_HASH) { | ||
return 'Forbidden' | ||
} | ||
|
||
const host = getRequestHeader(evt, 'x-forwarded-host') || getRequestHost(evt) | ||
const webhookUrl = `https://${host}/telegram-hook?secret_hash=${SECRET_HASH}` | ||
const webhookUrl = `https://${host}/telegram-hook?token=${TOKEN}` | ||
const isSet = await bot.telegram.setWebhook(webhookUrl) | ||
const info = await bot.telegram.getWebhookInfo() | ||
return `Set webhook to ${webhookUrl.replaceAll( | ||
SECRET_HASH, | ||
TOKEN, | ||
'*', | ||
)}: ${isSet}<br/>${JSON.stringify(info).replaceAll(SECRET_HASH, '*')}` | ||
)}: ${isSet}<br/>${JSON.stringify(info).replaceAll(TOKEN, '*')}` | ||
}) |
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
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import process from 'node:process' | ||
|
||
export const TOKEN = process.env.TOKEN! | ||
export const BOT_TOKEN = process.env.BOT_TOKEN! | ||
|
||
if (!TOKEN || !BOT_TOKEN) { | ||
throw new Error('missing env') | ||
} |
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