Skip to content

fix(app): made changes for deploying to vercel #614

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: deploy/vercel
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,7 @@ data/

# Ignore SQLite database
*.db
*.db-journal
*.db-journal

# Vercel project configuration
.vercel
1 change: 1 addition & 0 deletions .vercelignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.env*
62 changes: 44 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,56 @@ Follow these steps to set up and run your bot using this template:
npm run dev
```

**Production Mode:**
**Production Mode:**

Install only production dependencies:
Install Vercel CLI:
```bash
npm install --only=prod
npm i -g vercel
```

Set `DEBUG` environment variable to `false` in your `.env` file.
Create a project:
```bash
vercel link
```
### ---------- !! Do not use sensitive flag for variables !! ----------

Set `NODEJS_HELPERS` environment variable to `0`:
```bash
vercel env add NODEJS_HELPERS
```

Set `BOT_MODE` environment variable to `webhook`:
```bash
vercel env add BOT_MODE
```

Start the bot in production mode:
Set `BOT_TOKEN` environment variable:
```bash
vercel env add BOT_TOKEN
```

Set `BOT_WEBHOOK_SECRET` environment variable to a random secret token:
```bash
# Generate and set secret token using Node
node -e "console.log(crypto.randomBytes(256*0.75).toString('base64url'))" | vercel env add BOT_WEBHOOK_SECRET
```
```bash
# OR using Python
python3 -c "import secrets; print(secrets.token_urlsafe(256))" | vercel env add BOT_WEBHOOK_SECRET
```
```bash
npm run start:force # skip type checking and start
# or
npm start # with type checking (requires development dependencies)
# OR set manually:
vercel env add BOT_WEBHOOK_SECRET
```

Deploy your bot:
```bash
vercel
```

After successful deployment, set up a webhook to connect your Vercel app with Telegram, modify the below URL to your credentials and visit it from your browser:
```
https://APP_NAME.vercel.app/BOT_TOKEN
```

### List of Available Commands
Expand Down Expand Up @@ -302,16 +338,6 @@ bun add -d @types/bun
Enables debug mode. You may use <code>config.isDebug</code> flag to enable debugging functions.
</td>
</tr>
<tr>
<td>BOT_WEBHOOK</td>
<td>
String
</td>
<td>
<i>Optional in <code>polling</code> mode.</i>
Webhook endpoint URL, used to configure webhook.
</td>
</tr>
<tr>
<td>BOT_WEBHOOK_SECRET</td>
<td>
Expand Down
21 changes: 21 additions & 0 deletions api/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { createBot } from '#root/bot/index.js';
import { convertKeysToCamelCase, createConfig } from "#root/config.js";
import { logger } from '#root/logger.js';
import { createServer } from '#root/server/index.js';
import { handle } from '@hono/node-server/vercel';
import process from 'node:process';

// @ts-expect-error create config from environment variables
const config = createConfig(convertKeysToCamelCase(process.env))

const bot = createBot(config.botToken, {
config,
logger,
})
const server = createServer({
bot,
config,
logger,
})

export default handle(server)
1 change: 1 addition & 0 deletions locales/en.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
start-command-description = Start the bot
language-command-description = Change language
setcommands-command-description = Set bot commands
ping-pong-command-description = Test bot work. Ping-Pong

## Welcome Feature

Expand Down
22 changes: 22 additions & 0 deletions locales/uk.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
## Commands

start-command-description = Запустити бота
language-command-description = Змінити мову
setcommands-command-description = Встановити команди бота
ping-pong-command-description = Тест бота. Ping-Pong
## Welcome Feature

welcome = Ласкаво просимо!

## Language Feature

language-select = Будь ласка, оберіть мову
language-changed = Мову успішно змінено!

## Admin Feature

admin-commands-updated = Команди оновлено.

## Unhandled Feature

unhandled = Невідома команда. Спробуйте /start
13 changes: 13 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"@grammyjs/types": "3.11.1",
"@hono/node-server": "1.12.0",
"callback-data": "1.1.1",
"dotenv": "^16.4.7",
"grammy": "1.27.0",
"grammy-guard": "0.5.0",
"hono": "4.5.2",
Expand Down
17 changes: 17 additions & 0 deletions src/bot/features/ping.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type { Context } from '#root/bot/context.js'
import { logHandle } from '#root/bot/helpers/logging.js'
import { Composer } from 'grammy'

const composer = new Composer<Context>()

const feature = composer.chatType('private')

feature.command('ping', logHandle('command-ping-pong'), (ctx) => {
return ctx.reply('pong')
})

feature.filter((ctx) => ctx.msg?.text?.toLocaleLowerCase() === 'ping').on('msg:text', logHandle('msg-ping-pong'), (ctx) => {
return ctx.reply('pong')
})

export { composer as PingFeature }
8 changes: 5 additions & 3 deletions src/bot/features/welcome.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { Composer } from 'grammy'
import type { Context } from '#root/bot/context.js'
import { logHandle } from '#root/bot/helpers/logging.js'
import { Composer, Keyboard } from 'grammy'

const composer = new Composer<Context>()

const feature = composer.chatType('private')

feature.command('start', logHandle('command-start'), (ctx) => {
return ctx.reply(ctx.t('welcome'))
feature.command('start', logHandle('command-start'), async (ctx) => {
return ctx.reply(ctx.t('welcome'), {
reply_markup: new Keyboard().text('ping').resized(),
})
})

export { composer as welcomeFeature }
34 changes: 34 additions & 0 deletions src/bot/handlers/commands/command-definitions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { i18n } from "#root/bot/i18n.js";

export interface Command {
command: string;
description: (lang: string) => string;
isAdmin?: true;
scope?: "private" | "group" | "all";
}

export const commands: Command[] = [
{
command: "start",
description: (lang) => i18n.t(lang, "start-command-description"),
scope: "private",
},
{
command: "ping",
description: (lang) => i18n.t(lang, "ping-pong-command-description"),
scope: "private",
},
{
command: "setcommands",
description: (lang) => i18n.t(lang, "setcommands-command-description"),
isAdmin: true,
scope: "private",
},
];

export const languageCommand = {
command: "language",
description: (localeCode: string) =>
i18n.t(localeCode, "language-command-description"),
scope: "private",
} as const;
Loading