| Crates.io | miyabi-telegram |
| lib.rs | miyabi-telegram |
| version | 0.1.2 |
| created_at | 2025-11-22 09:47:26.417278+00 |
| updated_at | 2025-11-22 09:47:26.417278+00 |
| description | Telegram Bot integration for Miyabi |
| homepage | https://github.com/ShunsukeHayashi/Miyabi |
| repository | https://github.com/ShunsukeHayashi/Miyabi |
| max_upload_size | |
| id | 1945115 |
| size | 101,408 |
Telegram Bot API client for Miyabi - enabling natural language interaction and real-time notifications.
/newbot and follow the instructionsExample:
Use this token to access the HTTP API:
1234567890:ABCdefGHIjklMNOpqrsTUVwxyz
You need your chat ID to send messages to yourself:
https://api.telegram.org/bot<YOUR_BOT_TOKEN>/getUpdates"chat":{"id":123456789} - that's your chat IDOr use this helper:
curl -s "https://api.telegram.org/bot<YOUR_BOT_TOKEN>/getUpdates" | jq '.result[0].message.chat.id'
export TELEGRAM_BOT_TOKEN="1234567890:ABCdefGHIjklMNOpqrsTUVwxyz"
# Send a simple message
cargo run --example send_message <your_chat_id>
# Send an interactive menu with buttons
cargo run --example interactive_menu <your_chat_id>
use miyabi_telegram::TelegramClient;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create client from environment variable
let client = TelegramClient::from_env()?;
// Send a message
client.send_message(123456789, "Hello from Miyabi!").await?;
Ok(())
}
use miyabi_telegram::{TelegramClient, InlineKeyboard, InlineKeyboardButton};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = TelegramClient::from_env()?;
// Create keyboard
let keyboard = InlineKeyboard::single_row(vec![
InlineKeyboardButton::callback("✅ Yes", "yes"),
InlineKeyboardButton::callback("❌ No", "no"),
]);
// Send with keyboard
client.send_message_with_keyboard(
123456789,
"Do you agree?",
keyboard
).await?;
Ok(())
}
When users click inline keyboard buttons, Telegram sends a callback query to your webhook:
use miyabi_telegram::{TelegramClient, Update};
async fn handle_update(update: Update) -> Result<(), Box<dyn std::error::Error>> {
let client = TelegramClient::from_env()?;
if let Some(callback) = update.callback_query {
// Answer the callback query
client.answer_callback_query(&callback.id, Some("Processing...")).await?;
// Handle the callback data
match callback.data.as_deref() {
Some("yes") => {
// User clicked "Yes"
println!("User agreed!");
}
Some("no") => {
// User clicked "No"
println!("User declined!");
}
_ => {}
}
}
Ok(())
}
| Method | Description |
|---|---|
send_message(chat_id, text) |
Send a text message |
send_message_with_keyboard(chat_id, text, keyboard) |
Send message with inline keyboard |
answer_callback_query(id, text) |
Respond to button click |
get_me() |
Get bot information |
set_webhook(url) |
Set webhook URL |
delete_webhook() |
Delete webhook |
Update - Incoming updates from TelegramMessage - Message objectUser - User informationChat - Chat informationCallbackQuery - Button click eventInlineKeyboard - Inline keyboard markupInlineKeyboardButton - Individual buttonlet keyboard = InlineKeyboard::new(vec![
// Row 1
vec![
InlineKeyboardButton::callback("Option 1", "opt1"),
InlineKeyboardButton::callback("Option 2", "opt2"),
],
// Row 2
vec![
InlineKeyboardButton::callback("Option 3", "opt3"),
],
// Row 3 with URL
vec![
InlineKeyboardButton::url("Visit Website", "https://example.com"),
],
]);
Messages support Markdown formatting:
let text = r#"
*Bold text*
_Italic text_
[Link](https://example.com)
`Code`
```rust
fn hello() {
println!("Hello!");
}
"#;
client.send_message(chat_id, text).await?;
## Error Handling
```rust
use miyabi_telegram::{TelegramClient, TelegramError};
match client.send_message(chat_id, "Hello").await {
Ok(message) => {
println!("Message sent! ID: {}", message.message_id);
}
Err(TelegramError::ApiError(msg)) => {
eprintln!("Telegram API error: {}", msg);
}
Err(e) => {
eprintln!("Other error: {}", e);
}
}
# Run all tests
cargo test --package miyabi-telegram
# Run specific test
cargo test --package miyabi-telegram test_api_url
[dependencies]
miyabi-telegram = { path = "../miyabi-telegram" }
tokio = { version = "1", features = ["full"] }
| Variable | Description | Required |
|---|---|---|
TELEGRAM_BOT_TOKEN |
Your bot token from BotFather | Yes |
Same as Miyabi project.
This crate also includes a complete bot server binary for Miyabi integration:
# 1. Configure environment
cp ../../.env.telegram.example ../../.env.telegram
# Edit with your tokens
# 2. Build bot server
cargo build --release --bin miyabi-telegram-bot --features bot-server
# 3. Setup ngrok (local development)
ngrok http 3000
# 4. Update WEBHOOK_URL in .env.telegram with ngrok URL
# 5. Run bot
set -a && source ../../.env.telegram && set +a
../../target/release/miyabi-telegram-bot
See TELEGRAM_BOT_SETUP.md for complete documentation.