Exemplo n.º 1
0
        /// <summary>
        /// 发送图片
        /// </summary>
        /// <param name="fileName"></param>
        private void sendPicture(string fileName)
        {
            if (string.IsNullOrEmpty(fileName))
            {
                return;
            }

            var ext     = fileName.Substring(fileName.LastIndexOf('.') + 1);
            var message = new NIMImageMessage
            {
                SessionType     = NIMSessionType.kNIMSessionTypeP2P,
                ReceiverID      = target.accid,
                ImageAttachment = new NIMImageAttachment {
                    DisplayName = fileName, FileExtension = ext
                },
                LocalFilePath = fileName,
            };
            var body = new FileMessage {
                image = Util.getImageFromFile(fileName)
            };

            message.ImageAttachment.Height = body.image.Height;
            message.ImageAttachment.Width  = body.image.Width;

            sendMessage(1, body);
            TalkAPI.SendMessage(message);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 向机器人账号发送消息
        /// </summary>
        /// <param name="robotID">机器人Accid</param>
        /// <param name="message"></param>
        public static void SendMessage(string robotID, Robot.RobotMessageContent message)
        {
            var tmp = new { param = message, robotAccid = robotID };

            Robot.RobotMessage robotMsg = new Robot.RobotMessage();
            robotMsg.ReceiverID  = robotID;
            robotMsg.Attach      = tmp;
            robotMsg.TextContent = message.Text;
            TalkAPI.SendMessage(robotMsg);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 发送文件
        /// </summary>
        /// <param name="fileName"></param>
        private void sendFile(string fileName)
        {
            if (string.IsNullOrEmpty(fileName))
            {
                return;
            }

            var ext     = fileName.Substring(fileName.LastIndexOf('.') + 1);
            var message = new NIMFileMessage
            {
                SessionType    = NIMSessionType.kNIMSessionTypeP2P,
                ReceiverID     = target.accid,
                LocalFilePath  = fileName,
                FileAttachment = new NIMMessageAttachment {
                    DisplayName = fileName, FileExtension = ext
                }
            };

            var body = new FileMessage {
                attach = Util.serialize(new Attach {
                    name = fileName
                })
            };
            var id = sendMessage(6, body);

            TalkAPI.SendMessage(message, (uploaded, total, obj) =>
            {
                void action()
                {
                    mlcMessage.setPosition(id, (int)(100 * uploaded / total));
                    if (uploaded == total)
                    {
                        Messages.showMessage("文件上传完成");
                    }
                }

                while (!(Parent?.IsHandleCreated ?? false))
                {
                    Thread.Sleep(100);
                }

                Invoke((Action)action);
            });
        }
Exemplo n.º 4
0
        /// <summary>
        /// 发送文本消息
        /// </summary>
        /// <param name="msg">文字内容</param>
        private void sendTextMessage(string msg)
        {
            if (string.IsNullOrEmpty(msg))
            {
                return;
            }

            var body = new TextMessage {
                msg = msg
            };

            sendMessage(0, body);

            var message = new NIMTextMessage
            {
                SessionType = NIMSessionType.kNIMSessionTypeP2P,
                ReceiverID  = target.accid,
                TextContent = msg,
            };

            TalkAPI.SendMessage(message);
        }
Exemplo n.º 5
0
        void SendFile(string path)
        {
            string fileName  = System.IO.Path.GetFileNameWithoutExtension(path);
            string extension = System.IO.Path.GetExtension(path);

            if (IsImageFile(path))
            {
                NIM.NIMImageMessage imageMsg = new NIMImageMessage();
                imageMsg.LocalFilePath                 = path;
                imageMsg.ImageAttachment               = new NIMImageAttachment();
                imageMsg.ImageAttachment.DisplayName   = fileName;
                imageMsg.ImageAttachment.FileExtension = extension;
                imageMsg.ReceiverID  = _peerId;
                imageMsg.SessionType = _sessionType;
                using (var i = Image.FromFile(path))
                {
                    imageMsg.ImageAttachment.Height = i.Height;
                    imageMsg.ImageAttachment.Width  = i.Width;
                }
                TalkAPI.SendMessage(imageMsg);
            }
            else
            {
                NIM.NIMFileMessage fileMsg = new NIMFileMessage();
                fileMsg.LocalFilePath                = path;
                fileMsg.FileAttachment               = new NIMMessageAttachment();
                fileMsg.FileAttachment.DisplayName   = fileName;
                fileMsg.FileAttachment.FileExtension = extension;
                fileMsg.ReceiverID  = _peerId;
                fileMsg.SessionType = _sessionType;
                NIM.TalkAPI.SendMessage(fileMsg, (uploaded, total, obj) =>
                {
                    OutputForm.Instance.SetOutput(string.Format("upload file:{0} {1}/{2}", path, uploaded, total));
                });
            }
        }