Exemple #1
0
        static void Main(string[] args)
        {
            var cqWebSocketEvent = new CqHttpWebSocketEvent("ws://[::1]:6700/event", "");
            var httpApiClient    = new HttpApiClient("http://[::1]:5700", "");

            cqWebSocketEvent.ApiClient     = httpApiClient;
            cqWebSocketEvent.MessageEvent += (api, e) =>
            {
                Console.WriteLine(e.Content.Raw);
                Console.WriteLine(api is null);
            };
            cqWebSocketEvent.FriendRequestEvent += (api, e) =>
            {
                return(true);
            };
            cqWebSocketEvent.GroupInviteEvent += (api, e) =>
            {
                return(true);
            };
            cqWebSocketEvent.AnonymousMessageEvent += (api, e) =>
            {
                Console.WriteLine("id|name|flag:{0}|{1}|{2}", e.Anonymous.Id, e.Anonymous.Name, e.Anonymous.Flag);
                api.BanMessageSource(e.GroupId, e.Source, 1);
            };
            Task.Run(async() =>
            {
                while (true)
                {
                    await Task.Delay(1000);
                    Console.WriteLine("Available: {0}, Listening {1}", cqWebSocketEvent.IsAvailable, cqWebSocketEvent.IsListening);
                }
            });
            Task.Delay(TimeSpan.FromSeconds(3)).Wait();
            var cancellationTokenSource = new CancellationTokenSource();

            cqWebSocketEvent.StartListen(cancellationTokenSource.Token);
            Console.ReadLine();
            cancellationTokenSource.CancelAfter(TimeSpan.FromSeconds(2));
            Task.Delay(TimeSpan.FromSeconds(5)).Wait();
            cancellationTokenSource.Dispose();
            cancellationTokenSource = new CancellationTokenSource();
            cqWebSocketEvent.StartListen(cancellationTokenSource.Token);
            Task.Delay(-1).Wait();
        }
        private static async Task TestPositive()
        {
            var cqWebSocketEvent = new CqHttpWebSocketEvent("ws://[::1]:6700/event", "");
            var httpApiClient    = new CqHttpWebSocketApiClient("ws://[::1]:6700/api", "");

            cqWebSocketEvent.ApiClient = httpApiClient;
            ConfigListener(cqWebSocketEvent);

            await Task.Delay(TimeSpan.FromSeconds(3));

            var cancellationTokenSource = new CancellationTokenSource();
            await cqWebSocketEvent.StartListen(cancellationTokenSource.Token);

            Console.ReadLine();
            cancellationTokenSource.CancelAfter(TimeSpan.FromSeconds(2));
            await Task.Delay(TimeSpan.FromSeconds(5));

            cancellationTokenSource.Dispose();
            cancellationTokenSource = new CancellationTokenSource();
            await cqWebSocketEvent.StartListen(cancellationTokenSource.Token);

            await Task.Delay(-1);
        }
        public bool Load()
        {
            if (WebsocketUrl.EndsWith("/"))
            {
                WebsocketUrl = WebsocketUrl.Substring(0, WebsocketUrl.Length - 1);
            }
            _httpClient    = new HttpApiClient(ListenUrl, AccessToken);
            _wsApiListener = new CqHttpWebSocketEvent(WebsocketUrl, AccessToken);
            _wsApiListener.StartListen();
            _wsApiListener.MessageEvent += (api, message) =>
            {
                if (message is GroupMessage groupContent)
                {
                    var groupMessage = new AcceptedMessage()
                    {
                        IsGroupMessage = true,
                        FromGroup      = groupContent.GroupId,
                        FromUser       = groupContent.Sender.UserId,
                        Content        = groupContent.Content.Text
                    };
                    ReceivedMessageEvent?.Invoke(this, groupMessage);
                }

                if (message is PrivateMessage privateContent)
                {
                    var privateMessage = new AcceptedMessage()
                    {
                        IsGroupMessage = false,
                        FromUser       = privateContent.UserId,
                        Content        = privateContent.Content.Text
                    };
                    ReceivedMessageEvent?.Invoke(this, privateMessage);
                }
            };
            _wsApiListener.GroupInviteEvent += (api, request) => true;
            _wsApiListener.GroupAddedEvent  += (api, request) =>
            {
                var message = (SendingMessage)"欢迎使用Vtuber-Bot 查看帮助请输入help";
                message.TargetGroupId = request.GroupId;
                _sendingMessageQueue.Enqueue(message);
            };
            _enabled = true;
            new Thread(async() =>
            {
                while (_enabled)
                {
                    await Task.Delay(1500);
                    if (_sendingMessageQueue.TryDequeue(out var message))
                    {
                        var sendingMessage = new Sisters.WudiLib.SendingMessage();
                        foreach (var content in message.Contents)
                        {
                            if (content.Type == MessageType.Image)
                            {
                                sendingMessage += Sisters.WudiLib.SendingMessage.ByteArrayImage(content.Data);
                            }
                            if (content.Type == MessageType.Text)
                            {
                                sendingMessage += new Sisters.WudiLib.SendingMessage(content.Content);
                            }
                        }
                        await _httpClient.SendGroupMessageAsync(message.TargetGroupId, sendingMessage)
                        .Retry(5, handle: e => LogHelper.Error("Send message error", ex: e));
                    }
                }
            })
            {
                IsBackground = true
            }.Start();