Files
cloudflare_temp_email/worker/src/telegram_api/index.ts
T
Dream HunterandClaude Sonnet 4.5 3ebe22115a feat: add i18n support for backend API and Telegram bot (#797)
* feat: add i18n support for backend API and Telegram bot

- Add comprehensive i18n support for all backend API error messages (zh/en)
- Add /lang command for Telegram bot to set language preference
- Add bilingual command descriptions for Telegram bot
- Support per-user language preference stored in KV
- Global push uses DEFAULT_LANG, user push uses saved preference

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

* fix: improve Telegram bot language preference feature

- Add internationalized message for disabled language feature
- Fix hardcoded English message in /lang command
- Optimize getTgMessages calls (reduce from 3 to 1 call)
- Remove verbose comments for better code clarity
- Add TgLangFeatureDisabledMsg to i18n (zh/en)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-31 01:42:41 +08:00

78 lines
2.6 KiB
TypeScript

import { Hono } from 'hono'
import { ServerResponse } from 'node:http'
import { Writable } from 'node:stream'
import { newTelegramBot, initTelegramBotCommands, sendMailToTelegram } from './telegram'
import settings from './settings'
import miniapp from './miniapp'
import i18n from '../i18n'
export const api = new Hono<HonoCustomType>();
export { sendMailToTelegram }
api.use("/telegram/*", async (c, next) => {
const msgs = i18n.getMessagesbyContext(c);
if (!c.env.TELEGRAM_BOT_TOKEN) {
return c.text(msgs.TgBotTokenRequiredMsg, 400);
}
if (!c.env.KV) {
return c.text(msgs.KVNotAvailableMsg, 400);
}
return await next();
});
api.use("/admin/telegram/*", async (c, next) => {
const msgs = i18n.getMessagesbyContext(c);
if (!c.env.TELEGRAM_BOT_TOKEN) {
return c.text(msgs.TgBotTokenRequiredMsg, 400);
}
if (!c.env.KV) {
return c.text(msgs.KVNotAvailableMsg, 400);
}
return await next();
});
api.post("/telegram/webhook", async (c) => {
const token = c.env.TELEGRAM_BOT_TOKEN;
const bot = newTelegramBot(c, token);
let body = null;
const res = new Writable();
Object.assign(res, {
headersSent: false,
setHeader: (name: string, value: string) => c.header(name, value),
end: (data: any) => body = data,
});
const reqJson = await c.req.json();
await bot.handleUpdate(reqJson, res as ServerResponse);
return c.body(body);
});
api.post("/admin/telegram/init", async (c) => {
const domain = new URL(c.req.url).host;
const token = c.env.TELEGRAM_BOT_TOKEN;
const webhookUrl = `https://${domain}/telegram/webhook`;
console.log(`setting webhook to ${webhookUrl}`);
const bot = newTelegramBot(c, token);
await bot.telegram.setWebhook(webhookUrl)
await initTelegramBotCommands(c, bot);
return c.json({
message: "webhook set successfully",
});
});
api.get("/admin/telegram/status", async (c) => {
const token = c.env.TELEGRAM_BOT_TOKEN;
const bot = newTelegramBot(c, token);
const info = await bot.telegram.getWebhookInfo()
const commands = await bot.telegram.getMyCommands()
return c.json({ info, commands });
});
api.get("/admin/telegram/settings", settings.getTelegramSettings);
api.post("/admin/telegram/settings", settings.saveTelegramSettings);
api.post("/telegram/get_bind_address", miniapp.getTelegramBindAddress);
api.post("/telegram/new_address", miniapp.newTelegramAddress);
api.post("/telegram/bind_address", miniapp.bindAddress);
api.post("/telegram/unbind_address", miniapp.unbindAddress);
api.post("/telegram/get_mail", miniapp.getMail);