telegram-bot-grammy by pbnicad/telegram-bot-grammy-skill
npx skills add https://github.com/pbnicad/telegram-bot-grammy-skill --skill telegram-bot-grammy| 组件 | 技术 |
|---|---|
| 框架 | grammY |
| 语言 | TypeScript |
| 运行时 | Cloudflare Workers |
| ORM | Drizzle ORM |
| 数据库 | Cloudflare D1 (SQLite) |
| 测试 | Vitest |
| 代码检查 | Biome |
| 包管理器 | pnpm |
| Git 钩子 | Husky + lint-staged |
| CI/CD | GitHub Actions (多环境) |
| 分支 |
|---|
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
| 环境 |
|---|
| Worker 名称 |
|---|
| 数据库 |
|---|
dev | 开发环境 | my-telegram-bot-dev | telegram-bot-db-dev |
main | 生产环境 | my-telegram-bot | telegram-bot-db |
pnpm create cloudflare@latest my-telegram-bot
# 选择: "Hello World" Worker, TypeScript: 是, Git: 是, 部署: 否
cd my-telegram-bot
# 核心依赖
pnpm add grammy drizzle-orm
# 开发依赖
pnpm add -D drizzle-kit vitest @vitest/coverage-v8 @biomejs/biome husky lint-staged
drizzle.config.ts)import { defineConfig } from "drizzle-kit";
export default defineConfig({
schema: "./src/db/schema.ts",
out: "./migrations",
dialect: "sqlite",
});
src/db/schema.ts)import { relations, sql } from "drizzle-orm";
import { integer, sqliteTable, text, uniqueIndex } from "drizzle-orm/sqlite-core";
export const users = sqliteTable(
"users",
{
id: integer("id").primaryKey({ autoIncrement: true }),
telegramId: text("telegram_id").notNull(),
username: text("username"),
firstName: text("first_name"),
createdAt: text("created_at").notNull().default(sql`CURRENT_TIMESTAMP`),
updatedAt: text("updated_at").notNull().default(sql`CURRENT_TIMESTAMP`),
},
(table) => [uniqueIndex("users_telegram_id_unique").on(table.telegramId)]
);
export const settings = sqliteTable(
"settings",
{
id: integer("id").primaryKey({ autoIncrement: true }),
userId: integer("user_id")
.notNull()
.references(() => users.id, { onDelete: "cascade" }),
key: text("key").notNull(),
value: text("value"),
},
(table) => [uniqueIndex("settings_user_id_key_unique").on(table.userId, table.key)]
);
export const usersRelations = relations(users, ({ many }) => ({
settings: many(settings),
}));
name = "my-telegram-bot"
main = "src/index.ts"
compatibility_date = "2024-01-01"
compatibility_flags = ["nodejs_compat"]
[env.dev]
name = "my-telegram-bot-dev"
[env.dev.vars]
BOT_INFO = """{ "id": 123456789, "is_bot": true, "first_name": "MyBotDev", "username": "my_bot_dev" }"""
[[env.dev.d1_databases]]
binding = "DB"
database_name = "telegram-bot-db-dev"
database_id = "<DEV_DATABASE_ID>"
[env.production]
name = "my-telegram-bot"
[env.production.vars]
BOT_INFO = """{ "id": 987654321, "is_bot": true, "first_name": "MyBot", "username": "my_bot" }"""
[[env.production.d1_databases]]
binding = "DB"
database_name = "telegram-bot-db"
database_id = "<PRODUCTION_DATABASE_ID>"
# 创建开发数据库
pnpm exec wrangler d1 create telegram-bot-db-dev
# 创建生产数据库
pnpm exec wrangler d1 create telegram-bot-db
# 将 database_id 复制到 wrangler.toml 中
# 创建迁移文件
pnpm exec wrangler d1 migrations create telegram-bot-db-dev init
# 从 Drizzle 模式生成 SQL 文件
pnpm exec drizzle-kit generate
# 应用到开发环境
pnpm exec wrangler d1 migrations apply telegram-bot-db-dev --local
pnpm exec wrangler d1 migrations apply telegram-bot-db-dev --remote
# 应用到生产环境
pnpm exec wrangler d1 migrations apply telegram-bot-db --remote
pnpm exec husky init
.husky/pre-commit:
pnpm exec lint-staged
pnpm test
src/index.ts)import { drizzle } from "drizzle-orm/d1";
import { sql } from "drizzle-orm";
import { Bot, webhookCallback } from "grammy";
import { users } from "./db/schema";
export interface Env {
BOT_TOKEN: string;
BOT_INFO: string;
DB: D1Database;
}
export default {
async fetch(request: Request, env: Env): Promise<Response> {
const db = drizzle(env.DB);
const bot = new Bot(env.BOT_TOKEN, {
botInfo: JSON.parse(env.BOT_INFO),
});
bot.command("start", async (ctx) => {
const user = ctx.from;
if (user) {
await db
.insert(users)
.values({
telegramId: String(user.id),
username: user.username ?? null,
firstName: user.first_name ?? null,
})
.onConflictDoUpdate({
target: users.telegramId,
set: {
username: user.username ?? null,
firstName: user.first_name ?? null,
updatedAt: sql`CURRENT_TIMESTAMP`,
},
});
}
await ctx.reply("Welcome to the Bot!");
});
return webhookCallback(bot, "cloudflare-mod")(request);
},
};
| 分支 | 触发条件 | 目标环境 |
|---|---|---|
dev | push | 开发环境 (my-telegram-bot-dev) |
main | push | 生产环境 (my-telegram-bot) |
| PR | - | 仅运行测试,不部署 |
.github/workflows/ci.ymlname: CI/CD
on:
push:
branches: [main, dev]
pull_request:
branches: [main, dev]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: pnpm
- run: pnpm install --frozen-lockfile
- run: pnpm run lint
- run: pnpm run test
deploy-dev:
needs: test
if: github.ref == 'refs/heads/dev' && github.event_name == 'push'
environment: development
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
- uses: actions/setup-node@v4
- run: pnpm install --frozen-lockfile
- uses: cloudflare/wrangler-action@v3
with:
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
environment: dev
deploy-production:
needs: test
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
environment: production
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
- uses: actions/setup-node@v4
- run: pnpm install --frozen-lockfile
- uses: cloudflare/wrangler-action@v3
with:
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
environment: production
密钥 (设置 -> 密钥和变量 -> Actions):
CLOUDFLARE_API_TOKEN: Cloudflare API 令牌环境 (设置 -> 环境):
development: 可选的保护规则production: 建议配置必需审阅者# 部署到开发环境
pnpm exec wrangler deploy --env dev
# 部署到生产环境
pnpm exec wrangler deploy --env production
# 开发环境
pnpm exec wrangler secret put BOT_TOKEN --env dev
# 生产环境
pnpm exec wrangler secret put BOT_TOKEN --env production
# 开发环境
curl "https://api.telegram.org/bot<DEV_TOKEN>/setWebhook?url=https://my-telegram-bot-dev.<subdomain>.workers.dev/"
# 生产环境
curl "https://api.telegram.org/bot<PROD_TOKEN>/setWebhook?url=https://my-telegram-bot.<subdomain>.workers.dev/"
每周安装量
106
仓库
GitHub 星标数
2
首次出现
Jan 27, 2026
安全审计
安装于
codex93
opencode90
gemini-cli89
github-copilot86
cursor84
kimi-cli74
| Component | Technology |
|---|---|
| Framework | grammY |
| Language | TypeScript |
| Runtime | Cloudflare Workers |
| ORM | Drizzle ORM |
| Database | Cloudflare D1 (SQLite) |
| Testing | Vitest |
| Linting | Biome |
| Package Manager | pnpm |
| Git Hooks | Husky + lint-staged |
| CI/CD | GitHub Actions (multi-environment) |
| Branch | Environment | Worker Name | Database |
|---|---|---|---|
dev | development | my-telegram-bot-dev | telegram-bot-db-dev |
main | production | my-telegram-bot | telegram-bot-db |
pnpm create cloudflare@latest my-telegram-bot
# Select: "Hello World" Worker, TypeScript: Yes, Git: Yes, Deploy: No
cd my-telegram-bot
# Core dependencies
pnpm add grammy drizzle-orm
# Dev dependencies
pnpm add -D drizzle-kit vitest @vitest/coverage-v8 @biomejs/biome husky lint-staged
drizzle.config.ts)import { defineConfig } from "drizzle-kit";
export default defineConfig({
schema: "./src/db/schema.ts",
out: "./migrations",
dialect: "sqlite",
});
src/db/schema.ts)import { relations, sql } from "drizzle-orm";
import { integer, sqliteTable, text, uniqueIndex } from "drizzle-orm/sqlite-core";
export const users = sqliteTable(
"users",
{
id: integer("id").primaryKey({ autoIncrement: true }),
telegramId: text("telegram_id").notNull(),
username: text("username"),
firstName: text("first_name"),
createdAt: text("created_at").notNull().default(sql`CURRENT_TIMESTAMP`),
updatedAt: text("updated_at").notNull().default(sql`CURRENT_TIMESTAMP`),
},
(table) => [uniqueIndex("users_telegram_id_unique").on(table.telegramId)]
);
export const settings = sqliteTable(
"settings",
{
id: integer("id").primaryKey({ autoIncrement: true }),
userId: integer("user_id")
.notNull()
.references(() => users.id, { onDelete: "cascade" }),
key: text("key").notNull(),
value: text("value"),
},
(table) => [uniqueIndex("settings_user_id_key_unique").on(table.userId, table.key)]
);
export const usersRelations = relations(users, ({ many }) => ({
settings: many(settings),
}));
name = "my-telegram-bot"
main = "src/index.ts"
compatibility_date = "2024-01-01"
compatibility_flags = ["nodejs_compat"]
[env.dev]
name = "my-telegram-bot-dev"
[env.dev.vars]
BOT_INFO = """{ "id": 123456789, "is_bot": true, "first_name": "MyBotDev", "username": "my_bot_dev" }"""
[[env.dev.d1_databases]]
binding = "DB"
database_name = "telegram-bot-db-dev"
database_id = "<DEV_DATABASE_ID>"
[env.production]
name = "my-telegram-bot"
[env.production.vars]
BOT_INFO = """{ "id": 987654321, "is_bot": true, "first_name": "MyBot", "username": "my_bot" }"""
[[env.production.d1_databases]]
binding = "DB"
database_name = "telegram-bot-db"
database_id = "<PRODUCTION_DATABASE_ID>"
# Create development database
pnpm exec wrangler d1 create telegram-bot-db-dev
# Create production database
pnpm exec wrangler d1 create telegram-bot-db
# Copy database_id to wrangler.toml
# Create migration file
pnpm exec wrangler d1 migrations create telegram-bot-db-dev init
# Generate SQL files from Drizzle schema
pnpm exec drizzle-kit generate
# Apply to development
pnpm exec wrangler d1 migrations apply telegram-bot-db-dev --local
pnpm exec wrangler d1 migrations apply telegram-bot-db-dev --remote
# Apply to production
pnpm exec wrangler d1 migrations apply telegram-bot-db --remote
pnpm exec husky init
.husky/pre-commit:
pnpm exec lint-staged
pnpm test
src/index.ts)import { drizzle } from "drizzle-orm/d1";
import { sql } from "drizzle-orm";
import { Bot, webhookCallback } from "grammy";
import { users } from "./db/schema";
export interface Env {
BOT_TOKEN: string;
BOT_INFO: string;
DB: D1Database;
}
export default {
async fetch(request: Request, env: Env): Promise<Response> {
const db = drizzle(env.DB);
const bot = new Bot(env.BOT_TOKEN, {
botInfo: JSON.parse(env.BOT_INFO),
});
bot.command("start", async (ctx) => {
const user = ctx.from;
if (user) {
await db
.insert(users)
.values({
telegramId: String(user.id),
username: user.username ?? null,
firstName: user.first_name ?? null,
})
.onConflictDoUpdate({
target: users.telegramId,
set: {
username: user.username ?? null,
firstName: user.first_name ?? null,
updatedAt: sql`CURRENT_TIMESTAMP`,
},
});
}
await ctx.reply("Welcome to the Bot!");
});
return webhookCallback(bot, "cloudflare-mod")(request);
},
};
| Branch | Trigger | Target Environment |
|---|---|---|
dev | push | development (my-telegram-bot-dev) |
main | push | production (my-telegram-bot) |
| PR | - | Tests only, no deployment |
.github/workflows/ci.ymlname: CI/CD
on:
push:
branches: [main, dev]
pull_request:
branches: [main, dev]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: pnpm
- run: pnpm install --frozen-lockfile
- run: pnpm run lint
- run: pnpm run test
deploy-dev:
needs: test
if: github.ref == 'refs/heads/dev' && github.event_name == 'push'
environment: development
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
- uses: actions/setup-node@v4
- run: pnpm install --frozen-lockfile
- uses: cloudflare/wrangler-action@v3
with:
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
environment: dev
deploy-production:
needs: test
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
environment: production
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
- uses: actions/setup-node@v4
- run: pnpm install --frozen-lockfile
- uses: cloudflare/wrangler-action@v3
with:
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
environment: production
Secrets (Settings -> Secrets and variables -> Actions):
CLOUDFLARE_API_TOKEN: Cloudflare API TokenEnvironments (Settings -> Environments):
development: Optional protection rulesproduction: Recommend configuring Required reviewers# Deploy to development
pnpm exec wrangler deploy --env dev
# Deploy to production
pnpm exec wrangler deploy --env production
# Development
pnpm exec wrangler secret put BOT_TOKEN --env dev
# Production
pnpm exec wrangler secret put BOT_TOKEN --env production
# Development
curl "https://api.telegram.org/bot<DEV_TOKEN>/setWebhook?url=https://my-telegram-bot-dev.<subdomain>.workers.dev/"
# Production
curl "https://api.telegram.org/bot<PROD_TOKEN>/setWebhook?url=https://my-telegram-bot.<subdomain>.workers.dev/"
Weekly Installs
106
Repository
GitHub Stars
2
First Seen
Jan 27, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykFail
Installed on
codex93
opencode90
gemini-cli89
github-copilot86
cursor84
kimi-cli74
React 组合模式指南:Vercel 组件架构最佳实践,提升代码可维护性
118,000 周安装