This repository was archived by the owner on Nov 18, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathChatBot.cs
More file actions
1199 lines (1101 loc) · 41.2 KB
/
ChatBot.cs
File metadata and controls
1199 lines (1101 loc) · 41.2 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Pbx;
using Grpc.Core;
using Grpc;
using static Pbx.Plugin;
using static Pbx.Node;
using Google.Protobuf;
using System.Threading;
using System.Collections.Concurrent;
using Newtonsoft.Json;
using Google.Protobuf.Collections;
using System.Runtime.InteropServices;
using System.IO;
using Newtonsoft.Json.Linq;
using RestSharp;
using System.Net;
namespace Tinode.ChatBot
{
/// <summary>
/// ChatBot Plugin implement
/// </summary>
public class ChatBotPlugin : PluginBase
{
public override Task<Unused> Account(AccountEvent request, ServerCallContext context)
{
string action = string.Empty;
if (request.Action==Crud.Create)
{
action = "created";
}
else if (request.Action==Crud.Update)
{
action = "updated";
}
else if (request.Action==Crud.Delete)
{
action = "deleted";
}
else
{
action = "unknown";
}
return Task.FromResult(new Unused());
}
}
/// <summary>
/// CSharp ChatBot implement, same as the python version.
/// </summary>
public class ChatBot
{
/// <summary>
/// Uploaded attachment file info
/// </summary>
public class UploadedAttachmentInfo
{
/// <summary>
/// File fullname from local before upload
/// </summary>
public string FullFileName { get; set; }
/// <summary>
/// file short name
/// </summary>
public string FileName { get; set; }
/// <summary>
/// mime type, now is "file/[file extention]"
/// </summary>
public string Mime { get; set; }
/// <summary>
/// file size
/// </summary>
public long Size { get; set; }
/// <summary>
/// when uploaded to the server, the relative url to visit the file
/// </summary>
public string RelativeUrl { get; set; }
}
/// <summary>
/// Server pres event
/// </summary>
public class ServerPresEventArgs : EventArgs
{
public ServerPres Pres { get; private set; }
public ServerPresEventArgs(ServerPres pres)
{
Pres = pres;
}
}
/// <summary>
/// Server meta event
/// </summary>
public class ServerMetaEventArgs : EventArgs
{
public ServerMeta Meta { get; private set; }
public ServerMetaEventArgs(ServerMeta meta)
{
Meta = meta;
}
}
/// <summary>
/// Ctrl message event
/// </summary>
public class CtrlMessageEventArgs : EventArgs
{
/// <summary>
/// Ctrl message type
/// </summary>
public FutureTypes Type { get;private set; }
/// <summary>
/// tid
/// </summary>
public string Id { get;private set; }
/// <summary>
/// ctrl code
/// </summary>
public int Code { get; private set; }
/// <summary>
/// ctrl text
/// </summary>
public string Text { get; private set; }
/// <summary>
/// ctrl topic
/// </summary>
public string Topic { get; private set; }
/// <summary>
/// rpc callback status, if error or failed ,this will be true
/// </summary>
public bool HasError => !(Code >= 200 && Code < 400);
/// <summary>
/// paramaters
/// </summary>
public MapField<string,ByteString> Params { get; private set; }
public CtrlMessageEventArgs(FutureTypes type,string id,int code,string text,string topic, MapField<string, ByteString> paramaters)
{
Type = type;
Id = id;
Code = code;
Text = text;
Topic = topic;
Params = paramaters;
}
}
/// <summary>
/// Server Data event, when there is a message this event will fired
/// </summary>
public class ServerDataEventArgs : EventArgs
{
public ServerData Data { get; private set; }
public ServerDataEventArgs(ServerData data)
{
Data = data;
}
}
/// <summary>
/// Chatbot subscribed user/group information
/// </summary>
public class Subscriber:MessageBase
{
/// <summary>
/// User Id
/// </summary>
public string UserId { get; set; }
/// <summary>
/// topic
/// </summary>
public string Topic { get; set; }
/// <summary>
/// user name/nick
/// </summary>
public string UserName { get; set; }
/// <summary>
/// user photo with base64 encode
/// </summary>
public string PhotoData { get; set; }
/// <summary>
/// subscribed user/group type, if user,this will be "user",else will be "group"
/// </summary>
public string Type { get; set; }
/// <summary>
/// online status
/// </summary>
public bool Online { get; set; }
/// <summary>
/// user photo image type
/// </summary>
public string PhotoType { get; set; }
public Subscriber()
{
}
/// <summary>
/// constructor
/// </summary>
/// <param name="userId">user id</param>
/// <param name="topic">topic</param>
/// <param name="username"> user name/nick</param>
/// <param name="type">subscribed type, user or group?</param>
/// <param name="photo"> user photo with base64 encode</param>
/// <param name="photoType">user photo image type</param>
/// <param name="online">if the subscriber is online now</param>
public Subscriber(string userId,string topic,string username,string type,string photo,string photoType,bool online)
{
UserId = userId;
Topic = topic;
UserName = username;
Type = type;
PhotoData = photo;
PhotoType = photoType;
Online = online;
}
}
/// <summary>
/// chatbot received pres event
/// </summary>
public event EventHandler<ServerPresEventArgs> ServerPresEvent;
/// <summary>
/// chatbot receive meta data event
/// </summary>
public event EventHandler<ServerMetaEventArgs> ServerMetaEvent;
/// <summary>
/// chatbot receive ctrl message event
/// </summary>
public event EventHandler<CtrlMessageEventArgs> CtrlMessageEvent;
/// <summary>
/// chatbot receive message data event
/// </summary>
public event EventHandler<ServerDataEventArgs> ServerDataEvent;
/// <summary>
/// Login success event
/// </summary>
public event EventHandler LoginSuccessEvent;
/// <summary>
/// Login failed event
/// </summary>
public event EventHandler LoginFailedEvent;
/// <summary>
/// When chatbot disconnected with the server event
/// </summary>
public event EventHandler DisconnectedEvent;
void OnServerPresEvent(ServerPresEventArgs e)
{
var handler = ServerPresEvent;
if (handler!=null)
{
handler(this, e);
}
}
void OnServerMetaEvent(ServerMetaEventArgs e)
{
var handler = ServerMetaEvent;
if (handler != null)
{
handler(this, e);
}
}
void OnServerDataEvent(ServerDataEventArgs e)
{
var handler = ServerDataEvent;
if (handler != null)
{
handler(this, e);
}
}
void OnCtrlMessageEvent(FutureTypes type, string id, int code, string text, string topic, MapField<string, ByteString> paramaters)
{
var e = new CtrlMessageEventArgs(type, id, code, text, topic, paramaters);
var handler = CtrlMessageEvent;
if (handler != null)
{
handler(this, e);
}
}
void OnLoginEvent(bool isSuccess)
{
if (isSuccess)
{
var handler = LoginSuccessEvent;
if (handler!=null)
{
handler(this, new EventArgs());
}
}
else
{
var handler = LoginFailedEvent;
if (handler != null)
{
handler(this, new EventArgs());
}
}
}
void OnDisconnected()
{
var handler = DisconnectedEvent;
if (handler!=null)
{
handler(this, new EventArgs());
}
}
/// <summary>
/// future callback types
/// </summary>
public enum FutureTypes
{
/// <summary>
/// defatul, unknown callback operation
/// </summary>
Unknown,
/// <summary>
/// Hi rpc call
/// </summary>
Hi,
/// <summary>
/// login rpc call
/// </summary>
Login,
/// <summary>
/// sub rpc call
/// </summary>
Sub,
/// <summary>
/// get rpc call
/// </summary>
Get,
/// <summary>
/// pub rpc call
/// </summary>
Pub,
/// <summary>
/// note rpc call
/// </summary>
Note,
/// <summary>
/// leave rpc call
/// </summary>
Leave,
}
/// <summary>
/// Help define functionale which will be called in future.
/// </summary>
public class Future
{
/// <summary>
/// Each rpc call message id
/// </summary>
public string Tid { get; private set; }
/// <summary>
/// Argument needs by action.
/// </summary>
public string Arg { get; private set; }
/// <summary>
/// Future action type
/// </summary>
public FutureTypes Type { get; private set; }
/// <summary>
/// callback function
/// </summary>
public Action<string, MapField<string, ByteString>> Action { get; private set; }
/// <summary>
/// construction
/// </summary>
/// <param name="tid"> Each rpc call message id</param>
/// <param name="action">Argument needs by action.</param>
/// <param name="arg">callback function</param>
public Future(string tid,FutureTypes type,Action<string, MapField<string, ByteString>> action,string arg="")
{
Tid = tid;
Type = type;
Action = action;
Arg = arg;
}
}
/// <summary>
/// Chatbot application name
/// </summary>
public string AppName => "ChatBot";
/// <summary>
/// Chatbot version
/// </summary>
public string AppVersion => "0.18.1";
/// <summary>
/// Chatbot library version
/// </summary>
public string LibVersion => "0.18.1";
/// <summary>
/// Chatbot current platfrom information
/// </summary>
public string Platform => $"({RuntimeInformation.OSDescription} {RuntimeInformation.OSArchitecture})";
/// <summary>
/// upload large file endpoint
/// </summary>
public const string UploadEndpoint = "/v0/file/u";
/// <summary>
/// download large file endpoint
/// </summary>
public const string DownloadEndpoint = "/v0/file/s";
/// <summary>
/// Chatbot instance id, this will be used in chat
/// </summary>
public string BotUID { get; private set; }
/// <summary>
/// Next tid
/// </summary>
public long NextTid { get; private set; }
/// <summary>
/// gRPC server
/// </summary>
public string ServerHost { get; set; }
/// <summary>
/// Plugin API calls listen addr
/// </summary>
public string Listen { get; set; }
/// <summary>
/// Cookie file
/// </summary>
public string CookieFile { get; set; }
/// <summary>
/// Login in schema
/// </summary>
public string Schema { get; set; }
/// <summary>
/// Login in credentials
/// </summary>
public ByteString Secret { get; set; }
/// <summary>
/// Chatbot auto reply implement interface,you can use this to make you own chat logic
/// </summary>
public IBotResponse BotResponse { get; set; }
public Dictionary<string, Subscriber> Subscribers { get; private set; }
Server server;
AsyncDuplexStreamingCall<ClientMsg, ServerMsg> client;
Channel channel;
CancellationTokenSource cancellationTokenSource;
Queue<ClientMsg> sendMsgQueue;
Dictionary<string, bool> subscriptions;
Dictionary<string, Future> onCompletion;
string token;
string apiKey;
string apiBaseUrl;
/// <summary>
/// Contruction
/// </summary>
/// <param name="serverHost">gRPC server</param>
/// <param name="listen">Plugin API calls listen addr</param>
/// <param name="cookie">Cookie file</param>
/// <param name="schema">Login in schema</param>
/// <param name="secret">Login in credentials</param>
/// <param name="botResponse">Chatbot auto reply implement interface,you can use this to make you own chat logic</param>
public ChatBot(string serverHost="localhost:6061",string listen="0.0.0.0:40052",string cookie=".tn-cookie",string schema="basic",string secret="",IBotResponse botResponse=null)
{
//Initial a tid with a random value btw 1~1000
NextTid =new Random().Next(1,1000);
ServerHost = serverHost;
Listen = listen;
CookieFile = cookie;
Schema = schema;
Secret = ByteString.CopyFromUtf8(secret);
BotResponse = botResponse;
cancellationTokenSource = new CancellationTokenSource();
sendMsgQueue = new Queue<ClientMsg>();
subscriptions = new Dictionary<string, bool>();
onCompletion = new Dictionary<string, Future>();
Subscribers = new Dictionary<string, Subscriber>();
}
/// <summary>
///
/// </summary>
/// <param name="apiBaseUrl"></param>
/// <param name="apikey">http api operation api key</param>
public void SetHttpApi(string apiBaseUrl,string apikey)
{
this.apiBaseUrl = apiBaseUrl;
this.apiKey = apikey;
}
/// <summary>
/// Show log to console
/// </summary>
/// <param name="title">message title</param>
/// <param name="message">message content</param>
void Log(string title,string message)
{
Console.WriteLine($"[{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}] - [{title}] : {message}");
}
/// <summary>
/// generate the next tid
/// </summary>
/// <returns>new tid</returns>
public string GetNextTid()
{
NextTid += 1;
return NextTid.ToString();
}
/// <summary>
/// Initialize plugin api calls listen server
/// </summary>
/// <returns>Plugin api calls server</returns>
public Server InitServer()
{
var server = new Server();
server.Services.Add(Plugin.BindService(new ChatBotPlugin()));
var listenHost = Listen.Split(':')[0];
var listenPort = int.Parse(Listen.Split(':')[1]);
server.Ports.Add(new ServerPort(listenHost,listenPort, ServerCredentials.Insecure));
server.Start();
return server;
}
/// <summary>
/// Initialize chatbot client
/// </summary>
/// <returns>chatbot client instance</returns>
public AsyncDuplexStreamingCall<ClientMsg, ServerMsg> InitClient()
{
//ping / 2s and timeout 2s
var options = new List<ChannelOption>
{
new ChannelOption("grpc.keepalive_time_ms", 2000),
new ChannelOption("grpc.keepalive_timeout_ms",2000)
};
channel = new Channel(ServerHost, ChannelCredentials.Insecure,options);
var stub = new NodeClient(channel);
var stream = stub.MessageLoop(cancellationToken:cancellationTokenSource.Token);
ClientPost(Hello());
ClientPost(Login(CookieFile, Schema, Secret));
ClientPost(Subscribe("me"));
return stream;
}
/// <summary>
/// Add future callback
/// </summary>
/// <param name="tid">tid</param>
/// <param name="bundle">callback instance</param>
public void AddFuture(string tid,Future bundle)
{
onCompletion.Add(tid, bundle);
}
/// <summary>
/// Execute callbacks in future callback collection
/// </summary>
/// <param name="tid">tid</param>
/// <param name="code">rpc status code</param>
/// <param name="text">text</param>
/// <param name="topic">topic name</param>
/// <param name="paramaters">paramaters</param>
public void ExecFuture(string tid,int code,string text, string topic, MapField<string,ByteString> paramaters)
{
if (onCompletion.ContainsKey(tid))
{
var bundle = onCompletion[tid];
var type = onCompletion[tid].Type;
onCompletion.Remove(tid);
if (code>=200 && code<400)
{
var arg = bundle.Arg;
bundle.Action(arg, paramaters);
if (type==FutureTypes.Sub)
{
ClientPost(GetSubs("me"));
}
else if (type==FutureTypes.Login)
{
OnLoginEvent(true);
}
Log("Exec Future", $"Tid={tid} Code={code} Topic={topic} Text={text} Params={paramaters} ...OK");
}
else
{
if (type == FutureTypes.Login)
{
OnLoginEvent(false);
}
Log("Exec Future", $"Tid={tid} Code={code} Topic={topic} Text={text} Params={paramaters} ...Failed");
}
OnCtrlMessageEvent(type, tid, code, text, topic, paramaters);
}
}
/// <summary>
/// add a chat topic to subscription
/// </summary>
/// <param name="topic">topic name </param>
public void AddSubscription(string topic)
{
if (!subscriptions.ContainsKey(topic))
{
subscriptions.Add(topic, true);
}
}
public void AddSubscriber(Subscriber sub)
{
Log("Update Subscriber", sub.ToString());
//Log("Update Subscriber", $"UserId={sub.UserId} Name={sub.UserName} Online={sub.Online} Type={sub.Type}");
if (Subscribers.ContainsKey(sub.Topic))
{
Subscribers[sub.Topic] = sub;
}
else
{
Subscribers.Add(sub.Topic, sub);
//if the first, sub it ahead
ClientPost(Subscribe(sub.Topic));
}
}
/// <summary>
/// delete a chat topic from subscription
/// </summary>
/// <param name="topic">topic name </param>
public void DelSubscription(string topic)
{
if (subscriptions.ContainsKey(topic))
{
subscriptions.Remove(topic);
}
}
/// <summary>
/// Server version callback implement
/// </summary>
/// <param name="paramaters">paramaters</param>
public void ServerVersion(MapField<string, ByteString> paramaters)
{
if (paramaters==null)
{
return;
}
Log("Server Version",$"Server:{paramaters["build"].ToString(Encoding.ASCII)},{paramaters["ver"].ToString(Encoding.ASCII)}");
}
/// <summary>
/// login callback implement
/// </summary>
/// <param name="cookieFile">cookie file</param>
/// <param name="paramaters">paramaters</param>
public void OnLogin(string cookieFile, MapField<string, ByteString> paramaters)
{
if (paramaters == null || string.IsNullOrEmpty(cookieFile))
{
return;
}
if (paramaters.ContainsKey("user"))
{
BotUID = paramaters["user"].ToString(Encoding.ASCII);
}
Dictionary<string, string> cookieDics = new Dictionary<string, string>();
cookieDics["schema"] = "token";
if (paramaters.ContainsKey("token"))
{
cookieDics["secret"] = JsonConvert.DeserializeObject<string>(paramaters["token"].ToString(Encoding.UTF8));
cookieDics["expires"] = JsonConvert.DeserializeObject<string>(paramaters["expires"].ToString(Encoding.UTF8));
}
else
{
cookieDics["schema"] = "basic";
cookieDics["secret"] = JsonConvert.DeserializeObject<string>(paramaters["token"].ToString(Encoding.UTF8));
}
//save token for upload operation
token = cookieDics["secret"];
try
{
using (FileStream stream = new FileStream(cookieFile, FileMode.Create,FileAccess.Write))
using (StreamWriter w = new StreamWriter(stream))
{
w.Write(JsonConvert.SerializeObject(cookieDics));
}
}
catch (Exception e)
{
Log("On Login",$"Failed to save authentication cookie:{e}");
}
}
public void OnGetMeta(ServerMeta meta)
{
if (meta.Sub!=null)
{
foreach (var sub in meta.Sub)
{
var userId = sub.UserId;
var online = sub.Online;
var topic = sub.Topic;
var publicInfo = sub.Public.ToStringUtf8();
var subObj = JsonConvert.DeserializeObject<JObject>(publicInfo);
string userName = topic;
string type = subObj == null ? "group" : "user";
string photoData = string.Empty;
string photoType = string.Empty;
if (subObj != null)
{
userName = subObj["fn"].ToString();
if (subObj.ContainsKey("photo"))
{
photoData = subObj["photo"]["data"].ToString();
photoType = subObj["photo"]["type"].ToString();
}
}
AddSubscriber(new Subscriber(userId,topic, userName, type, photoData, photoType,online));
}
}
}
/// <summary>
/// read auth information from cookie
/// </summary>
/// <param name="schema">schema type</param>
/// <param name="secret">secret</param>
/// <returns>success-true, faild-false</returns>
public bool ReadAuthCookie(out string schema,out ByteString secret)
{
schema = null;
secret =null;
if (!File.Exists(CookieFile))
{
return false;
}
try
{
using (FileStream stream = new FileStream(CookieFile, FileMode.Open,FileAccess.Read))
using (StreamReader r = new StreamReader(stream))
{
var cookies = JsonConvert.DeserializeObject<Dictionary<string, string>>(r.ReadToEnd());
schema = cookies["schema"];
secret = null;
if (schema == "token")
{
var defautl = Encoding.Default.GetBytes(cookies["secret"]);
var utf8Str = Encoding.UTF8.GetString(defautl);
secret = ByteString.FromBase64(utf8Str);
}
else
{
secret = ByteString.CopyFromUtf8(cookies["secret"]);
}
return true;
}
}
catch (Exception)
{
return false;
}
}
/// <summary>
/// Post message to message queue
/// </summary>
/// <param name="msg">message</param>
public void ClientPost(ClientMsg msg)
{
sendMsgQueue.Enqueue(msg);
}
/// <summary>
/// reset client status
/// </summary>
public void ClientReset()
{
try
{
token = null;
subscriptions.Clear();
onCompletion.Clear();
Subscribers.Clear();
while (sendMsgQueue.Count > 0)
{
sendMsgQueue.Dequeue();
}
}
catch (Exception e)
{
}
}
/// <summary>
/// Say Hi to server
/// </summary>
/// <returns>Hi message</returns>
public ClientMsg Hello()
{
var tid = GetNextTid();
AddFuture(tid, new Future(tid, FutureTypes.Hi,new Action<string, MapField<string, ByteString>>((unused, paramaters) =>
{
ServerVersion(paramaters);
})));
return new ClientMsg() { Hi = new ClientHi() { Id = tid, UserAgent = $"{AppName}/{AppVersion} {Platform}; gRPC-csharp/{AppVersion}", Ver = LibVersion, Lang = "EN" } };
}
public ClientMsg GetSubs(string topic="me",bool getAll=false)
{
var tid = GetNextTid();
if (getAll)
{
return new ClientMsg() { Get = new ClientGet() { Id = tid, Topic = topic, Query = new GetQuery() { What = "sub" } } };
}
else
{
return new ClientMsg() { Get = new ClientGet() { Id = tid, Topic = topic} };
}
}
/// <summary>
/// login in
/// </summary>
/// <param name="cookieFile">cookie file</param>
/// <param name="scheme">schema type</param>
/// <param name="secret">secret</param>
/// <returns>login in message</returns>
public ClientMsg Login(string cookieFile,string scheme,ByteString secret)
{
var tid = GetNextTid();
AddFuture(tid, new Future(tid, FutureTypes.Login,new Action<string, MapField<string, ByteString>>((fname, paramaters) =>
{
OnLogin(fname, paramaters);
}),cookieFile));
return new ClientMsg() { Login = new ClientLogin() { Id = tid, Scheme = scheme, Secret = secret } };
}
/// <summary>
/// Subscribe topic
/// </summary>
/// <param name="topic">topic name</param>
/// <returns>subscribe message</returns>
public ClientMsg Subscribe(string topic)
{
var tid = GetNextTid();
AddFuture(tid, new Future(tid, FutureTypes.Sub,new Action<string, MapField<string, ByteString>>((topicName, unused) =>
{
AddSubscription(topicName);
}),topic));
return new ClientMsg() { Sub = new ClientSub() { Id = tid, Topic = topic } };
}
/// <summary>
/// leave topic
/// </summary>
/// <param name="topic">topic name</param>
/// <returns>leave message</returns>
public ClientMsg Leave(string topic)
{
var tid = GetNextTid();
AddFuture(tid, new Future(tid, FutureTypes.Leave,new Action<string, MapField<string, ByteString>>((topicName, unused) =>
{
DelSubscription(topicName);
}), topic));
return new ClientMsg() { Leave = new ClientLeave() { Id = tid, Topic = topic } };
}
/// <summary>
/// publish message to a topic
/// </summary>
/// <param name="topic">topic name</param>
/// <param name="text">message</param>
/// <returns>publish message</returns>
public ClientMsg Publish(string topic,string text)
{
var tid = GetNextTid();
var message = ByteString.CopyFromUtf8(JsonConvert.SerializeObject(text));
return new ClientMsg() { Pub = new ClientPub() { Id = tid, Topic = topic, NoEcho = true, Content = message } };
}
public ClientMsg Publish(string topic,ChatMessage msg)
{
var tid = GetNextTid();
var pub = new ClientPub() { Id = tid, Topic = topic, NoEcho = true, Content = ByteString.CopyFromUtf8(msg.ToString()) };
pub.Head.Add("mime",ByteString.CopyFromUtf8("\"text/x-drafty\""));
return new ClientMsg() { Pub = pub };
}
public ClientMsg PublishWithAttachments(string topic, List<string> attachments,ChatMessage msg)
{
var tid = GetNextTid();
StringBuilder builder = new StringBuilder();
builder.Append("[");
foreach (var attach in attachments)
{
builder.Append($"\"{attach}\",");
}
builder.Remove(builder.Length - 1,1);
builder.Append("]");
var pub = new ClientPub() { Id = tid, Topic = topic, NoEcho = true, Content = ByteString.CopyFromUtf8(msg.ToString()) };
pub.Head.Add("attachments", ByteString.CopyFromUtf8(builder.ToString()));
pub.Head.Add("mime", ByteString.CopyFromUtf8("\"text/x-drafty\""));
return new ClientMsg() { Pub = pub };
}
/// <summary>
/// note read
/// </summary>
/// <param name="topic">topic name </param>
/// <param name="seq">chat sequence id</param>
/// <returns>note message</returns>
public ClientMsg NoteRead(string topic, int seq)
{
return new ClientMsg() { Note = new ClientNote() { SeqId = seq, Topic = topic, What = InfoNote.Read } };
}
/// <summary>
/// upload large file as attachment
/// </summary>
/// <param name="fileName">the file will be uploaded</param>
/// <param name="redirectUrl">if upload operation should temp changed</param>
/// <returns>uploaded information for send message to user or group</returns>
public async Task<UploadedAttachmentInfo> Upload(string fileName, string redirectUrl = "")
{
if (string.IsNullOrEmpty(token))
{
//not login or login failed, disable upload
Log("Upload Attachment Error", $"Not login, upload operation disabled...");
return null;
}
if (string.IsNullOrEmpty(fileName)||!File.Exists(fileName))
{
Log("Upload Attachment Error", $"can not find file:{fileName}");
return null;
}
try
{
var fullFileName = Path.GetFullPath(fileName);
var fileInfo = new FileInfo(fullFileName);
UploadedAttachmentInfo attachmentInfo = new UploadedAttachmentInfo();
attachmentInfo.FullFileName = fullFileName;
attachmentInfo.FileName = fileInfo.Name;
attachmentInfo.Size = fileInfo.Length;
attachmentInfo.Mime = $"file/{fileInfo.Extension}";
var restClient = new RestClient(apiBaseUrl) ;
RestRequest request;
if (string.IsNullOrEmpty(redirectUrl))
{
request = new RestRequest(UploadEndpoint, Method.Put);
}
else
{
request = new RestRequest(redirectUrl, Method.Put);
}
request.AddHeader("X-Tinode-APIKey", apiKey);
request.AddHeader("X-Tinode-Auth", $"Token {token}");
request.AddBody("id", GetNextTid());
request.AddFile("file", fullFileName);
var cancellationTokenSource = new CancellationTokenSource();
var response = await restClient.ExecuteAsync(request,cancellationTokenSource.Token);