Mattermost.NET
0.2.2
dotnet add package Mattermost.NET --version 0.2.2
NuGet\Install-Package Mattermost.NET -Version 0.2.2
<PackageReference Include="Mattermost.NET" Version="0.2.2" />
paket add Mattermost.NET --version 0.2.2
#r "nuget: Mattermost.NET, 0.2.2"
// Install Mattermost.NET as a Cake Addin #addin nuget:?package=Mattermost.NET&version=0.2.2 // Install Mattermost.NET as a Cake Tool #tool nuget:?package=Mattermost.NET&version=0.2.2
<a id="readme-top"></a>
Mattermost.NET
Ready-to-use .NET Standard library for convenient development of Mattermost bots.
Table of Contents
Installation
The library is available as a NuGet package. You can install it using the NuGet Package Manager or the dotnet
CLI.
dotnet add package Mattermost.NET
Usage
Create a new bot
using Mattermost.NET;
const string token = "37VlFKySIZn6gryA85cR1GKBQkjmfRZ6";
const string server = "https://mm.your-server.com"; // or https://community.mattermost.com by default
MattermostClient client = new(server);
Authenticate the bot
var botUser = await client.LoginAsync(token);
Subscribe to post updates
client.OnMessageReceived += ClientOnMessageReceived;
private static void ClientOnMessageReceived(object? sender, MessageEventArgs e)
{
if (string.IsNullOrWhiteSpace(e.Message.Post.Text))
{
return;
}
e.Client.SendMessageAsync(e.Message.Post.ChannelId, "Hello, World!");
}
Start the bot updates
await client.StartReceivingAsync();
Note: The bot will automatically reconnect if the connection is lost. It's not required to call
StartReceivingAsync
if you don't want to receive updates through the WebSocket connection.
Stop the bot
await client.StopReceivingAsync();
Client Methods
Posts
CreatePostAsync
- Send message to specified channel using channel identifier.
string channelId
- The ID of the channel to send the message to.
string message
- The message to send.
string replyToPostId
- The ID of the post to reply to (optional).
MessagePriority priority
- The priority of the message, default is MessagePriority.Empty
.
IEnumerable<string> files
- The files to upload, you have to upload files before sending the message.
Example:
await client.CreatePostAsync("channel_id", "Hello, World!");
GetPostAsync
- Get post by identifier.
string postId
- The ID of the post to get.
Example:
var post = await client.GetPostAsync("post_id");
UpdatePostAsync
- Update message text for specified post identifier.
string postId
- The ID of the post to update.
string message
- The new message text.
Example:
await client.UpdatePostAsync("post_id", "I changed my mind");
DeletePostAsync
- Delete post with specified post identifier.
string postId
- The ID of the post to delete.
Example:
await client.DeletePostAsync("post_id");
Channels
CreateChannelAsync
- Create simple channel with specified users.
string teamId
- The ID of the team to create the channel in.
string name
- The name of the channel to create.
string displayName
- The display name of the channel to create.
ChannelType channelType
- The type of the channel to create.
string purpose
- The purpose of the channel to create (optional).
string header
- The header of the channel to create (optional).
Example:
await client.CreateChannelAsync("team_id", "channel_name", "Channel display name", ChannelType.Public, "Channel purpose", "Channel header");
CreateGroupChannelAsync
- Create group channel with specified users.
string[] userIds
- The IDs of the users to create the group channel with.
Example:
await client.CreateGroupChannelAsync([ "user_id_1", "user_id_2" ]);
AddUserToChannelAsync
- Add user to specified channel.
string channelId
- The ID of the channel to add the user to.
string userId
- The ID of the user to add to the channel.
Example:
await client.AddUserToChannelAsync("channel_id", "user_id");
DeleteUserFromChannelAsync
- Remove user from specified channel.
string channelId
- The ID of the channel to remove the user from.
string userId
- The ID of the user to remove from the channel.
Example:
await client.DeleteUserFromChannelAsync("channel_id", "user_id");
FindChannelByNameAsync
- Find channel by name and team ID.
string teamId
- The ID of the team to search in.
string channelName
- The name of the channel to search for.
Example:
Channel? channel = await client.FindChannelByNameAsync("team_id", "channel_name");
ArchiveChannelAsync
- Archive specified channel.
string channelId
- The ID of the channel to archive.
Example:
bool archived = await client.ArchiveChannelAsync("channel_id");
Files
GetFileAsync
- Get file bytes by identifier.
string fileId
- The ID of the file to get.
Example:
byte[] file = await client.GetFileAsync("file_id");
GetFileDetailsAsync
- Get file details by identifier.
string fileId
- The ID of the file to get.
Example:
FileDetails fileDetails = await client.GetFileDetailsAsync("file_id");
UploadFileAsync
- Upload file to specified channel.
string channelId
- The ID of the channel to upload the file to.
string filePath
- The path to the file to upload.
Action<int>? progressChanged
- The action to call with the upload progress.
Example:
var callback = new Action<int>(progress => Console.WriteLine($"Upload progress: {progress}%"));
await client.UploadFileAsync("channel_id", "file_path", callback);
Users
GetMeAsync
- Get current authenticated user information.
Example:
User me = await client.GetMeAsync();
GetUserAsync
- Get user by identifier.
string userId
- The ID of the user to get.
Example:
User user = await client.GetUserAsync("user_id");
GetUserByUsernameAsync
- Get user by username.
string username
- The username of the user to get.
Example:
User user = await client.GetUserByUsernameAsync("username");
Other
SetChannelCallStateAsync
- Set call state for specified channel ('Calls' plugin required).
If you are looking for another methods, please visit the Mattermost API documentation and create an issue in the GitHub repository with what exact methods you need - I will add them as soon as possible.
License
Distributed under the MIT License. See LICENSE.md for more information.
Contact
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
.NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.1 is compatible. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- System.Text.Json (>= 9.0.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.