Skip to content

Commit

Permalink
Reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
deptyped committed May 23, 2024
1 parent 377b612 commit 5357649
Show file tree
Hide file tree
Showing 32 changed files with 402 additions and 400 deletions.
6 changes: 3 additions & 3 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
version: 2
updates:
- package-ecosystem: 'npm'
directory: '/'
- package-ecosystem: npm
directory: /
schedule:
interval: 'daily'
interval: daily
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
id: metadata
uses: dependabot/fetch-metadata@v2
with:
github-token: "${{ secrets.GITHUB_TOKEN }}"
github-token: '${{ secrets.GITHUB_TOKEN }}'
- name: Auto-merge
if: >
${{ steps.metadata.outputs.update-type == 'version-update:semver-minor' &&
Expand Down
36 changes: 18 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<img align="right" width="35%" src="https://github.com/bot-base/telegram-bot-template/assets/26162440/c4371683-3e99-4b1c-ae8e-11ccbea78f4b">

Bot starter template based on [grammY](https://grammy.dev/) bot framework.
Bot starter template based on [grammY](https://grammy.dev/) bot framework.

## Features

Expand Down Expand Up @@ -33,19 +33,19 @@ Follow these steps to set up and run your bot using this template:
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.

3. **Launching the Bot**

You can run your bot in both development and production modes.

**Development Mode:**

Install the required dependencies:
```bash
npm install
Expand All @@ -56,22 +56,22 @@ Follow these steps to set up and run your bot using this template:
```

**Production Mode:**

Install only production dependencies (no development dependencies):
```bash
npm install --only=prod
```
Set `NODE_ENV` environment variable to `production` in your `.env` file.
Update `BOT_WEBHOOK` with the actual URL where your bot will receive updates.

Set `NODE_ENV` environment variable to `production` in your `.env` file.
Update `BOT_WEBHOOK` with the actual URL where your bot will receive updates.
Update `BOT_WEBHOOK_SECRET` with a random secret token.

```dotenv
NODE_ENV=production
BOT_WEBHOOK=<server_url>/webhook
BOT_WEBHOOK_SECRET=<random_secret_value>
```

Start the bot in production mode:
```bash
npm start # with type checking (requires development dependencies)
Expand Down Expand Up @@ -117,7 +117,7 @@ project-root/
### Docker ([docker.com](https://docker.com))
Branch:
[deploy/docker-compose](https://github.com/bot-base/telegram-bot-template/tree/deploy/docker-compose)
[deploy/docker-compose](https://github.com/bot-base/telegram-bot-template/tree/deploy/docker-compose)
([open diff](https://github.com/bot-base/telegram-bot-template/compare/deploy/docker-compose))
Use in your project:
Expand All @@ -138,7 +138,7 @@ git merge template/deploy/docker-compose -X theirs --squash --no-commit --allow-
### Vercel ([vercel.com](https://vercel.com))

Branch:
[deploy/vercel](https://github.com/bot-base/telegram-bot-template/tree/deploy/vercel)
[deploy/vercel](https://github.com/bot-base/telegram-bot-template/tree/deploy/vercel)
([open diff](https://github.com/bot-base/telegram-bot-template/compare/deploy/vercel))

Use in your project:
Expand All @@ -161,7 +161,7 @@ git merge template/deploy/vercel -X theirs --squash --no-commit --allow-unrelate
### grammY conversations ([grammy.dev/plugins/conversations](https://grammy.dev/plugins/conversations))

Branch:
[example/plugin-conversations](https://github.com/bot-base/telegram-bot-template/tree/example/plugin-conversations)
[example/plugin-conversations](https://github.com/bot-base/telegram-bot-template/tree/example/plugin-conversations)
([open diff](https://github.com/bot-base/telegram-bot-template/compare/example/plugin-conversations))

Use in your project:
Expand All @@ -188,7 +188,7 @@ npm i @grammyjs/conversations
### grammY runner ([grammy.dev/plugins/runner](https://grammy.dev/plugins/runner))

Branch:
[example/plugin-runner](https://github.com/bot-base/telegram-bot-template/tree/example/plugin-runner)
[example/plugin-runner](https://github.com/bot-base/telegram-bot-template/tree/example/plugin-runner)
([open diff](https://github.com/bot-base/telegram-bot-template/compare/example/plugin-runner))

Use in your project:
Expand All @@ -215,7 +215,7 @@ npm i @grammyjs/runner
### Prisma ORM ([prisma.io](https://prisma.io))

Branch:
[example/orm-prisma](https://github.com/bot-base/telegram-bot-template/tree/example/orm-prisma)
[example/orm-prisma](https://github.com/bot-base/telegram-bot-template/tree/example/orm-prisma)
([open diff](https://github.com/bot-base/telegram-bot-template/compare/example/orm-prisma))

Use in your project:
Expand Down Expand Up @@ -243,7 +243,7 @@ npm i @prisma/client
### Bun ([bun.sh](https://bun.sh))

Branch:
[example/runtime-bun](https://github.com/bot-base/telegram-bot-template/tree/example/runtime-bun)
[example/runtime-bun](https://github.com/bot-base/telegram-bot-template/tree/example/runtime-bun)
([open diff](https://github.com/bot-base/telegram-bot-template/compare/example/runtime-bun))

Use in your project:
Expand Down Expand Up @@ -381,11 +381,11 @@ bun add -d @types/bun
Array of Number
</td>
<td>
<i>Optional.</i>
Administrator user IDs.
<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>
</tbody>
</table>
</table>
6 changes: 3 additions & 3 deletions src/bot/callback-data/change-language.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createCallbackData } from "callback-data";
import { createCallbackData } from 'callback-data'

export const changeLanguageData = createCallbackData("language", {
export const changeLanguageData = createCallbackData('language', {
code: String,
});
})
2 changes: 1 addition & 1 deletion src/bot/callback-data/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from "./change-language.js";
export * from './change-language.js'
44 changes: 22 additions & 22 deletions src/bot/context.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
import { Update, UserFromGetMe } from "@grammyjs/types";
import { Context as DefaultContext, SessionFlavor, type Api } from "grammy";
import type { AutoChatActionFlavor } from "@grammyjs/auto-chat-action";
import type { HydrateFlavor } from "@grammyjs/hydrate";
import type { I18nFlavor } from "@grammyjs/i18n";
import type { ParseModeFlavor } from "@grammyjs/parse-mode";
import type { Logger } from "#root/logger.js";
import type { Update, UserFromGetMe } from '@grammyjs/types'
import { type Api, Context as DefaultContext, type SessionFlavor } from 'grammy'
import type { AutoChatActionFlavor } from '@grammyjs/auto-chat-action'
import type { HydrateFlavor } from '@grammyjs/hydrate'
import type { I18nFlavor } from '@grammyjs/i18n'
import type { ParseModeFlavor } from '@grammyjs/parse-mode'
import type { Logger } from '#root/logger.js'

export type SessionData = {
export interface SessionData {
// field?: string;
};
}

type ExtendedContextFlavor = {
logger: Logger;
};
interface ExtendedContextFlavor {
logger: Logger
}

export type Context = ParseModeFlavor<
HydrateFlavor<
DefaultContext &
ExtendedContextFlavor &
SessionFlavor<SessionData> &
I18nFlavor &
AutoChatActionFlavor
ExtendedContextFlavor &
SessionFlavor<SessionData> &
I18nFlavor &
AutoChatActionFlavor
>
>;
>

interface Dependencies {
logger: Logger;
logger: Logger
}

export function createContextConstructor({ logger }: Dependencies) {
return class extends DefaultContext implements ExtendedContextFlavor {
logger: Logger;
logger: Logger

constructor(update: Update, api: Api, me: UserFromGetMe) {
super(update, api, me);
super(update, api, me)

this.logger = logger.child({
update_id: this.update.update_id,
});
})
}
} as unknown as new (update: Update, api: Api, me: UserFromGetMe) => Context;
} as unknown as new (update: Update, api: Api, me: UserFromGetMe) => Context
}
26 changes: 13 additions & 13 deletions src/bot/features/admin.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { chatAction } from "@grammyjs/auto-chat-action";
import { Composer } from "grammy";
import type { Context } from "#root/bot/context.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";
import { chatAction } from '@grammyjs/auto-chat-action'
import { Composer } from 'grammy'
import type { Context } from '#root/bot/context.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 composer = new Composer<Context>()

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

feature.command(
"setcommands",
logHandle("command-setcommands"),
chatAction("typing"),
'setcommands',
logHandle('command-setcommands'),
chatAction('typing'),
setCommandsHandler,
);
)

export { composer as adminFeature };
export { composer as adminFeature }
8 changes: 4 additions & 4 deletions src/bot/features/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from "./admin.js";
export * from "./language.js";
export * from "./unhandled.js";
export * from "./welcome.js";
export * from './admin.js'
export * from './language.js'
export * from './unhandled.js'
export * from './welcome.js'
38 changes: 19 additions & 19 deletions src/bot/features/language.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
import { Composer } from "grammy";
import { changeLanguageData } from "#root/bot/callback-data/index.js";
import type { Context } from "#root/bot/context.js";
import { logHandle } from "#root/bot/helpers/logging.js";
import { i18n } from "#root/bot/i18n.js";
import { createChangeLanguageKeyboard } from "#root/bot/keyboards/index.js";
import { Composer } from 'grammy'
import { changeLanguageData } from '#root/bot/callback-data/index.js'
import type { Context } from '#root/bot/context.js'
import { logHandle } from '#root/bot/helpers/logging.js'
import { i18n } from '#root/bot/i18n.js'
import { createChangeLanguageKeyboard } from '#root/bot/keyboards/index.js'

const composer = new Composer<Context>();
const composer = new Composer<Context>()

const feature = composer.chatType("private");
const feature = composer.chatType('private')

feature.command("language", logHandle("command-language"), async (ctx) => {
return ctx.reply(ctx.t("language.select"), {
feature.command('language', logHandle('command-language'), async (ctx) => {
return ctx.reply(ctx.t('language.select'), {
reply_markup: await createChangeLanguageKeyboard(ctx),
});
});
})
})

feature.callbackQuery(
changeLanguageData.filter(),
logHandle("keyboard-language-select"),
logHandle('keyboard-language-select'),
async (ctx) => {
const { code: languageCode } = changeLanguageData.unpack(
ctx.callbackQuery.data,
);
)

if (i18n.locales.includes(languageCode)) {
await ctx.i18n.setLocale(languageCode);
await ctx.i18n.setLocale(languageCode)

return ctx.editMessageText(ctx.t("language.changed"), {
return ctx.editMessageText(ctx.t('language.changed'), {
reply_markup: await createChangeLanguageKeyboard(ctx),
});
})
}
},
);
)

export { composer as languageFeature };
export { composer as languageFeature }
24 changes: 12 additions & 12 deletions src/bot/features/unhandled.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { Composer } from "grammy";
import type { Context } from "#root/bot/context.js";
import { logHandle } from "#root/bot/helpers/logging.js";
import { Composer } from 'grammy'
import type { Context } from '#root/bot/context.js'
import { logHandle } from '#root/bot/helpers/logging.js'

const composer = new Composer<Context>();
const composer = new Composer<Context>()

const feature = composer.chatType("private");
const feature = composer.chatType('private')

feature.on("message", logHandle("unhandled-message"), (ctx) => {
return ctx.reply(ctx.t("unhandled"));
});
feature.on('message', logHandle('unhandled-message'), (ctx) => {
return ctx.reply(ctx.t('unhandled'))
})

feature.on("callback_query", logHandle("unhandled-callback-query"), (ctx) => {
return ctx.answerCallbackQuery();
});
feature.on('callback_query', logHandle('unhandled-callback-query'), (ctx) => {
return ctx.answerCallbackQuery()
})

export { composer as unhandledFeature };
export { composer as unhandledFeature }
18 changes: 9 additions & 9 deletions src/bot/features/welcome.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Composer } from "grammy";
import type { Context } from "#root/bot/context.js";
import { logHandle } from "#root/bot/helpers/logging.js";
import { Composer } from 'grammy'
import type { Context } from '#root/bot/context.js'
import { logHandle } from '#root/bot/helpers/logging.js'

const composer = new Composer<Context>();
const composer = new Composer<Context>()

const feature = composer.chatType("private");
const feature = composer.chatType('private')

feature.command("start", logHandle("command-start"), (ctx) => {
return ctx.reply(ctx.t("welcome"));
});
feature.command('start', logHandle('command-start'), (ctx) => {
return ctx.reply(ctx.t('welcome'))
})

export { composer as welcomeFeature };
export { composer as welcomeFeature }
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-admin.js";
export * from './is-admin.js'
6 changes: 3 additions & 3 deletions src/bot/filters/is-admin.ts
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";
import { isUserHasId } from 'grammy-guard'
import { config } from '#root/config.js'

export const isAdmin = isUserHasId(...config.BOT_ADMINS);
export const isAdmin = isUserHasId(...config.BOT_ADMINS)
Loading

0 comments on commit 5357649

Please sign in to comment.