Beispiel #1
0
        private async Task SendPictureAsync2(IMiraiHttpSession session, string path) // 发图, 不同的是使用 IMessageChainBuilder 构造消息链
        {
            IImageMessage msg = await session.UploadPictureAsync(UploadTarget.Group, path);

            Builders.IMessageChainBuilder builder = session.GetMessageChainBuilder();
            builder.Add(msg);
            await session.SendGroupMessageAsync(0, builder); // 自己填群号, 一般由 IGroupMessageEventArgs 提供
        }
Beispiel #2
0
        private async Task SendPictureAsync(IMiraiHttpSession session, string path) // 发图
        {
            // 你也可以使用另一个重载 UploadPictureAsync(PictureTarget, Stream)
            // 当 mirai-api-http 在v1.7.0以下时将使用本地的HttpListener做图片中转
            // UploadTarget.Friend 对应 IMiraiSession.SendFriendMessageAsync
            IImageMessage msg = await session.UploadPictureAsync(UploadTarget.Group, path);

            IChatMessage[] chain = new IChatMessage[] { msg }; // 数组里边可以加上更多的 IMessageBase, 以此达到例如图文并发的情况
            await session.SendGroupMessageAsync(0, chain);     // 自己填群号, 一般由 IGroupMessageEventArgs 提供
        }
        public static async Task <IChatMessage[]> ToMiraiApiHttpMessages(this IGreenOnionsMessages greenOnionsMessage, IMiraiHttpSession session, UploadTarget uploadTarget)
        {
            List <IChatMessage> miraiApiHttpMessages = new List <IChatMessage>();
            List <Mirai.CSharp.HttpApi.Models.ChatMessages.ForwardMessageNode> nodes = new List <Mirai.CSharp.HttpApi.Models.ChatMessages.ForwardMessageNode>();

            for (int i = 0; i < greenOnionsMessage.Count; i++)
            {
                if (greenOnionsMessage[i] is IGreenOnionsTextMessage txtMsg)
                {
                    miraiApiHttpMessages.Add(new Mirai.CSharp.HttpApi.Models.ChatMessages.PlainMessage(txtMsg.Text));
                }
                else if (greenOnionsMessage[i] is IGreenOnionsImageMessage imgMsg)
                {
                    if (!string.IsNullOrEmpty(imgMsg.Url))
                    {
                        string url  = null;
                        string path = null;
                        if (File.Exists(imgMsg.Url))
                        {
                            path = imgMsg.Url;
                        }
                        else
                        {
                            url = imgMsg.Url;
                        }
                        miraiApiHttpMessages.Add(new Mirai.CSharp.HttpApi.Models.ChatMessages.ImageMessage(null, url, path));
                    }
                    else if (!string.IsNullOrEmpty(imgMsg.Base64Str))
                    {
                        using (MemoryStream ms = imgMsg.MemoryStream)
                        {
                            miraiApiHttpMessages.Add(await session.UploadPictureAsync(uploadTarget, ms));
                        }
                    }
                }
                else if (greenOnionsMessage[i] is IGreenOnionsAtMessage atMsg)
                {
                    if (atMsg.AtId == -1)
                    {
                        miraiApiHttpMessages.Add(new Mirai.CSharp.HttpApi.Models.ChatMessages.AtAllMessage());
                    }
                    else
                    {
                        miraiApiHttpMessages.Add(new Mirai.CSharp.HttpApi.Models.ChatMessages.AtMessage(atMsg.AtId));
                    }
                }
                else if (greenOnionsMessage[i] is IGreenOnionsForwardMessage forwardMsg)
                {
                    for (int j = 0; j < forwardMsg.ItemMessages.Count; j++)
                    {
                        Mirai.CSharp.HttpApi.Models.ChatMessages.ForwardMessageNode node = new Mirai.CSharp.HttpApi.Models.ChatMessages.ForwardMessageNode()
                        {
                            Id       = i * j + j,
                            Name     = forwardMsg.ItemMessages[j].NickName,
                            QQNumber = forwardMsg.ItemMessages[j].QQid,
                            Time     = DateTime.Now,
                            Chain    = (await ToMiraiApiHttpMessages(forwardMsg.ItemMessages[j].itemMessage, session, uploadTarget)).Select(msg => msg as Mirai.CSharp.HttpApi.Models.ChatMessages.IChatMessage).ToArray(),
                        };
                        nodes.Add(node);
                    }
                }
            }
            if (nodes.Count > 0)
            {
                Mirai.CSharp.HttpApi.Models.ChatMessages.ForwardMessage forwardMessage = new Mirai.CSharp.HttpApi.Models.ChatMessages.ForwardMessage(nodes.ToArray());
                miraiApiHttpMessages.Add(forwardMessage);
            }
            return(miraiApiHttpMessages.ToArray());
        }