Skip to content

Commit

Permalink
Use znv to load config
Browse files Browse the repository at this point in the history
  • Loading branch information
deptyped committed Nov 9, 2023
1 parent 4c4d894 commit fa69a70
Show file tree
Hide file tree
Showing 12 changed files with 131 additions and 103 deletions.
9 changes: 4 additions & 5 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
NODE_ENV=development
LOG_LEVEL=debug
BOT_TOKEN=123:ABCABCD
BOT_WEBHOOK=https://www.example.com/<BOT_TOKEN>
LOG_LEVEL=debug
BOT_SERVER_HOST=0.0.0.0
BOT_SERVER_PORT=80
BOT_ALLOWED_UPDATES=[]
BOT_ADMIN_USER_ID=1
BOT_SERVER_HOST=localhost
BOT_SERVER_PORT=3000
BOT_ADMINS=[1]
109 changes: 68 additions & 41 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,49 +26,62 @@ Bot starter template based on [grammY](https://grammy.dev/) bot framework.

## Usage

1. [Create a new repository](https://github.com/bot-base/telegram-bot-template/generate) using this template.
Follow these steps to set up and run your bot using this template:

2. Create an environment variables file:
1. **Create a New Repository**

```bash
cp .env.example .env
```

3. Set BOT_TOKEN [environment variable](#environment-variables) in `.env` file.
Start by creating a new repository using this template. You can do this by clicking [here](https://github.com/bot-base/telegram-bot-template/generate).

2. **Environment Variables Setup**

Create an environment variables file by copying the provided example file:
```bash
cp .env.example .env
```
Open the newly created `.env` file and set the `BOT_TOKEN` environment variable.

4. Launch bot

Development mode:
3. **Launching the Bot**
You can run your bot in both development and production modes.

**Development Mode:**

Install the required dependencies:
```bash
npm install
```
Start the bot in watch mode (auto-reload when code changes):
```bash
# 1. Install dependencies
npm i

# 2. Run bot (in watch mode)
npm run dev
```

Production mode:

**Production Mode:**

Install only production dependencies (no development dependencies):
```bash
# 1. Install dependencies
npm i --only=prod
# 2. Set NODE_ENV to production and change BOT_WEBHOOK to the actual URL to receive updates
# 3. Run bot
npm start
npm install --only=prod
```

Set the `NODE_ENV` environment variable to "production" in your `.env` file. Also, make sure to update `BOT_WEBHOOK` with the actual URL where your bot will receive updates.
```dotenv
NODE_ENV=production
BOT_WEBHOOK=<your_webhook_url>
```

Start the bot in production mode:
```bash
npm start
# or
npm run start:force # if you want to skip type checking
```

### List of available commands
### List of Available Commands

- `npm run lint` — Lint source code.
- `npm run format` — Format source code.
- `npm run typecheck`Runs type checking.
- `npm run dev`Starts the bot in development mode.
- `npm run start`Starts the bot.
- `npm run typecheck`Run type checking.
- `npm run dev`Start the bot in development mode.
- `npm run start`Start the bot.
- `npm run start:force` — Starts the bot without type checking.

## Deploy
Expand Down Expand Up @@ -236,36 +249,48 @@ git merge template/example/webapp-vue -X theirs --squash --no-commit --allow-unr
<tr>
<td>NODE_ENV</td>
<td>String</td>
<td>Application environment (<code>development</code> or <code>production</code>)</td>
<td>Specifies the application environment. (<code>development</code> or <code>production</code>)</td>
</tr>
<tr>
<td>BOT_TOKEN</td>
<td>
String
</td>
<td>
Token, get it from <a href="https://t.me/BotFather">@BotFather</a>.
Telegram Bot API token obtained from <a href="https://t.me/BotFather">@BotFather</a>.
</td>
</tr>
<tr>
<td>BOT_WEBHOOK</td>
<tr>
<td>LOG_LEVEL</td>
<td>
String
</td>
<td>
Webhook endpoint, used to configure webhook in <b>production</b> environment.
<i>Optional.</i>
Specifies the application log level. <br/>
For example, use <code>info</code> for general logging. View the <a href="https://github.com/pinojs/pino/blob/master/docs/api.md#level-string">Pino documentation</a> for more log level options. <br/>
Defaults to <code>info</code>.
</td>
</tr>
<tr>
<td>LOG_LEVEL</td>
<td>BOT_MODE</td>
<td>
String
</td>
<td>
<i>Optional.</i>
Application log level.
See <a href="https://github.com/pinojs/pino/blob/master/docs/api.md#level-string">Pino docs</a> for a complete list of available log levels. <br/>
Defaults to <code>info</code>.
Specifies method to receive incoming updates. (<code>polling</code> or <code>webhook</code>)
Defaults to <code>polling</code>.
</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 in <b>production</b> environment.
</td>
</tr>
<tr>
Expand All @@ -274,7 +299,7 @@ git merge template/example/webapp-vue -X theirs --squash --no-commit --allow-unr
String
</td>
<td>
<i>Optional.</i> Server address. <br/>
<i>Optional.</i> Specifies the server hostname. <br/>
Defaults to <code>0.0.0.0</code>.
</td>
</tr>
Expand All @@ -284,7 +309,7 @@ git merge template/example/webapp-vue -X theirs --squash --no-commit --allow-unr
Number
</td>
<td>
<i>Optional.</i> Server port. <br/>
<i>Optional.</i> Specifies the server port. <br/>
Defaults to <code>80</code>.
</td>
</tr>
Expand All @@ -299,12 +324,14 @@ git merge template/example/webapp-vue -X theirs --squash --no-commit --allow-unr
</td>
</tr>
<tr>
<td>BOT_ADMIN_USER_ID</td>
<td>BOT_ADMINS</td>
<td>
Number or <br> Array of Number
Array of Number
</td>
<td>
<i>Optional.</i> Administrator user ID. Commands such as <code>/setcommands</code> will only be available to a user with this ID. <br/>
<i>Optional.</i>
Administrator user IDs.
Use this to specify user IDs that have special privileges, such as executing <code>/setcommands</code>. <br/>
Defaults to an empty array.
</td>
</tr>
Expand Down
12 changes: 12 additions & 0 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"pino": "8.16.1",
"pino-pretty": "10.2.3",
"tsx": "3.14.0",
"znv": "0.4.0",
"zod": "3.22.4"
},
"devDependencies": {
Expand Down Expand Up @@ -62,4 +63,4 @@
"lint-staged": {
"*.ts": "npm run lint"
}
}
}
4 changes: 2 additions & 2 deletions scripts/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ try {
await bot.stop();
});

if (config.isProd) {
if (config.BOT_MODE === "webhook") {
// to prevent receiving updates before the bot is ready
await bot.init();

Expand All @@ -30,7 +30,7 @@ try {
await bot.api.setWebhook(config.BOT_WEBHOOK, {
allowed_updates: config.BOT_ALLOWED_UPDATES,
});
} else if (config.isDev) {
} else if (config.BOT_MODE === "polling") {
await bot.start({
allowed_updates: config.BOT_ALLOWED_UPDATES,
onStart: ({ username }) =>
Expand Down
6 changes: 3 additions & 3 deletions src/bot/features/bot-admin.ts → src/bot/features/admin.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { chatAction } from "@grammyjs/auto-chat-action";
import { Composer } from "grammy";
import type { Context } from "#root/bot/context.js";
import { isBotAdmin } from "#root/bot/filters/index.js";
import { isAdmin } from "#root/bot/filters/index.js";
import { setCommandsHandler } from "#root/bot/handlers/index.js";
import { logHandle } from "#root/bot/helpers/logging.js";

const composer = new Composer<Context>();

const feature = composer.chatType("private").filter(isBotAdmin);
const feature = composer.chatType("private").filter(isAdmin);

feature.command(
"setcommands",
Expand All @@ -16,4 +16,4 @@ feature.command(
setCommandsHandler,
);

export { composer as botAdminFeature };
export { composer as adminFeature };
2 changes: 1 addition & 1 deletion src/bot/features/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from "./bot-admin.js";
export * from "./admin.js";
export * from "./language.js";
export * from "./unhandled.js";
export * from "./welcome.js";
2 changes: 1 addition & 1 deletion src/bot/filters/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from "./is-bot-admin.js";
export * from "./is-admin.js";
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isUserHasId } from "grammy-guard";
import { config } from "#root/config.js";

export const isBotAdmin = isUserHasId(...config.BOT_ADMIN_USER_ID);
export const isAdmin = isUserHasId(...config.BOT_ADMINS);
2 changes: 1 addition & 1 deletion src/bot/handlers/commands/setcommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export async function setCommandsHandler(ctx: CommandContext<Context>) {
{
scope: {
type: "chat",
chat_id: Number(config.BOT_ADMIN_USER_ID),
chat_id: Number(config.BOT_ADMINS),
},
},
);
Expand Down
4 changes: 2 additions & 2 deletions src/bot/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
createContextConstructor,
} from "#root/bot/context.js";
import {
botAdminFeature,
adminFeature,
languageFeature,
unhandledFeature,
welcomeFeature,
Expand Down Expand Up @@ -52,7 +52,7 @@ export function createBot(token: string, options: Options = {}) {

// Handlers
protectedBot.use(welcomeFeature);
protectedBot.use(botAdminFeature);
protectedBot.use(adminFeature);

if (isMultipleLocales) {
protectedBot.use(languageFeature);
Expand Down
Loading

0 comments on commit fa69a70

Please sign in to comment.