diff --git a/site/docs/guide/files.md b/site/docs/guide/files.md index 2d38c81ad..31806c3de 100644 --- a/site/docs/guide/files.md +++ b/site/docs/guide/files.md @@ -181,12 +181,20 @@ new InputFile("/path/to/file"); new InputFile(await Deno.open("/path/to/file")); ``` +```ts [Bun] +// Send a local file. +new InputFile("/path/to/file"); + +// Send a `BunFile` instance. +new InputFile(await Bun.file("/path/to/file").bytes()); +``` + ::: #### Uploading Raw Binary Data You can also send a `Buffer` object, or an iterator that yields `Buffer` objects. -On Deno, you can send `Blob` objects, too. +On Deno and Bun you can send `Blob` objects, too. ::: code-group @@ -215,6 +223,20 @@ new InputFile(function* () { }); ``` +```ts [Bun] +// Send a blob. +const blob = new Blob("ABC", { type: "text/plain" }); +new InputFile(blob); +// Send a buffer or a byte array. +const buffer = Uint8Array.from([65, 66, 67]); +new InputFile(buffer); // "ABC" +// Send an iterable. +new InputFile(function* () { + // "ABCABCABCABC" + for (let i = 0; i < 4; i++) yield buffer; +}); +``` + ::: #### Downloading and Reuploading a File diff --git a/site/docs/guide/getting-started.md b/site/docs/guide/getting-started.md index b072e9353..471a576bd 100644 --- a/site/docs/guide/getting-started.md +++ b/site/docs/guide/getting-started.md @@ -150,6 +150,119 @@ 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 +``` + +Next, you should initialize your project. + +```sh +# Use bun to init Typescript config +bunx tsc --init +# Run bun init to scaffold a new project. +bun init +``` + +After running the command, the script will prompt you to select a project name and an entry point. + +```ansi{3-4,12} +bun init helps you get started with a minimal project and tries to guess sensible defaults. Press ^C anytime to quit + +package name (my-bot): my-bot // [!code focus] +entry point (index.ts): bot.ts // [!code focus] + +Done! A package.json file was saved in the current directory. + + bot.ts + + .gitignore + + tsconfig.json (for editor auto-complete) + + README.md + +To get started, run: // [!code focus] + bun run bot.ts // [!code focus] +``` + +Finally, you can add the `grammy` package. + +```sh +# Install grammY. +bun add grammy +``` + +Your folder structure should look like this: + +```asciiart:no-line-numbers +. +├── node_modules/ +├── .gitignore +├── bot.ts +├── bun.lockb +├── package.json +├── README.md +└── 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. +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 will make it easier to debug your bot. +::: + ## Getting Started on Deno > This guide assumes that you have [Deno](https://deno.com) installed. diff --git a/site/docs/guide/introduction.md b/site/docs/guide/introduction.md index 6e174b877..14fd8c234 100644 --- a/site/docs/guide/introduction.md +++ b/site/docs/guide/introduction.md @@ -66,7 +66,7 @@ You will get to know them as you go. ## Prerequisites to Getting Started -> Skip the rest of this page if you already know how to develop a Deno or a Node.js application, and [get started](./getting-started). +> Skip the rest of this page if you already know how to develop Deno, Bun, or Node.js application, and [get started](./getting-started). Here are a few interesting things about programming---things that are essential to coding, yet rarely explained because most developers think they are self-evident. @@ -173,6 +173,48 @@ You can stop the bot again with `Ctrl+C`. Ready? [Get started](./getting-started#getting-started-on-deno)! :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` in your terminal to initialize the project and fill it out as written below. + + ```ansi{3-4,12} + bun init helps you get started with a minimal project and tries to guess sensible defaults. Press ^C anytime to quit + + package name (my-bot): my-bot // [!code focus] + entry point (index.ts): bot.ts // [!code focus] + + Done! A package.json file was saved in the current directory. + + bot.ts + + .gitignore + + tsconfig.json (for editor auto-complete) + + README.md + ``` + +2. 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: + ### Prerequisites for Node.js You are going to write your bot in TypeScript, but, contrary to Deno, Node.js cannot actually run TypeScript. diff --git a/site/docs/hosting/firebase.md b/site/docs/hosting/firebase.md index 415f57e6d..74ac1353e 100644 --- a/site/docs/hosting/firebase.md +++ b/site/docs/hosting/firebase.md @@ -49,7 +49,7 @@ npm install -g firebase-tools - TypeScript 6. Optionally, you can select ESLint. 7. The CLI asks you if you want to install the dependencies with npm. - If you use another package manager like `yarn` or `pnpm` you can decline. + If you use another package manager like `yarn`, `bun`, or `pnpm` you can decline. In that case, you have to `cd` into the `functions` directory and install the dependencies manually. 8. Open `./functions/package.json` and look for the key: `"engines": {"node": "16"}`. The `node` version should match your installed version of Node.js. diff --git a/site/docs/hosting/vps.md b/site/docs/hosting/vps.md index ba9623f3a..977d5ff9a 100644 --- a/site/docs/hosting/vps.md +++ b/site/docs/hosting/vps.md @@ -260,6 +260,10 @@ yarn global add pm2 pnpm add -g pm2 ``` +```sh [bun] +bun add -g pm2 +``` + ::: #### Creating an Application