-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTDLibHelper.cs
More file actions
59 lines (52 loc) · 1.8 KB
/
Copy pathTDLibHelper.cs
File metadata and controls
59 lines (52 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using System;
using System.Threading.Tasks;
using Telegram.Td;
using Telegram.Td.Api;
namespace AutoReplyUserBot
{
public static class TDLibHelper
{
public static Task<BaseObject> SendAsyncHelper(this Client client, Function function)
{
TdCompletionSource tcs = new TdCompletionSource();
client.Send(function, tcs);
return tcs.Task;
}
public static async Task<BaseObject> SendAsync(this Client client, Function function)
{
BaseObject result = await client.SendAsyncHelper(function);
if (result is Error error)
{
throw new TDLibException(error.Message, error.Code);
}
return result;
}
/// <summary>
/// This is used to send async/await requests via TDLib
/// </summary>
private class TdCompletionSource : TaskCompletionSource<BaseObject>, ClientResultHandler
{
public void OnResult(BaseObject result)
{
SetResult(result);
}
}
}
[Serializable]
public class TDLibException : Exception
{
public int Code { get; private set; }
public Error _error;
public TDLibException(Error error) : base(error.Message)
{
_error = error;
Code = _error.Code;
}
public TDLibException(string message, int code) : base(message) { Code = code; }
public TDLibException(string message) : base(message) { }
public TDLibException(string message, Exception inner) : base(message, inner) { }
protected TDLibException(
System.Runtime.Serialization.SerializationInfo info,
System.Runtime.Serialization.StreamingContext context) : base(info, context) { }
}
}