[−][src]Crate matrix_bot_api
matrix_bot_api
Easy to use API for implementing your own Matrix-Bot (see matrix.org)
Basic setup:
There are two main 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 with an ActiveBot
handle that allows the handler to
respond to the 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, _, _| { bot.shutdown(); HandleResult::ContinueHandling /* Other handlers might need to clean up after themselves on shutdown */ }); handler.register_handle("echo", |bot, message, tail| { bot.send_message(&format!("Echo: {}", tail), &message.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
ActiveBot | Handle for an active bot that allows sending message, leaving rooms and shutting down the bot |
MatrixBot | |
Message | |
Room |
Enums
MessageType | How messages from the bot should be formatted. This is up to the client, but usually RoomNotice's have a different color than TextMessage's. |