コード例 #1
0
ファイル: SendAction.aspx.cs プロジェクト: forrest15/WeChat-1
    protected void Button1_Click(object sender, EventArgs e)
    {
        NewsMessageModel nmsg = new NewsMessageModel();

        nmsg.touser = "******";
        List <NewsArticles> list     = new List <NewsArticles>();
        NewsArticles        articles = new NewsArticles();

        articles.description = "图文描述";
        articles.picurl      = "图片Url";
        articles.title       = "图片标题";
        articles.url         = "跳转Url";
        list.Add(articles);
        NewsArticles articles1 = new NewsArticles();

        articles1.description = "图文描述1";
        articles1.picurl      = "图片Url1";
        articles1.title       = "图片标题1";
        articles1.url         = "跳转Url1";
        list.Add(articles1);
        nmsg.news.articles = list;

        string json = JsonHelper.JsonSerializer(nmsg);
        string url  = "http://10.1.40.136:8040/Service/SaveService.ashx";

        HttpRequestHelper.HttpPostWebRequest(url, 10000, json);
        //string msg = SendMessageApi.CommSendMessageTest(json);//接手post请求测试
        //MusicMessageModel msModel = new MusicMessageModel();
        //msModel.touser = "******";
        //msModel.music.description = "音乐描述";
        //msModel.music.hqmusicurl = "高品质链接";
        //msModel.music.thumb_media_id = "缩略ID";
        //msModel.music.title = "主题";
        //msModel.music.musicurl = "音乐链接";
        //string json = JsonHelper.JsonSerializer(msModel);
        //msg = string.Format("<script>jsonF('{0}')</script>", msg);
        //div_show.InnerHtml = msg;
    }
コード例 #2
0
        /// <summary>
        /// 发送图文消息
        /// </summary>
        /// <param name="accessToken"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool SendNewsMessage(NewsMessageModel model)
        {
            string data = JsonHelper.JsonSerializer(model);//转化json数据

            return(CommSendMessage(data));
        }
コード例 #3
0
    /// <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);//保存消息
    }