-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from beilunyang/dev
refactor: migrate to grammy and hono frameworks
- Loading branch information
Showing
13 changed files
with
2,048 additions
and
4,253 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
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 |
---|---|---|
|
@@ -170,4 +170,3 @@ dist | |
|
||
.dev.vars | ||
.wrangler/ | ||
wrangler.toml |
Large diffs are not rendered by default.
Oops, something went wrong.
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,27 +1,21 @@ | ||
{ | ||
"name": "img-mom", | ||
"version": "0.0.0", | ||
"version": "0.1.0", | ||
"private": true, | ||
"scripts": { | ||
"deploy": "wrangler deploy", | ||
"dev": "wrangler dev", | ||
"start": "wrangler dev", | ||
"build": "webpack --progress" | ||
"dev": "wrangler dev src/index.ts", | ||
"tunnel": "cloudflared tunnel --url http://127.0.0.1:8787", | ||
"deploy": "wrangler deploy --minify src/index.ts" | ||
}, | ||
"devDependencies": { | ||
"@cloudflare/workers-types": "^4.20230419.0", | ||
"itty-router": "^3.0.12", | ||
"node-polyfill-webpack-plugin": "^2.0.1", | ||
"ts-loader": "^9.5.1", | ||
"typescript": "^5.2.2", | ||
"webpack": "^5.89.0", | ||
"webpack-cli": "^5.1.4", | ||
"wrangler": "^3.28.1" | ||
"@cloudflare/workers-types": "^4.20240806.0", | ||
"cloudflared": "^0.5.3", | ||
"typescript": "^5.5.2", | ||
"wrangler": "^3.60.3" | ||
}, | ||
"dependencies": { | ||
"@aws-sdk/client-s3": "^3.504.0", | ||
"@cfworker/web": "^1.14.0", | ||
"cfworker-middleware-telegraf": "^2.0.2", | ||
"telegraf": "^4.15.0" | ||
"@aws-sdk/client-s3": "^3.627.0", | ||
"grammy": "^1.28.0", | ||
"hono": "^4.5.4" | ||
} | ||
} |
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 |
---|---|---|
@@ -1,57 +1,49 @@ | ||
import { Application, Router } from '@cfworker/web'; | ||
import createTelegrafMiddleware from 'cfworker-middleware-telegraf'; | ||
import { Hono } from 'hono'; | ||
import { webhookCallback } from 'grammy/web'; | ||
import bot from './bot'; | ||
|
||
const router = new Router(); | ||
const app = new Hono(); | ||
|
||
router.post('/bot', (ctx, next) => { | ||
const webhookSecretToken = ctx.req.headers.get('X-Telegram-Bot-Api-Secret-Token'); | ||
if (self.TG_WEBHOOK_SECRET_TOKEN && webhookSecretToken !== self.TG_WEBHOOK_SECRET_TOKEN) { | ||
ctx.res.status = 401; | ||
return; | ||
} | ||
self.host = ctx.req.url.host; | ||
app.post('/bot', async (ctx, next) => { | ||
self.host = new URL(ctx.req.url).host; | ||
return next(); | ||
}, createTelegrafMiddleware(bot)); | ||
}, webhookCallback(bot, 'hono', { | ||
secretToken: self.TG_WEBHOOK_SECRET_TOKEN | ||
})); | ||
|
||
router.get('/setup', async (ctx) => { | ||
app.get('/setup', async (ctx) => { | ||
const webhookHost = await self.KV_IMG_MOM.get('webhookHost'); | ||
if (!webhookHost) { | ||
const host = ctx.req.url.host; | ||
const host = new URL(ctx.req.url).host; | ||
const botUrl = `https://${host}/bot`; | ||
await bot.telegram.setWebhook(botUrl, { | ||
await bot.api.setWebhook(botUrl, { | ||
secret_token: self.TG_WEBHOOK_SECRET_TOKEN, | ||
}); | ||
await bot.telegram.setMyCommands([{ | ||
await bot.api.setMyCommands([{ | ||
command: '/settings', | ||
description: 'Setting up the bot', | ||
}]); | ||
await self.KV_IMG_MOM.put('webhookHost', host); | ||
ctx.res.body = `Webhook(${botUrl}) setup successful`; | ||
return; | ||
return ctx.text(`Webhook(${botUrl}) setup successful`); | ||
} | ||
ctx.res.status = 401; | ||
return ctx.text('401 Unauthorized. Please visit ImgMom docs (https://github.com/beilunyang/img-mom)', 401) | ||
}); | ||
|
||
router.get('/', (ctx) => { | ||
ctx.res.body = "Hello ImgMom (https://github.com/beilunyang/img-mom)"; | ||
}) | ||
|
||
router.get('/img/:fileName', async (ctx) => { | ||
const fileName = ctx.req.params.fileName; | ||
|
||
const res = await fetch(`https://api.telegram.org/file/bot${self.TG_BOT_TOKEN}/photos/${fileName}`); | ||
app.get('/img/:fileId', async (ctx) => { | ||
const fileId = ctx.req.param('fileId'); | ||
const file = await bot.api.getFile(fileId) | ||
const res = await fetch(`https://api.telegram.org/file/bot${self.TG_BOT_TOKEN}/${file.file_path}`); | ||
if (!res.ok) { | ||
ctx.res.status = 404; | ||
return; | ||
return ctx.text('404 Not Found. Please visit ImgMom docs (https://github.com/beilunyang/img-mom)', 404); | ||
} | ||
|
||
const fileType = fileName.split('.').pop(); | ||
ctx.res.type = `image/${fileType}`; | ||
ctx.res.body = await res.arrayBuffer(); | ||
}) | ||
|
||
const fileType = file.file_path?.split('.').pop() ?? ''; | ||
|
||
return ctx.body(await res.arrayBuffer(), 200, { | ||
'Content-Type': `image/${fileType}` | ||
}); | ||
}); | ||
|
||
new Application().use(router.middleware).listen(); | ||
app.get('/', (ctx) => ctx.text('Hello ImgMom (https://github.com/beilunyang/img-mom)')); | ||
|
||
app.fire() |
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
Oops, something went wrong.