Skip to content

About

Rust client for the Bot API of Dialog, the private messenger

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

Repository files navigation

dialog-bot

Rust client for the Bot API of Dialog, the private messenger. Send yourself (or anyone who starts your bot) a notification from a program, a server or a cron job: text, pictures and buttons.

  • Async, on reqwest with rustls; works with any runtime reqwest works with (tokio).
  • A bot writes only to people who started it, and stops when they stop it.
  • The bot never learns who it is talking to: a chat id is random and different for every bot.

Install

cargo add dialog-bot

Create a bot

In the Dialog app: Settings → Bots → New bot. You get a token and a public link (dialog.pm/@your_bot). Open the link, press Start, and the chat id appears in the list of subscribers.

Send a message

use dialog_bot::Bot;

let bot = Bot::new("TOKEN");
bot.send_message("CHAT_ID", "Backup finished").await?;

Pictures and buttons:

use dialog_bot::{Button, SendOptions};

let options = SendOptions::new()
    .caption("*Motion at the door*")
    .row([Button::callback("Ignore", "ignore"), Button::link("Live view", "https://example.org/cam")]);
bot.send_photo_with("CHAT_ID", std::fs::read("door.jpg")?, &options).await?;

Answer people

use dialog_bot::Event;

let mut updates = bot.poll();                 // long polling
loop {
    let update = updates.next().await?;
    match &update.event {
        Event::Message(m) => { bot.send_message(&m.chat_id, &format!("You said: {}", m.text)).await?; }
        Event::CallbackQuery(q) => { bot.send_message(&q.chat_id, &format!("You pressed {}", q.data)).await?; }
        Event::ChatStopped { chat_id, .. } => println!("stopped by {chat_id}"),
        Event::Other => {}
    }
}

next confirms the updates returned before, retries network errors and returns an error when the server refuses the bot.

Commands offered after / in the chat:

bot.set_my_commands(&[dialog_bot::Command::new("status", "How things are")]).await?;

Errors

A refused call is Error::Api with status, code, message and, when the server gives one, a reason:

reason Meaning
bot_stopped The user stopped the bot (err.is_stopped()). Do not write to this chat until it sends /start again.
chat_not_found Not a chat of this bot.
bot_disabled The server operator switched the bot off.

A 429 carries retry_after. Error messages never contain the token.

Another server

Dialog can be self-hosted:

let bot = Bot::with_base_url("TOKEN", "https://chat.example.org");

Good to know

Messages exchanged with bots are not end-to-end encrypted: the bot's program and the server can read them. Everything else in Dialog is.

License

MIT

About

Rust client for the Bot API of Dialog, the private messenger

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages