# Guide

![Nestgram](/files/AwPJ0bI2MhE2bULnBvZc)

{% hint style="info" %}
You can read the guide here or on the [Medium](https://medium.com/p/ff251fb825fd)
{% endhint %}

## Install Nestgram

You need to install nestgram at first. You can do this using `yarn` or `npm`

```nginx
yarn add nestgram@1.9.0
// or
npm i nestgram@1.9.0
```

## Create main file

Our next step is creating the main file, so let's create the `main.ts` file

{% code title="main.ts" %}

```typescript
import { NestGram } from 'nestgram';
import { AppModule } from './app/app.module';

async function bootstrap(): Promise<void> {
  const bot = new NestGram('TOKEN', AppModule);
  await bot.start();
}

bootstrap();
```

{% endcode %}

At first, we imported nestgram and our AppModule, later we will create it. In the next step, we created a bootstrap function, in which we set up and run the project. The NestGram class gets bot token as 1st parameter (you can get it via [BotFather](https://t.me/BotFather)), app module as 2nd parameter, options as 3rd parameter, and [run config](/nestgram/config/webhooks-and-run-config.md) as 4th parameter

#### Nestgram class take arguments:

<table><thead><tr><th width="150" data-type="number">Argument</th><th>Name</th><th>Required</th></tr></thead><tbody><tr><td>1</td><td>Bot token. You can get it <a href="https://t.me/BotFather">here</a></td><td><strong>Required</strong></td></tr><tr><td>2</td><td><a href="#create-app.module.ts">App module</a></td><td><strong>Required</strong></td></tr><tr><td>3</td><td>Config. <a href="https://core.telegram.org/bots/api#getupdates">IPollingConfig</a> or <a href="https://core.telegram.org/bots/api#setwebhook">IWebhookConfig</a></td><td>Optional</td></tr><tr><td>4</td><td><a href="/pages/ifyxBRDzhAWASG3ezRhJ#run-config">Run config</a></td><td>Optional</td></tr></tbody></table>

## Create app.module.ts

Let's create the `app.module.ts` file. In it, we will describe all available controllers and services

{% code title="app.module.ts" %}

```typescript
import { Module } from 'nestgram';
import { AppController } from './app.controller';
import { AppService } from './app.service';

@Module({
  controllers: [AppController],
  services: [AppService],
})
export class AppModule {}
```

{% endcode %}

At first, we imported Module class from nestgram, AppController and AppService also, that we will create later. Then we described our controllers and services in @Module decorator

## Create app.controller.ts

Let's create the app.controller.ts file. In it, we will describe updates, that we want to handle

{% code title="app.controller.ts" %}

```typescript
import { OnCommand, Controller } from 'nestgram';
import { AppService } from './app.service';

@Controller()
export class AppController {
  constructor(private readonly appService?: AppService) {}

  @OnCommand('start')
  start(): string {
    return 'Hello, world!';
  }
}
```

{% endcode %}

We have created a controller where we describe an update when the user writes **/start**, and we handle it by sending a message "Hello, world!"

## Create app.service.ts

Let's create the app.service.ts file. In it, we will describe methods with working with db, that we will call in controller

{% code title="app.service.ts" %}

```typescript
import { Service } from 'nestgram';

@Service()
export class AppService {}
```

{% endcode %}

We can describe methods in AppService class, and call it in controller by `this.appService`

## Run project

To run the project, open a terminal in the project directory and type:

```nginx
npm run dev
```

Or build and run production:

```nginx
npm run build && npm run prod
```

## What's next?

Now you know about the syntax and structure of the Nestgram project, but if you want to write your own pro bot, you can check out the [Nestgram documentation](/nestgram/handling-updates/handling-commands.md)


---

# 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://degreetpro.gitbook.io/nestgram/guide.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.
