Skip to content

Commit 52719d2

Browse files
authored
153 (#154)
* #153 docs: add demo documentation and bot examples - Add comprehensive README for demo explaining WebApp architecture - Create Python bot example with python-telegram-bot - Create Rust bot example using teloxide and masterror - Add wasm-bindgen version mismatch resolution guide - Exclude rust_bot from workspace to avoid conflicts Closes #153 * #153 fix: add SPDX license headers to example files - Add SPDX headers to python_bot.py - Add SPDX headers to rust_bot .env.example - Add SPDX headers to rust_bot .gitignore - Add SPDX headers to rust_bot main.rs Fixes reuse lint compliance check * #153 refactor: remove Python bot example Remove Python bot - this is a Rust project, only Rust examples * #153 docs: improve clarity and fix Python references - Remove Python references from demo README - Update code examples in rust_bot README to show masterror usage - Add doc-comments to rust_bot structures and functions - Add explanatory comments to .env.example - Fix function name reference (handle_webapp_data) * #153 test: add tests for rust_bot and local development guide - Add unit tests for OrderData serialization/deserialization - Add lib.rs with testable OrderData structure - Add local development section with cloudflared and ngrok tunnels - Document HTTPS requirement and tunnel URL management * #153 docs: update wasm-bindgen version fix instructions Add explicit version number from current error and keep dynamic command as alternative * #153 fix: update dependencies to resolve wasm-bindgen version conflicts - Update masterror to 0.25 - Update wasm-bindgen to 0.2.105 - trunk build now works without version mismatches
1 parent 8f33664 commit 52719d2

File tree

15 files changed

+1143
-928
lines changed

15 files changed

+1143
-928
lines changed

Cargo.lock

Lines changed: 222 additions & 344 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ rust-version = "1.90"
1616

1717
[workspace.dependencies]
1818
inventory = "0.3.21"
19-
masterror = "0.24"
19+
masterror = "0.25"
2020

2121

2222
[lib]
@@ -81,6 +81,7 @@ mock = ["dep:urlencoding"]
8181

8282
[workspace]
8383
members = ["demo"]
84+
exclude = ["examples/bots/rust_bot"]
8485

8586
[dev-dependencies]
8687
wasm-bindgen-test = "0.3"

demo/Cargo.lock

Lines changed: 0 additions & 164 deletions
This file was deleted.

demo/README.md

Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
# Telegram WebApp SDK Demo
2+
3+
This is a demonstration application showcasing the capabilities of the `telegram-webapp-sdk`.
4+
5+
## Architecture Overview
6+
7+
This demo is a **frontend-only WebApp** that runs inside Telegram. To create a complete Telegram Mini App, you need two components:
8+
9+
1. **WebApp (this demo)** - Frontend application built with Rust/WASM
10+
2. **Bot Backend** - A Telegram bot that receives data from the WebApp
11+
12+
```
13+
┌─────────────────┐ ┌──────────────────┐
14+
│ Telegram User │ │ Your Server │
15+
│ │ │ │
16+
│ ┌───────────┐ │ HTTP │ ┌────────────┐ │
17+
│ │ WebApp │──┼────────▶│ │ Bot API │ │
18+
│ │ (demo) │ │ Data │ │ Handler │ │
19+
│ └───────────┘ │ │ └────────────┘ │
20+
└─────────────────┘ └──────────────────┘
21+
```
22+
23+
## What This Demo Does
24+
25+
The demo includes several pages:
26+
27+
- **Burger King Demo** (`/burger-king`) - Demonstrates sending data to bot using `send_data()`
28+
- **Init Data** (`/init-data`) - Shows user and chat information from Telegram
29+
- **Launch Parameters** (`/launch-params`) - Displays platform and version info
30+
- **Theme Parameters** (`/theme-params`) - Shows Telegram app color scheme
31+
32+
When a user clicks "Order" in the Burger King demo, the app calls `TelegramWebApp::send_data()` which sends a JSON payload to your bot. **However, you need a bot to receive this data.**
33+
34+
## Building the Demo
35+
36+
### Prerequisites
37+
38+
- Rust nightly toolchain
39+
- `trunk` for building WASM apps:
40+
```bash
41+
cargo install trunk
42+
```
43+
44+
### Build
45+
46+
```bash
47+
cd demo
48+
trunk build --release --public-url "https://your-domain.com/path"
49+
```
50+
51+
The built files will be in `demo/dist/`.
52+
53+
### Serve Locally (with mock)
54+
55+
For local development with mock Telegram environment:
56+
57+
```bash
58+
trunk serve
59+
```
60+
61+
Open http://localhost:8080 in your browser.
62+
63+
### Local Development with Real Telegram (HTTPS Tunnel)
64+
65+
Telegram requires HTTPS for WebApps. Use a tunnel to expose your local server:
66+
67+
**Option A: cloudflared (recommended)**
68+
69+
```bash
70+
# Install cloudflared
71+
# https://developers.cloudflare.com/cloudflare-one/connections/connect-networks/downloads/
72+
73+
# Serve demo locally
74+
trunk serve
75+
76+
# In another terminal, create tunnel
77+
cloudflared tunnel --url http://localhost:8080
78+
```
79+
80+
You'll get a URL like `https://random-name.trycloudflare.com` - use this in BotFather.
81+
82+
**Option B: ngrok**
83+
84+
```bash
85+
# Install ngrok
86+
# https://ngrok.com/download
87+
88+
# Serve demo locally
89+
trunk serve
90+
91+
# In another terminal, create tunnel
92+
ngrok http 8080
93+
```
94+
95+
You'll get a URL like `https://abc123.ngrok.io` - use this in BotFather.
96+
97+
**Important:** Tunnel URLs change each time you restart. Update BotFather or your bot's `.env` file after each restart.
98+
99+
## Setting Up a Complete Mini App
100+
101+
### Step 1: Deploy the WebApp
102+
103+
1. Build the demo with your public URL
104+
2. Upload `dist/` contents to your web server (must be HTTPS)
105+
3. Your WebApp will be accessible at `https://your-domain.com/path/index.html`
106+
107+
### Step 2: Create a Bot
108+
109+
A complete Rust bot example with teloxide and masterror is available in `examples/bots/rust_bot/`:
110+
111+
```bash
112+
cd examples/bots/rust_bot
113+
cp .env.example .env
114+
# Edit .env with your bot token and WebApp URL
115+
cargo run --release
116+
```
117+
118+
The bot demonstrates:
119+
- Sending WebApp buttons to users
120+
- Receiving and processing orders from the Burger King demo
121+
- Proper error handling with masterror and AppError
122+
- Using teloxide for Telegram Bot API
123+
124+
See [examples/bots/rust_bot/README.md](../examples/bots/rust_bot/README.md) for complete documentation.
125+
126+
### Step 3: Configure Bot in BotFather
127+
128+
1. Open [@BotFather](https://t.me/BotFather)
129+
2. Send `/mybots` and select your bot
130+
3. Choose "Bot Settings" → "Menu Button"
131+
4. Send your WebApp URL: `https://your-domain.com/path/index.html`
132+
133+
### Step 4: Test
134+
135+
1. Open your bot in Telegram
136+
2. Send `/start` (the Rust bot example handles this command)
137+
3. Click the WebApp button
138+
4. Select an item and click "Order"
139+
5. The data will be sent to your bot's `handle_webapp_data` function
140+
141+
## Common Issues
142+
143+
### "Clicking buttons doesn't send events to the bot"
144+
145+
This is expected if you haven't set up a bot backend. The WebApp is only the frontend. You need:
146+
147+
1. A bot running on your server (see Rust example in `examples/bots/rust_bot/`)
148+
2. The bot must handle `web_app_data` messages
149+
3. The WebApp must call `send_data()` (which the Burger King demo already does)
150+
151+
### wasm-bindgen version mismatch
152+
153+
If you see:
154+
```
155+
rust Wasm file schema version: 0.2.X
156+
this binary schema version: 0.2.Y
157+
```
158+
159+
Solution:
160+
```bash
161+
# Find the version in the error message and install it
162+
cargo install -f wasm-bindgen-cli --version 0.2.104
163+
164+
# Or install from Cargo.lock (if available)
165+
cargo install wasm-bindgen-cli --version $(grep -A1 'name = "wasm-bindgen"' Cargo.lock | grep version | cut -d'"' -f2)
166+
```
167+
168+
Or update dependencies:
169+
```bash
170+
cargo update
171+
trunk build --release
172+
```
173+
174+
### The demo doesn't run in Telegram
175+
176+
Make sure:
177+
- Your WebApp is served over HTTPS (not HTTP)
178+
- The URL is publicly accessible (not localhost)
179+
- You've configured the URL in BotFather
180+
181+
## Project Structure
182+
183+
```
184+
demo/
185+
├── src/
186+
│ ├── main.rs - Entry point with telegram_app! macro
187+
│ ├── router.rs - Route definitions
188+
│ ├── pages/ - Page components
189+
│ │ ├── index.rs - Home page with navigation
190+
│ │ ├── burger_king.rs - Order demo with send_data()
191+
│ │ ├── init_data.rs - User/chat data display
192+
│ │ ├── launch_params.rs
193+
│ │ └── theme_params.rs
194+
│ └── components/ - Reusable UI components
195+
├── index.html - HTML template
196+
├── style.css - Styles
197+
└── Cargo.toml - Dependencies
198+
199+
```
200+
201+
## Learn More
202+
203+
- [Telegram WebApp Documentation](https://core.telegram.org/bots/webapps)
204+
- [telegram-webapp-sdk Documentation](https://docs.rs/telegram-webapp-sdk)
205+
- [SDK GitHub Repository](https://github.com/RAprogramm/telegram-webapp-sdk)
206+
207+
## License
208+
209+
This demo is part of the `telegram-webapp-sdk` project and is licensed under MIT.
-1010 KB
Binary file not shown.

0 commit comments

Comments
 (0)