Crate matrix_bot_api[−][src]
matrix_bot_api
Easy to use API for implementing your own Matrix-Bot (see matrix.org)
Basic setup:
There are two parts: A MessageHandler and the MatrixBot. The MessageHandler defines what happens with received messages. The MatrixBot consumes your MessageHandler and deals with all the matrix-protocol-stuff, calling your MessageHandler for each new text-message.
You can write your own MessageHandler by implementing the MessageHandler
-trait,
or use one provided by this crate (currently only StatelessHandler
).
Multple Handlers:
One can register multiple MessageHandlers with a bot. Thus one can "plug and play"
different features to ones MatrixBot.
Messages are given to each handler in the order of their registration.
A message is given to the next handler until one handler returns StopHandling
.
Thus a message can be handled by multiple handlers as well (for example for "help").
Example
extern crate matrix_bot_api; use matrix_bot_api::{MatrixBot, MessageType}; use matrix_bot_api::handlers::{StatelessHandler, HandleResult}; fn main() { let mut handler = StatelessHandler::new(); handler.register_handle("shutdown", |bot: &MatrixBot, _room: &str, _cmd: &str| { bot.shutdown(); HandleResult::ContinueHandling /* Other handlers might need to clean up after themselves on shutdown */ }); handler.register_handle("echo", |bot: &MatrixBot, room: &str, cmd: &str| { bot.send_message(&format!("Echo: {}", cmd), room, MessageType::TextMessage); HandleResult::StopHandling }); let mut bot = MatrixBot::new(handler); bot.run("your_bot", "secret_password", "https://your.homeserver"); }
Have a look in the examples/ directory for detailed examples.
Modules
handlers |
Structs
MatrixBot |
Enums
MessageType |
How messages from the bot should be formated. This is up to the client, but usually RoomNotice's have a different color than TextMessage's. |