Skip to content

Create .NET bot #79

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions dotnet/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System;
using System.Diagnostics;
using System.ComponentModel;
using System.Text.Json.Serialization;
using System.Text.Json;
using StreamJsonRpc;
using System.Buffers;

Process childProcess = Process.Start(new ProcessStartInfo("deltachat-rpc-server")
{
RedirectStandardInput = true,
RedirectStandardOutput = true,
});

var formatter = new StreamJsonRpc.SystemTextJsonFormatter();

// "kind" property does not have to be the first.
// Otherwise messages like
// `{"jsonrpc":"2.0","id":18,"result":{"contextId":1,"event":{"comment":null,"kind":"ConfigureProgress","progress":1}}}`
// don't parse because `comment` goes before `kind`.
formatter.JsonSerializerOptions.AllowOutOfOrderMetadataProperties = true;

JsonRpc rpc = new JsonRpc(new StreamJsonRpc.NewLineDelimitedMessageHandler(childProcess.StandardInput.BaseStream, childProcess.StandardOutput.BaseStream, formatter));
rpc.StartListening();

var accounts = await rpc.InvokeAsync<int[]>("get_all_account_ids");
int accountId;
if(accounts.Length == 0) {
accountId = await rpc.InvokeAsync<int>("add_account");
} else {
accountId = accounts[0];
}

var res = await rpc.InvokeAsync<Dictionary<String, String>>("get_system_info");

foreach(var item in res)
{
Console.WriteLine($"{item.Key}={item.Value}");
}

bool isConfigured = await rpc.InvokeAsync<bool>("is_configured", accountId);
if(!isConfigured) {
Console.WriteLine("not configured");
await rpc.InvokeAsync("set_config_from_qr", accountId, "dcaccount:https://nine.testrun.org/new");
await rpc.InvokeAsync("configure", accountId);
}

await rpc.InvokeAsync("start_io_for_all_accounts");

while(true) {
Event eventResponse = await rpc.InvokeAsync<Event>("get_next_event");
if (eventResponse.@event is InfoEventType infoEvent) {
Console.WriteLine($"INFO: {infoEvent.msg}");
} else {
Console.WriteLine($"Got event {eventResponse}");
}
}

public class Event {
public int contextId { get; set; }
public EventType @event { get; set; }
}

[JsonPolymorphic(TypeDiscriminatorPropertyName = "kind", IgnoreUnrecognizedTypeDiscriminators = true)]
[JsonDerivedType(typeof(InfoEventType), typeDiscriminator: "Info")]
public class EventType {
}

public class InfoEventType : EventType {
public string msg { get; set; }
}
12 changes: 12 additions & 0 deletions dotnet/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
To run C# bot,
you need to install .NET.
On Arch Linux it can be done with
`pacman -S dotnet-sdk`.

To build the program,
run `dotnet build`.

To run the program,
run `dotnet run`.

To get debugging logs of all JSON-RPC messages, run with `RUST_LOG="deltachat_rpc_server=trace" dotnet run`.
14 changes: 14 additions & 0 deletions dotnet/csharp.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="StreamJsonRpc" Version="2.21.69" />
</ItemGroup>

</Project>