private void SendImageMessage(object param) { string imagePath = null; BitmapImage img = null; Application.Current.Dispatcher.Invoke(() => { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Title = "Select image to send"; openFileDialog.Filter = "JPEG Files|*.jpeg"; Nullable <bool> res = openFileDialog.ShowDialog(); if (res == true) { imagePath = openFileDialog.FileName; } }); if (String.IsNullOrWhiteSpace(imagePath)) { return; } img = new BitmapImage(new Uri(imagePath)); MessageModel message = new ImageMessageModel(imagePath, true); message.StatusMessage = "Pending"; Application.Current.Dispatcher.Invoke(() => { Model.Messages.Add(message); }); try { SendAction(message); } catch (NoConnectionException) { message.StatusMessage = "Not delivered"; } }
/// <summary> /// 发送图片消息 /// </summary> /// <param name="accessToken"></param> /// <param name="model"></param> /// <returns></returns> public bool SendImageMessage(ImageMessageModel model) { string data = JsonHelper.JsonSerializer(model);//转化json数据 return(CommSendMessage(data)); }
/// <summary> /// 回复消息 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btn_Save_Click(object sender, EventArgs e) { string data = string.Empty; SendMessageApi sendApi = new SendMessageApi(); #region 文本消息 if (hidMsgType.Value == CommonEnum.enumMsgType.text.ToString())//文本消息 { TextMessageModel txtModel = new TextMessageModel(); txtModel.msgtype = hidMsgType.Value; //消息类型 txtModel.touser = getmodel.TMIOpenId; //接收人 txtModel.msgrelative = getmodel.TMIMsgRelative; //消息唯一标识 txtModel.msgsource = CommonEnum.enumMsgSource.send.GetHashCode(); //消息来源 txtModel.text.content = txtContent.Text.Trim(); if (!sendApi.SendTextMessage(txtModel)) { Response.Write("<script>alert('发送失败')</script>"); return; } data = JsonHelper.JsonSerializer(txtModel); } #endregion #region 图片消息 else if (hidMsgType.Value == CommonEnum.enumMsgType.image.ToString())//图片消息 { ImageMessageModel imgModel = new ImageMessageModel(); string fileName = GetFileName(Fileimg.PostedFile, "imgMsg"); if (string.IsNullOrEmpty(fileName)) { Response.Write("<script>alert('上传文件为空')</script>"); return; } imgModel.msgtype = hidMsgType.Value; imgModel.touser = getmodel.TMIOpenId; imgModel.msgrelative = getmodel.TMIMsgRelative; //消息唯一标识 imgModel.msgsource = CommonEnum.enumMsgSource.send.GetHashCode(); //消息来源 imgModel.image.media_id = CommMsgParam.UploadMultimedia(fileName, CommonEnum.enumMsgType.image.ToString()); //上传多媒体返回media_Id if (!sendApi.SendImageMessage(imgModel)) { Response.Write("<script>alert('发送失败')</script>"); return; } data = JsonHelper.JsonSerializer(imgModel); } #endregion #region 语音消息 else if (hidMsgType.Value == CommonEnum.enumMsgType.voice.ToString())//语音消息 { VoiceMessageModel vocModel = new VoiceMessageModel(); string fileName = GetFileName(Filevoice.PostedFile, "voiceMsg"); if (string.IsNullOrEmpty(fileName)) { Response.Write("<script>alert('上传文件为空')</script>"); return; } vocModel.msgtype = hidMsgType.Value; vocModel.touser = getmodel.TMIOpenId; //接收人 vocModel.msgrelative = getmodel.TMIMsgRelative; //消息唯一标识 vocModel.msgsource = CommonEnum.enumMsgSource.send.GetHashCode(); //消息来源 vocModel.voice.media_id = CommMsgParam.UploadMultimedia(fileName, CommonEnum.enumMsgType.voice.ToString()); //上传多媒体返回media_Id if (!sendApi.SendVoiceMessage(vocModel)) { Response.Write("<script>alert('发送失败')</script>"); return; } data = JsonHelper.JsonSerializer(vocModel); } #endregion #region 视频消息 else if (hidMsgType.Value == CommonEnum.enumMsgType.video.ToString())//视频消息 { VideoMessageModel vdoModel = new VideoMessageModel(); string fileName = GetFileName(FileVideo.PostedFile, "videoMsg");//上传返回名称 if (string.IsNullOrEmpty(fileName)) { Response.Write("<script>alert('上传文件为空')</script>"); return; } vdoModel.msgtype = hidMsgType.Value; vdoModel.touser = getmodel.TMIOpenId; //接收人 vdoModel.msgrelative = getmodel.TMIMsgRelative; //消息唯一标识 vdoModel.msgsource = CommonEnum.enumMsgSource.send.GetHashCode(); //消息来源 vdoModel.video.media_id = CommMsgParam.UploadMultimedia(fileName, CommonEnum.enumMsgType.video.ToString()); //上传多媒体返回media_Id vdoModel.video.thumb_media_id = CommMsgParam.UploadMultimedia(fileName, CommonEnum.enumMsgType.thumb.ToString()); //上传多媒体返回media_Id vdoModel.video.title = title.Text; vdoModel.video.description = description.Text; if (!sendApi.SendVideoMessage(vdoModel)) { Response.Write("<script>alert('发送失败')</script>"); return; } data = JsonHelper.JsonSerializer(vdoModel); } #endregion #region 音乐消息 else if (hidMsgType.Value == CommonEnum.enumMsgType.music.ToString())//音乐消息 { MusicMessageModel musModel = new MusicMessageModel(); string fileName = GetFileName(FileMusic.PostedFile, "musicMsg"); if (string.IsNullOrEmpty(fileName)) { Response.Write("<script>alert('上传文件为空')</script>"); return; } musModel.msgtype = hidMsgType.Value; musModel.touser = getmodel.TMIOpenId; //接收人 musModel.msgrelative = getmodel.TMIMsgRelative; //消息唯一标识 musModel.msgsource = CommonEnum.enumMsgSource.send.GetHashCode(); //消息来源 musModel.music.musicurl = musicUrl.Text.Trim(); musModel.music.hqmusicurl = hqMusicUrl.Text; musModel.music.title = title.Text; musModel.music.description = description.Text; musModel.music.thumb_media_id = CommMsgParam.UploadMultimedia(fileName, CommonEnum.enumMsgType.thumb.ToString());//上传多媒体返回media_Id; if (!sendApi.SendMusicMessage(musModel)) { Response.Write("<script>alert('发送失败')</script>"); return; } data = JsonHelper.JsonSerializer(musModel); } #endregion #region 图文消息 else if (hidMsgType.Value == CommonEnum.enumMsgType.news.ToString())//图文消息 { NewsMessageModel newsModel = new NewsMessageModel(); newsModel.msgtype = hidMsgType.Value; newsModel.touser = getmodel.TMIOpenId; //接收人 newsModel.msgrelative = getmodel.TMIMsgRelative; //消息唯一标识 newsModel.msgsource = CommonEnum.enumMsgSource.send.GetHashCode(); //消息来源 List <NewsArticles> list = new List <NewsArticles>(); string newslist = hidnewsType.Value.TrimEnd('|'); string[] newsListArr = newslist.Split('|'); string picUrls = string.Empty;//获取所有的图片链接 for (int i = 0; i < newsListArr.Length; i++) { NewsArticles articles = new NewsArticles(); string[] listArr = newsListArr[i].Split(','); articles.title = listArr[0]; //图文标题 articles.url = listArr[1]; //点击跳转的链接 articles.picurl = listArr[2]; //图文消息的链接 articles.description = listArr[3]; //描述 list.Add(articles); picUrls += list[0] + ","; } newsModel.news.articles = list; if (!sendApi.SendNewsMessage(newsModel)) { Response.Write("<script>alert('发送失败')</script>"); return; } data = JsonHelper.JsonSerializer(newsModel); } #endregion SaveMessage(data);//保存消息 }
public ImageChatMessage(ImageMessageModel ImageMessage, string Sender, Guid id) : base(Sender, id, "ImageChat") { this.Image = ImageMessage.Image; }