A Matrix bot built as a .NET worker service: it logs in, joins every room it is invited to and sends the text messages of other users back into the room.
EchoBotHost.csbuilds the Generic Host and bindsEchoBotOptionsfrom theEchoBotconfiguration section,Program.cssets up the SDK (MatrixSdk.Initialize, its logs go tologs/in the working directory) and runs the host.EchoBotWorker.csis theBackgroundService: it logs in or restores its session withStoredClient.LoginOrRestoreAsync, runs a/syncloop withClient.SyncOnceV2, joins invites and sends the messages back.RoomTimeline.cslistens to the timeline of a joined room and picks out new text messages of other users.
The example references the bindings from source and copies the locally built native library, build it first with
./scripts/build-debug.sh (see the README in the repository root). Your own application
references the Matrix.RustSdk.Bindings.Native.* packages instead.
Create an account for the bot, then pass its credentials with environment variables or command line arguments instead
of writing the password into appsettings.json:
export EchoBot__Password='...'
dotnet run --project example/Matrix.RustSdk.Examples.EchoBot -- \
--EchoBot:Homeserver=matrix.org \
--EchoBot:Username=my-echo-botInvite the bot to a room and send a message, it answers with the same text. Stop it with Ctrl+C. The bot stores its
session and the stores of the SDK in data/ in the working directory, later starts restore the session and don't need
the password.
| Setting | Description |
|---|---|
EchoBot:Homeserver |
Server name (matrix.org) or homeserver URL |
EchoBot:Username |
Username of the bot |
EchoBot:Password |
Password of the bot, only needed for the first login |
EchoBot:DeviceName |
Name of the device created at login, defaults to Echo Bot |
EchoBot:DataDirectory |
Directory of the session and the stores, defaults to data |
The bot only answers messages that arrive while it is running. It doesn't set up end-to-end encryption: its device
isn't verified and has no key backup, so other users' clients may refuse to send it keys. A real bot would verify its
device or use Encryption.Recover after the first login (StoredClient.IsRestored is false).
test/Matrix.RustSdk.Examples.EchoBot.Tests starts this host in-process against a tuwunel homeserver in docker and
chats with the bot as a second user.