Skip to content

Commit 7cf0230

Browse files
committed
feat: introduce Telegram Bot for Green Goods
- Added a new Telegram Bot package to facilitate user interactions with Green Goods, allowing users to join gardens, submit work, and approve submissions via Telegram. - Implemented core functionalities including custodial wallet management, natural language submission, and voice-to-text capabilities using OpenAI Whisper. - Established a structured architecture with a focus on ease of use and low-bandwidth accessibility, utilizing Telegraf for bot logic and better-sqlite3 for session management. - Included comprehensive documentation outlining the bot's features, architecture, and setup instructions.
1 parent 03e588a commit 7cf0230

File tree

12 files changed

+700
-6
lines changed

12 files changed

+700
-6
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Telegram Bot
2+
3+
The **Telegram Bot** package (`packages/telegram`) provides a conversational interface for Green Goods, enabling users to join gardens, submit work, and approve submissions directly from Telegram.
4+
5+
## Overview
6+
7+
The bot is designed to be a "Bot-First" alternative to the web client, focusing on ease of use and low-bandwidth accessibility. It leverages AI for natural language understanding and voice processing.
8+
9+
### Key Features
10+
11+
* **Custodial Wallet Management**: Automatically generates and manages wallets for users (MVP).
12+
* **Natural Language Submission**: Users can describe their work in plain text.
13+
* **Voice-to-Text**: Supports voice notes for work submission using OpenAI Whisper (via `@xenova/transformers`).
14+
* **Operator Approvals**: Operators receive notifications and can approve work with a single command.
15+
16+
## Architecture
17+
18+
The bot is built with **Telegraf** and runs as a standalone Node.js process.
19+
20+
### Tech Stack
21+
22+
* **Framework**: [Telegraf](https://telegraf.js.org/)
23+
* **Database**: `better-sqlite3` (local SQLite for sessions and user data)
24+
* **AI/ML**: `@xenova/transformers` (local inference for STT)
25+
* **Blockchain**: `viem` (interaction with EAS and Green Goods contracts)
26+
27+
### Data Flow
28+
29+
1. **User Input**: Text or Voice message received via Telegram Webhook/Polling.
30+
2. **Processing**:
31+
* **Voice**: Transcribed to text using Whisper.
32+
* **Text**: Parsed (currently Regex/Logic, planned LLM) to extract work details.
33+
3. **Session**: Draft work stored in SQLite session.
34+
4. **Confirmation**: User confirms details via Inline Keyboard.
35+
5. **Submission**:
36+
* Bot creates a `WorkDraft`.
37+
* Bot signs and submits transaction to EAS using the user's local custodial wallet.
38+
* **Pending State**: If approval is needed, work is stored as "pending" and operator is notified.
39+
6. **Approval**: Operator sends `/approve <id>`, bot submits on-chain attestation.
40+
41+
## Directory Structure
42+
43+
```
44+
packages/telegram/
45+
├── src/
46+
│ ├── services/
47+
│ │ ├── ai.ts # STT and NLU logic
48+
│ │ └── storage.ts # SQLite database wrapper
49+
│ ├── bot.ts # Main bot logic and command handlers
50+
│ └── index.ts # Entry point
51+
├── bot.db # Local database (gitignored)
52+
└── package.json
53+
```
54+
55+
## Setup & Configuration
56+
57+
The bot requires a Telegram Bot Token from [@BotFather](https://t.me/BotFather).
58+
59+
### Environment Variables
60+
61+
| Variable | Description |
62+
| :--- | :--- |
63+
| `TELEGRAM_BOT_TOKEN` | API Token from BotFather |
64+
| `DB_PATH` | Path to SQLite DB (default: `bot.db`) |
65+
66+
### Running Locally
67+
68+
```bash
69+
# From root
70+
bun run dev:telegram
71+
```

package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@
2020
"setup": "node scripts/setup.js",
2121
"prepare": "husky",
2222

23-
"dev": "trap 'bunx pm2 delete all' EXIT; bunx pm2 start ecosystem.config.js && bunx pm2 logs --raw",
24-
"dev:stop": "bunx pm2 delete all",
23+
"dev": "trap 'npx pm2 delete all' EXIT; npx pm2 start ecosystem.config.js && npx pm2 logs --raw",
24+
"dev:stop": "npx pm2 delete all",
2525
"dev:client": "cd packages/client && bun run dev",
2626
"dev:admin": "cd packages/admin && bun run dev",
2727
"dev:indexer": "cd packages/indexer && bun run dev",
2828
"dev:contracts": "cd packages/contracts && bun run dev",
2929

30-
"format": "bunx @biomejs/biome format --write .",
31-
"format:check": "bunx @biomejs/biome format .",
30+
"format": "npx @biomejs/biome format --write .",
31+
"format:check": "npx @biomejs/biome format .",
3232
"lint": "bun oxlint packages/client/src packages/admin/src packages/shared/src packages/indexer/src packages/contracts/script",
3333
"test": "bun run test:client && bun run test:admin && bun run test:indexer && bun run test:contracts",
3434

@@ -75,10 +75,10 @@
7575
},
7676
"lint-staged": {
7777
"*.{js,jsx,ts,tsx,json}": [
78-
"bunx @biomejs/biome format --write"
78+
"npx @biomejs/biome format --write"
7979
],
8080
"*.sol": [
81-
"cd packages/contracts && bun run format"
81+
"npx forge fmt"
8282
]
8383
},
8484
"workspaces": [

packages/telegram/AGENTS.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Green Goods — Telegram Bot Agent Guide
2+
3+
Context for AI agents working on the `packages/telegram` directory.
4+
5+
## Core Responsibilities
6+
7+
- **Bot Interface**: Primary interface for users to join gardens, submit work, and approve work via Telegram.
8+
- **Custodial Wallet Management**: Manages local wallets for users (MVP).
9+
- **AI Integration**: Handles STT (Speech-to-Text) and NLU (Natural Language Understanding) for conversational flows.
10+
11+
## Key Patterns
12+
13+
- **Telegraf**: Uses `telegraf` framework for bot logic.
14+
- **Session Management**: Uses `better-sqlite3` for persistent session state and pending work storage.
15+
- **Shared Logic**: Imports business logic (submission, encoding) from `@green-goods/shared`.
16+
- **Environment**: Loads environment variables from the root `.env` file.
17+
18+
## Development Rules
19+
20+
1. **Statelessness**: Avoid in-memory state for critical flows; persist to SQLite.
21+
2. **Type Safety**: Use shared types from `@green-goods/shared` where possible.
22+
3. **Error Handling**: Gracefully handle AI model failures (fallback to regex/text).
23+
4. **Security**: Treat private keys in `bot.db` with extreme caution (MVP only).
24+
25+
## Common Tasks
26+
27+
- **Adding Commands**: Register new commands in `bot.ts`.
28+
- **Updating AI**: Modify `services/ai.ts` for model updates or logic changes.
29+
- **Schema Changes**: Update `services/storage.ts` and run migrations (if any) manually for now.

packages/telegram/data/bot.db

20 KB
Binary file not shown.

packages/telegram/package.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "@green-goods/telegram",
3+
"version": "0.0.0",
4+
"private": true,
5+
"main": "src/index.ts",
6+
"scripts": {
7+
"dev": "bun --watch src/index.ts",
8+
"build": "tsc",
9+
"start": "node dist/index.js",
10+
"lint": "oxlint src"
11+
},
12+
"dependencies": {
13+
"@green-goods/shared": "workspace:*",
14+
"@xenova/transformers": "^2.17.1",
15+
"dotenv": "^16.4.5",
16+
"fluent-ffmpeg": "^2.1.2",
17+
"sharp": "^0.34.5",
18+
"telegraf": "^4.16.3",
19+
"viem": "^2.9.16",
20+
"wavefile": "^11.0.0"
21+
},
22+
"devDependencies": {
23+
"bun-types": "latest",
24+
"@types/fluent-ffmpeg": "^2.1.24",
25+
"@types/node": "^20.12.7",
26+
"tsx": "^4.7.2",
27+
"typescript": "^5.4.5"
28+
}
29+
}

0 commit comments

Comments
 (0)