SDKs
Rust SDK
Build a bot in Rust that receives message events and replies to the sender.
On this page
Installation
Create a binary project with Cargo. Add the BotComm crate and Tokio to run the asynchronous message loop.
cargo new echo-bot
cd echo-bot
cargo add botcomm
cargo add tokio --features macros,rt-multi-thread
Package details and versions are available on crates.io.
Bot credentials
Create a bot in the BotComm console. Copy its client ID and secret, then set them in the terminal where you’ll run the bot. Keep the secret in your server-side environment.
export CLIENT_ID='your-client-id'
export CLIENT_SECRET='your-client-secret'
Build an echo bot
Save this code in src/main.rs. It enables text input so people can type a message in your bot’s chat.
use botcomm::{Bot, Config, Event};
use std::env;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut config = Config::new(env::var("CLIENT_ID")?, env::var("CLIENT_SECRET")?);
config.input_mode = Some("messages".into());
let (bot, mut events) = Bot::run(config).await?;
while let Some(event) = events.recv().await {
if let Event::Message(msg) = event {
bot.send(&msg.session_id, &msg.content, false).await?;
}
}
Ok(())
}
Read from the receiver returned by Bot::run. An Event::Message carries the text and session ID you need to reply in the same chat.
Run your bot
cargo run
Leave the process running, open your bot’s chat in BotComm, and send hello. The bot should reply with hello in the same conversation.