Git Product home page Git Product logo

matrix-dotnet-sdk's Introduction

Matrix .NET SDK

matrix-logo

This open-source library allows you to build .NET apps compatible with Matrix Protocol. It has support for a limited subset of the APIs presently.

This SDK was built for interaction with the Beacon Node. It supports login through the crypto_auth_provider.py.

Supported Platforms

Installation

Matrix .NET SDK is available on NuGet:

dotnet add package Matrix.Sdk

Use the SDK in your app

For a complete example, refer to 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

var factory = new MatrixClientFactory();
IMatrixClient client = factory.Create();

2. Login

Currently, MatrixClient supports only password login.

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;

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

client.Start();

If you need to stop listening, for example, when the app is suspended, then do the following

client.Stop();

4. Basic functions

// 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.

Console.WriteLine($"client.IsLoggedIn: {client.IsLoggedIn}");

Console.WriteLine($"client.IsSyncing: {client.IsSyncing}");

matrix-dotnet-sdk's People

Contributors

iviich4el avatar k-karuna avatar m-kus avatar slimeq avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

matrix-dotnet-sdk's Issues

OnMatrixRoomEventReceived never called

I just downloaded this library to try and make a simple bot by following the guide one the README, however the program never calls the room event received method. It connects and sends the "testing..." message perfectly, but is unable to detect when someone else sends a message in that same room.

class Program {
	static IMatrixClient Client = new MatrixClientFactory().Create();

	static async Task Main() {
		await Client.LoginAsync(new Uri("https://matrix-client.matrix.org/"), "[USERNAME]", "[PASSWORD]", "Bot");
		Client.OnMatrixRoomEventsReceived += OnMessage;
		Client.Start();
		await Client.JoinTrustedPrivateRoomAsync("[ROOM_ID]");
		await Client.SendMessageAsync("[ROOM_ID]", "testing...");
		await Task.Delay(-1);
	}

	static void OnMessage(object? sender, MatrixRoomEventsEventArgs e) {
		Console.WriteLine("Event received");
		foreach (BaseRoomEvent roomEvent in e.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}.");
		}
	}
}

Get room name and member names with this SDK

Hi!

What is the expected way of dealing with display names in this SDK?
Everything works fine but i am missing the names of rooms and users and can only find ids.
In the .js SDK i got room names and member names after initial sync.

Some questions for the Future

Hi,
I would like to implement some other parts of the API (Room Topic, room image, ...) in the next days.
Wondering if you would be interested in maintaining the SDK in the future (acc the PR ;-) ?
@k-karuna I have forked from @SlimeQ repo so it would be nice if @SlimeQ could also send a PR for all his new stuff, because I would be rely on some of this .

Originally posted by @SlimeQ in #11 (comment)

Would love to bring this SDK a bit further because we would like to use this in some parts of our Hackerspace in the future.

Blazor WASM integration

Hi

Is it possible to integfrate this to Blazor WASM application. Do we need to make any changes in the source code for that?

Clarifications regarding limited API support

Hi! First off, thanks for this project! I've been looking for a newer Matrix .NET SDK to use in TRBot to allow people to play games by chatting in Matrix rooms.

The README says that this SDK has support for a limited subset of the Matrix API at present. Are there plans to support all of the API in time? No pressure here - just asking!

As-is, does this library allow reading chats from a specific Matrix room? Does this room have to be unencrypted, or is there also support for E2E encryption?

Thank you very much!

New fork

Hello, and thank you for making this awesome library!

I was looking for a C# implementation of the matrix sdk to use in my projects, however I wanted one that worked with .NET 7 and its new Native AOT feature. I wanted to be able to compile my bot into a small native library that could be called from other languages through FFI, which meant I needed to find a library that could function without reflection. Taking a look at this I determined that the use of Newtonsoft.Json caused me to be unable to use AOT compilation, so I created a fork and refactored this project to rely on System.Text.Json using source generators. My forked project is available here: https://codeberg.org/nullbyte/matrix-sdk, and if any of these changes are of interest I would be happy to open a pull request to merge them upstream. (it's worth noting, however, that I had to update the target framework to net7 in order to use AOT, and I haven't tested with netstandard)
I also altered the nullability of certain properties to remove warnings when building, which fixed #7.

Thank you for your time, and best wishes with future development.

[help] Can't receive a message

Hello!

I like this API, but I'm having issues receiving messages - not sure if I'm doing something wrong, or if it isn't implemented yet.
I used client credentials for a user that was already set up - joined in the rooms I need it in.

Most of the process works - I can log in and send a message into a room. (I didn't call Join. It's a public room the user was already in and I just provided the ID.)
However, when I send a message to that room (from a different user, logged in via Element) the OnMatrixRoomEventsReceived callback is never called. (I'm using the one from the sample.)

I did try calling the JoinTrustedPrivateRoomAsync with that ID (the room is public, but I see no other join method). It threw no error, but it also didn't change anything.
Is this possible?
Thank you!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.