Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/baking-bad/matrix-dotnet-sdk
This open-source library allows you to build .NET apps compatible with Matrix Protocol
https://github.com/baking-bad/matrix-dotnet-sdk
matrix p2p
Last synced: about 14 hours ago
JSON representation
This open-source library allows you to build .NET apps compatible with Matrix Protocol
- Host: GitHub
- URL: https://github.com/baking-bad/matrix-dotnet-sdk
- Owner: baking-bad
- License: mit
- Created: 2021-11-18T10:38:07.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2024-06-23T11:50:10.000Z (6 months ago)
- Last Synced: 2024-12-11T03:54:22.553Z (18 days ago)
- Topics: matrix, p2p
- Language: C#
- Homepage: https://www.nuget.org/packages/Matrix.Sdk
- Size: 85 KB
- Stars: 98
- Watchers: 6
- Forks: 17
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-matrix-bots - Matrix .NET SDK (Baking Bad)
README
# **Matrix .NET SDK**
[![matrix-logo](matrix-logo.svg)](https://matrix.org/)This open-source library allows you to build .NET apps compatible with [Matrix Protocol](http://www.matrix.org).
It has support for a limited subset of the APIs presently.This SDK was built for interaction with the [Beacon Node](https://github.com/airgap-it/beacon-node). It supports login through the [`crypto_auth_provider.py`](https://github.com/airgap-it/beacon-node/blob/master/docker/crypto_auth_provider.py).
## Supported Platforms
* [.NET Standard 2.0](https://docs.microsoft.com/en-us/dotnet/standard/net-standard) or greater
## InstallationMatrix .NET SDK is [available on NuGet](https://www.nuget.org/packages/Matrix.Sdk/):
```
dotnet add package Matrix.Sdk
```# Use the SDK in your app
For a complete example, refer to [`Sample.cs`](https://github.com/baking-bad/matrix-dotnet-sdk/blob/main/Matrix.Sdk.Sample.Console/Sample.cs).
You can also clone this repository and run `Matrix.Sdk.Sample.Console`.Here is step by step guide:
## 1. Create
Use `MatrixClientFactory` to create an instance of `MatrixClient`
```cs
var factory = new MatrixClientFactory();
IMatrixClient client = factory.Create();
```## 2. Login
Currently, `MatrixClient` supports only [password login](https://spec.matrix.org/v1.1/client-server-api/#password-based).```cs
await client.LoginAsync(matrixNodeAddress, username, password, deviceId);
```## 3. Start listening for incoming events
To listen for the incoming Matrix room events you need to subscribe to `OnMatrixRoomEventsReceived;````cs
client.OnMatrixRoomEventsReceived += (sender, eventArgs) =>
{
foreach (BaseRoomEvent roomEvent in eventArgs.MatrixRoomEvents)
{
if (roomEvent is not TextMessageEvent textMessageEvent)
continue;(string roomId, string senderUserId, string message) = textMessageEvent;
if (client.UserId != senderUserId)
Console.WriteLine($"RoomId: {roomId} received message from {senderUserId}: {message}.");
}
};
```I recommend that you don't use anonymous functions to subscribe to events if you have to unsubscribe from the event at some later point in your code.
Then you should **start** `MatrixClient`
```cs
client.Start();
```
If you need to **stop** listening, for example, when the app is suspended, then do the following
```cs
client.Stop();
```
## 4. Basic functions```cs
// Create room
CreateRoomResponse createRoomResponse = await client.CreateTrustedPrivateRoomAsync(new[]
{
anotherClient.UserId
});// Join room
await anotherClient.JoinTrustedPrivateRoomAsync(createRoomResponse.RoomId);// Send message
await client.SendMessageAsync(createRoomResponse.RoomId, "some message");// Get joined rooms ids
await client.GetJoinedRoomsIdsAsync();// Leave room
await client.LeaveRoomAsync(roomId);
```## 5. Status check
`IsLoggedIn` - matrix login status.
`IsSyncing` - sync. Read more about syncing [here](https://spec.matrix.org/v1.1/client-server-api/#syncing).
```cs
Console.WriteLine($"client.IsLoggedIn: {client.IsLoggedIn}");Console.WriteLine($"client.IsSyncing: {client.IsSyncing}");
```