# How to build Telegram bot (with the GAME Typescript SDK)

Hello! Today we're going to guide you how to build a telegram bot using an easy-to-use plugin from the GAME SDK. You'll be chatting with your telegram bot by the end of the tutorial!

## 1. Register a new telegram bot with telegram

\
First, we're going to register a new telegram bot. Go to your Telegram account and DM @BotFather

<figure><img src="https://1626894119-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FN859t4u3tRXPoiDxXGQr%2Fuploads%2FhYdt5Mcf2lfrtLdSdwJp%2Fimage.png?alt=media&#x26;token=0a3b6752-8dde-47cf-b01d-0d6bea639163" alt="" width="375"><figcaption><p>The famous bot father of telegram! Where all TG bots are born</p></figcaption></figure>

In the chat with the BotFather, click `/start` and click `/newbot!` It will guide you through creating a new bot. \
\
First, give your bot a name (it doesn't have to be unique and you can change this anytime.) \
\
Then, BotFather will ask you to give the bot a unique username. The username must end in "bot." And the username cannot be changed.\
\
A bot's username can look like this:\
\- my\_agent\_bot\
\- myAgentBot\
\- myAgent\_Bot\
\
or any combination of the above!

<figure><img src="https://1626894119-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FN859t4u3tRXPoiDxXGQr%2Fuploads%2Fdvyi6wtglMIMLoZRVHJ2%2FScreen%20Shot%202025-02-02%20at%2012.41.47%20PM.png?alt=media&#x26;token=475a2378-6ed0-4411-ac2b-5277233575d7" alt="" width="375"><figcaption><p>Click or type in /newbot to start</p></figcaption></figure>

Once you've set the name and username of your bot, the BotFather will give you an API key. Save it, we will use it in our project :)

<figure><img src="https://1626894119-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FN859t4u3tRXPoiDxXGQr%2Fuploads%2FYndL93RvyPa9EdR09o1W%2FScreen%20Shot%202025-02-02%20at%201.13.05%20PM.png?alt=media&#x26;token=4359a486-5a37-4fb5-a4d9-1b36c0ff6303" alt="" width="375"><figcaption><p>My bot's generated API key</p></figcaption></figure>

By the way, while we are here, you can also edit your bot anytime! Click or type in /mybots and you'll get to a menu where you can edit your bot's name anytime and set your bot's image and description. You do not have to do any of this right now to complete this tutorial.

<figure><img src="https://1626894119-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FN859t4u3tRXPoiDxXGQr%2Fuploads%2Ff1cASrQnbswWomtfJUqw%2FScreen%20Shot%202025-02-02%20at%201.16.29%20PM.png?alt=media&#x26;token=5eafb453-7aaf-4983-9060-c9cf81a591f5" alt="" width="375"><figcaption><p>Go to /mybots to edit your bot anytime</p></figcaption></figure>

<figure><img src="https://1626894119-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FN859t4u3tRXPoiDxXGQr%2Fuploads%2Fumv4IeTZoiZf1nSqylBm%2FScreen%20Shot%202025-02-02%20at%201.18.44%20PM.png?alt=media&#x26;token=90016c97-144d-4620-9e8c-e49248ea6c95" alt="" width="375"><figcaption><p>Here is where you can set your bot's profile! Give it an image and description</p></figcaption></figure>

## 2. Git clone repo

Now, let's go to our dev environment and git clone the game-node repository.&#x20;

```sh
// clone the repository
git clone https://github.com/game-by-virtuals/game-node

// go to the plugin directory
cd game-node/plugins 
```

{% embed url="<https://github.com/game-by-virtuals/game-node>" %}
The GAME node repository
{% endembed %}

Now, we are in the `plugins` folder. There are a few awesome ones here that you can play with right away - such as the `discordPlugin`, `twitterPlugin` and the `onChainActionsPlugin` (where you can handle crypto wallets and do on-chain transactions.) But for today, we will just focus on the telegramPlugin.&#x20;

<figure><img src="https://1626894119-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FN859t4u3tRXPoiDxXGQr%2Fuploads%2F5Bc3SlH9ssRsUxeh5WmD%2FScreen%20Shot%202025-02-02%20at%202.32.21%20PM.png?alt=media&#x26;token=ee3bd621-ad49-4605-aa1f-71268dabc389" alt=""><figcaption><p>The game-node plugin folder</p></figcaption></figure>

> At the moment of this tutorial, we recommend you to be on node version 23 and npm version 10.9.2. We recommend the [node version manager, NVM](https://github.com/nvm-sh/nvm).

Let's go into the `telegramPlugin` folder and install the dependencies.<br>

```sh
// go into the telegram plugin folder
cd telegramPlugin

// install dependencies
npm install
```

We are going to run the example code that comes in the `telegramPlugin`. \
\
Open `src/example.ts`. \
\
Replace `<BOT_TOKEN>` with the Telegram API key that we generated from the BotFather. &#x20;

Replace `<API_TOKEN>` with your GAME API key. If you do not have one, you can generate one here: <https://console.game.virtuals.io/>

#### In the example.js file

The example file actually contains three agents. There is an autoReply agent, a financial agent, and a nutritionist agent. When we run this program, all three agents will run at the same time and they will all try to respond to you!&#x20;

When you are modifying this program, you can delete all but one agent. You can customize your agent by changing the name, goal, and description!<br>

<figure><img src="https://1626894119-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FN859t4u3tRXPoiDxXGQr%2Fuploads%2FIvFPg3nAVLxmB0SKnV5S%2FScreen%20Shot%202025-02-02%20at%202.55.35%20PM.png?alt=media&#x26;token=2f68857f-3785-4eb8-8040-807825ba36b2" alt=""><figcaption><p>The example.ts file with the API key and the bot token </p></figcaption></figure>

## 3. Run the program

Great! Now that you've entered your GAME API key and the bot token, we are ready to run the code.&#x20;

At this point, you can edit the agents in the `example.ts` file. The example comes with three agents (a generic autoReply agent, a financial agent, and a nutrionist agent) you can par the code down to just one agent. Give your agent some personality and specialty by editing the name, goal, and description.

Let's compile the typescript. Edit the package.json so that the script for "tsup" will compile `example.ts` instead of `index.ts`. Save the changes.&#x20;

<figure><img src="https://1626894119-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FN859t4u3tRXPoiDxXGQr%2Fuploads%2FPHLazbrCr3r8H7FiFVyf%2FScreen%20Shot%202025-02-02%20at%207.05.07%20PM.png?alt=media&#x26;token=57b8138c-e426-40bb-ae82-01b7ac8099ca" alt=""><figcaption><p>your package.json should look like this</p></figcaption></figure>

In the terminal, run the following commands:<br>

```shell
// install dependencies
npm install

// this command compiles the example.ts
// and places it in a new "dist" folder
npm run tsup

// run the compiled example.js
node dist/example.js
```

You should start seeing a running log of activity in the terminal.&#x20;

## 4. Let's talk to your agent!

Go back to telegram and send a message to your bot. If you are able to see some responses from your Telegram bot, it means that your bot is working!

<figure><img src="https://1626894119-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FN859t4u3tRXPoiDxXGQr%2Fuploads%2FeT1a09xmQweBmsvhDEYq%2FScreen%20Shot%202025-02-02%20at%207.08.11%20PM.png?alt=media&#x26;token=ae93fde9-5e4d-4fcf-9c49-c597e5a5e3d7" alt="" width="375"><figcaption></figcaption></figure>

<figure><img src="https://1626894119-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FN859t4u3tRXPoiDxXGQr%2Fuploads%2FecHAnLVqm2WTEPb6s29e%2FScreen%20Shot%202025-02-02%20at%207.09.05%20PM.png?alt=media&#x26;token=8e7ea71b-f316-46bc-a501-9fb3694f170c" alt=""><figcaption><p>What the terminal logs should look like as your bot is receiving messages and responding</p></figcaption></figure>

## :tada: Congratulations, you've just built your first telegram bot with the GAME SDK!

### Stay Connected and Join the Virtuals Community! 🤖 🎈

<table data-view="cards"><thead><tr><th></th><th data-type="content-ref"></th><th></th><th data-hidden data-card-cover data-type="files"></th></tr></thead><tbody><tr><td><mark style="background-color:blue;"><strong>X: @GAME_Virtuals</strong></mark></td><td><a href="https://x.com/GAME_Virtuals">https://x.com/GAME_Virtuals</a></td><td>For updates and to join our live streaming jam sessions every Wednesday. Stay in the loop and engage with us in real time!</td><td><a href="https://1626894119-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FN859t4u3tRXPoiDxXGQr%2Fuploads%2FV1iyZvP9Tfn8uCrEu8yA%2FWhat-is-X.avif?alt=media&#x26;token=097dd525-76ec-43fb-a73e-5d9ea9077449">What-is-X.avif</a></td></tr><tr><td><mark style="background-color:blue;"><strong>Discord: @Virtuals Protocol</strong></mark></td><td><a href="http://discord.gg/virtualsio">http://discord.gg/virtualsio</a></td><td>Join Discord for tech support and troubleshooting, and don’t miss our GAME Jam session every Wednesday!</td><td><a href="https://1626894119-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FN859t4u3tRXPoiDxXGQr%2Fuploads%2FlsmSFAtrOUMgW5qauUY3%2Fdiscord.png?alt=media&#x26;token=ec9d5b60-d711-464d-a6f1-ce22f811176b">discord.png</a></td></tr><tr><td><mark style="background-color:blue;"><strong>Telegram: @Virtuals Protocol</strong></mark></td><td><a href="https://t.me/virtuals">https://t.me/virtuals</a></td><td>Join our Telegram group for non-tech support! Whether you need advice, a chat, or just a friendly space, we’re here to help!</td><td><a href="https://1626894119-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FN859t4u3tRXPoiDxXGQr%2Fuploads%2Fpbjvgqhv2jfIgWopFgfG%2Ftelegram.png?alt=media&#x26;token=d55c61bf-541f-46fc-806c-0918c12a64e6">telegram.png</a></td></tr></tbody></table>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.game.virtuals.io/how-to/articles/how-to-build-telegram-bot-with-the-game-typescript-sdk.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
