Skip to content

Commit

Permalink
set bot to context, upgrade grammy
Browse files Browse the repository at this point in the history
  • Loading branch information
vorant94 committed Nov 12, 2024
1 parent 79ebd07 commit 3bf4dd4
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 15 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# sofash
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"prepare": "lefthook install"
},
"dependencies": {
"grammy": "^1.31.0",
"grammy": "^1.31.2",
"hono": "^4.6.9",
"zod": "^3.23.8"
},
Expand Down
2 changes: 1 addition & 1 deletion scripts/set-webhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { parseArgs } from "node:util";
import { config } from "dotenv";
import { Bot } from "grammy";
import { z } from "zod";
import { envSchema } from "../src/shared/env.ts";
import { envSchema } from "../src/shared/context/env.ts";

config();

Expand Down
18 changes: 10 additions & 8 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { Bot, webhookCallback } from "grammy";
import { Hono } from "hono";
import { env } from "hono/adapter";
import { contextStorage, getContext } from "hono/context-storage";
import type { Context } from "./shared/context.ts";
import { envSchema } from "./shared/env.ts";
import type { Context } from "./shared/context/context.ts";
import { envSchema } from "./shared/context/env.ts";

if (import.meta.env.DEV) {
// dotenv needed during development to set env locally from process.env
Expand All @@ -15,22 +15,24 @@ if (import.meta.env.DEV) {

const app = new Hono<Context>();

// set validated and parsed env back to the context storage
app.use(contextStorage());
app.use(async (hc, next) => {
hc.set("env", envSchema.parse(env(hc)));
const parsedEnv = envSchema.parse(env(hc));
const bot = new Bot(parsedEnv.BOT_TOKEN);

hc.set("env", parsedEnv);
hc.set("bot", bot);

await next();
});

app.get("/", (hc) => hc.text("Hello CloudFlare!"));

// cannot set this path to be secret since in CF secrets are accessed only
// during request. the same goes for creating a bot instance outside of request
// inside request. the same goes for creating a bot instance outside of request
// scope since token is a secret that is accessible only inside request
app.use("/telegram", (hc) => {
const { env } = getContext<Context>().var;

const bot = new Bot(env.BOT_TOKEN);
const { bot } = getContext<Context>().var;

bot.command("start", (tc) => tc.reply("Hello Telegram!"));
bot.on("message", (tc) =>
Expand Down
3 changes: 2 additions & 1 deletion src/shared/context.ts → src/shared/context/context.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { Bot } from "grammy";
import type { Env as HonoEnv } from "hono";
import type { Env } from "./env.ts";

export interface Context extends HonoEnv {
// biome-ignore lint/style/useNamingConvention: 3-rd party type
Variables: { env: Env };
Variables: { env: Env; bot: Bot };
}
File renamed without changes.

0 comments on commit 3bf4dd4

Please sign in to comment.