Skip to content
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

feat: add bun related docs #1105

Closed
wants to merge 3 commits into from
Closed
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
83 changes: 83 additions & 0 deletions site/docs/guide/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,89 @@ in your terminal before you execute `node bot.js`.
This makes it easier to debug your bot.
:::

## Getting Started on Bun

> This guide assumes that you have [Bun](https://bun.sh) installed.
> If you don't know what these things are, check out our [Introduction](./introduction)!

Create a new project and install the `grammy` package.
Do this by opening a terminal and typing:

```sh
# Create a new directory and change into it.
mkdir my-bot
cd my-bot

# Run bun init to scaffold a new project, for this tutorial, just press enter to accept the default answer for each prompt.
bun init

# Install grammY.
bun add grammy
```

Create a new empty text file, e.g. called `bot.ts`.
Copy link
Collaborator

@MasedMSD MasedMSD Sep 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aren't bun automatically creating index.ts if we "just press enter to accept the default answer for each prompt."

Your folder structure should now look like this:

```asciiart:no-line-numbers
.
├── bot.ts
├── node_modules/
├── package.json
├── bun.lockb
└── tsconfig.json
```

Now, it's time to open Telegram to create a bot account, and obtain a bot token for it.
Talk to [@BotFather](https://t.me/BotFather) to do this.
The bot token looks like `123456:aBcDeF_gHiJkLmNoP-q`.
It is used to authenticate your bot.

Got the token? You can now code your bot in the `bot.ts` file.
You can copy the following example bot into that file, and pass your token to the `Bot` constructor:

```ts [TypeScript]
import { Bot } from "grammy";

// Create an instance of the `Bot` class and pass your bot token to it.
const bot = new Bot(""); // <-- put your bot token between the ""

// You can now register listeners on your bot object `bot`.
// grammY will call the listeners when users send messages to your bot.

// Handle the /start command.
bot.command("start", (ctx) => ctx.reply("Welcome! Up and running."));
// Handle other messages.
bot.on("message", (ctx) => ctx.reply("Got another message!"));

// Now that you specified how to handle messages, you can start your bot.
// This will connect to the Telegram servers and wait for messages.

// Start the bot.
bot.start();
```

You can now run the bot by executing

```sh
bun run bot.ts
```

in your terminal.
Done! :tada:

Head over to Telegram to watch your bot respond to messages!

::: tip Enabling Logging
You can enable basic logging by running

```sh
export DEBUG="grammy*"
```

in your terminal before you execute `bun run bot.ts`.
This makes it easier to debug your bot.
:::

## Getting Started on Deno

> This guide assumes that you have [Deno](https://deno.com) installed.
Expand Down
28 changes: 28 additions & 0 deletions site/docs/guide/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,31 @@ If you feel lost at this point, you should leave Node.js behind and use [Deno](#
Still confident?
Great!
[Get started](./getting-started#getting-started-on-node-js)! :robot:

### Prerequisites for Bun

Bun is a new JavaScript runtime, which is another good choice to build bot, like Deno, you are going to write your bot in TypeScript.
The exact commands for all of that will be introduced in the next section when you actually create a bot, but it is important to know that these steps are necessary.

Firstly, you have to have [Bun](https://bun.sh/) installed.

In summary, this is what you have to do for Bun:

Create a new directory somewhere.
It will contain your bot project.
Open this new directory in VS Code.

```sh
mkdir ./my-bot
cd ./my-bot
code .
```

Then:

1. Run `bun init --yes` in your terminal to initialize the project.
2. Create a source file `bot.ts` with TypeScript code inside the project.
3. Run `bun run bot.ts` from your terminal, or run `bun --watch run bot.ts` if you want to keep updated with file changes.

Ready?
[Get started](./getting-started#getting-started-on-bun)! :robot:
Loading