# Setup (/docs/cookbook/risex-telegram-bot/setup)



import { Tabs, Tab } from 'fumadocs-ui/components/tabs';
import { Steps, Step } from 'fumadocs-ui/components/steps';
import { Callout } from 'fumadocs-ui/components/callout';

## Project Setup

### Create Project Structure

This project uses a monorepo structure with two apps:

```bash
mkdir risex-tg-bot
cd risex-tg-bot
npm init -y
```

Create the following folder structure:

```
risex-tg-bot/
├── apps/
│   ├── tg-bot/          # Telegram bot backend
│   └── frontend/        # Registration UI
├── package.json
└── .gitignore
```

Update your root `package.json`:

```json title="package.json"
{
  "name": "risex-tg-bot",
  "version": "1.0.0",
  "scripts": {
    "dev:tg-bot": "cd apps/tg-bot && npm run dev",
    "dev:frontend": "cd apps/frontend && npm run dev",
    "dev": "concurrently \"npm run dev:tg-bot\" \"npm run dev:frontend\"",
    "build": "npm run build:tg-bot && npm run build:frontend",
    "build:tg-bot": "cd apps/tg-bot && npm run build",
    "build:frontend": "cd apps/frontend && npm run build"
  },
  "devDependencies": {
    "concurrently": "^9.1.0"
  }
}
```

Install the root dependency:

<Tabs items={['npm', 'yarn', 'pnpm', 'bun']} groupId="package-manager">
  <Tab value="npm">
    ```bash
    npm install
    ```
  </Tab>

  <Tab value="yarn">
    ```bash
    yarn install
    ```
  </Tab>

  <Tab value="pnpm">
    ```bash
    pnpm install
    ```
  </Tab>

  <Tab value="bun">
    ```bash
    bun install
    ```
  </Tab>
</Tabs>

***

## Set Up the Telegram Bot

First, create a Telegram bot through BotFather:

<Steps>
  <Step>
    ### Create the Bot

    1. Open Telegram and search for `@BotFather`
    2. Send `/newbot` and follow the prompts
    3. Choose a name (e.g., "RISEx Trader")
    4. Choose a username (must end in "bot", e.g., "risex\_trader\_bot")
    5. Copy the bot token as you'll need it later

        <img alt="RISEx Telegram Bot Profile" src={__img0} placeholder="blur" />
  </Step>
</Steps>

***

## Initialize the Backend

### Create the Backend App

Create the Telegram bot app:

```bash
cd apps
mkdir tg-bot
cd tg-bot
npm init -y
```

### Install Dependencies

<Tabs items={['npm', 'yarn', 'pnpm', 'bun']} groupId="package-manager">
  <Tab value="npm">
    ```bash
    npm install telegraf axios openai viem zod express cors dotenv
    npm install -D typescript @types/node tsx
    ```
  </Tab>

  <Tab value="yarn">
    ```bash
    yarn add telegraf axios openai viem zod express cors dotenv
    yarn add -D typescript @types/node tsx
    ```
  </Tab>

  <Tab value="pnpm">
    ```bash
    pnpm add telegraf axios openai viem zod express cors dotenv
    pnpm add -D typescript @types/node tsx
    ```
  </Tab>

  <Tab value="bun">
    ```bash
    bun add telegraf axios openai viem zod express cors dotenv
    bun add -D typescript @types/node tsx
    ```
  </Tab>
</Tabs>

**Key packages:**

* `telegraf` - Telegram bot framework
* `axios` - HTTP client for RISEx API calls
* `openai` - OpenRouter/OpenAI SDK for GPT integration
* `viem` - EIP-712 signing and cryptography
* `zod` - Runtime type validation
* `express` - HTTP server for frontend API routes

### Configure TypeScript

Create `tsconfig.json`:

```json title="apps/tg-bot/tsconfig.json"
{
  "compilerOptions": {
    "target": "ES2021",
    "module": "commonjs",
    "lib": ["ES2021"],
    "outDir": "./dist",
    "rootDir": "./src",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "resolveJsonModule": true
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules"]
}
```

Update `package.json` scripts:

```json title="apps/tg-bot/package.json"
{
  "scripts": {
    "dev": "tsx watch src/index.ts",
    "build": "tsc",
    "start": "node dist/index.js"
  }
}
```

### Create Data Storage

Create a `data` directory for JSON storage:

```bash
mkdir -p src/data
```

Create empty JSON files:

```json title="apps/tg-bot/src/data/signers.json"
{}
```

```json title="apps/tg-bot/src/data/verified-links.json"
{}
```

These files will store encrypted signer keys and Telegram-to-wallet mappings.

***

## Initialize the Frontend

Navigate back to apps directory and create the frontend:

```bash
cd ..
npx create-next-app@latest frontend
```

Select these options:

* Use recommended Next.js defaults: **No, customize settings**
* TypeScript: **Yes**
* Linter: **None**
* React Compiler: **Yes**
* Tailwind CSS: **Yes**
* `src/` directory: **Yes**
* App Router: **Yes**
* Customize import alias: **No**

<Callout type="info">
  If you've completed another tutorial, you can select **No, reuse previous settings** to skip configuration.
</Callout>

### Install Frontend Dependencies

```bash
cd frontend
```

<Tabs items={['npm', 'yarn', 'pnpm', 'bun']} groupId="package-manager">
  <Tab value="npm">
    ```bash
    npm install rise-wallet wagmi viem ox @tanstack/react-query
    ```
  </Tab>

  <Tab value="yarn">
    ```bash
    yarn add rise-wallet wagmi viem ox @tanstack/react-query
    ```
  </Tab>

  <Tab value="pnpm">
    ```bash
    pnpm add rise-wallet wagmi viem ox @tanstack/react-query
    ```
  </Tab>

  <Tab value="bun">
    ```bash
    bun add rise-wallet wagmi viem ox @tanstack/react-query
    ```
  </Tab>
</Tabs>

***

## Environment Variables

Create environment files for both apps.

### Backend Environment

Create `apps/tg-bot/.env`:

```bash title="apps/tg-bot/.env"
# Telegram
TELEGRAM_BOT_TOKEN=your_bot_token_from_botfather

# RISEx
RISEX_API_URL=https://api.testnet.rise.trade

# Security
ENCRYPTION_SECRET=generate_with_node_command_below

# Server
PORT=8008
FRONTEND_URL=http://localhost:3000

# LLM
OPENROUTER_API_KEY=sk-or-v1-your_key_from_openrouter
```

Generate encryption secret:

```bash
node -e "console.log(require('crypto').randomBytes(32).toString('hex'))"
```

### Frontend Environment

Create `apps/frontend/.env.local`:

```bash title="apps/frontend/.env.local"
NEXT_PUBLIC_TELEGRAM_BOT_NAME=your_bot_username
NEXT_PUBLIC_API_URL=http://localhost:8008
```

***

Your project structure is now ready! In the [next section](/docs/cookbook/risex-telegram-bot/backend), we'll build the backend services.
