Exemplo n.º 1
0
        /// <summary>
        /// 处理语音请求
        /// </summary>
        /// <param name="requestMessage"></param>
        /// <returns></returns>
        public override IResponseMessageBase OnVoiceRequest(RequestMessageVoice requestMessage)
        {
            IWC_OfficalAccountsBLL account_BLL = new WC_OfficalAccountsBLL()
            {
                m_Rep = new WC_OfficalAccountsRepository(new Models.DBContainer())
            };

            //获得当前公众号
            WC_OfficalAccountsModel account = account_BLL.GetById(Id);
            var responseMessage             = CreateResponseMessage <ResponseMessageMusic>();
            //上传缩略图
            var uploadResult = Senparc.Weixin.MP.AdvancedAPIs.MediaApi.UploadTemporaryMedia(account.AccessToken, UploadMediaFileType.image,
                                                                                            Server.GetMapPath("~/Images/Logo.jpg"));

            //设置音乐信息
            responseMessage.Music.Title        = "天籁之音";
            responseMessage.Music.Description  = "播放您上传的语音";
            responseMessage.Music.MusicUrl     = "http://sdk.weixin.senparc.com/Media/GetVoice?mediaId=" + requestMessage.MediaId;
            responseMessage.Music.HQMusicUrl   = "http://sdk.weixin.senparc.com/Media/GetVoice?mediaId=" + requestMessage.MediaId;
            responseMessage.Music.ThumbMediaId = uploadResult.media_id;
            return(responseMessage);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 处理文字请求
        /// </summary>
        /// <returns></returns>
        public override IResponseMessageBase OnTextRequest(RequestMessageText requestMessage)
        {
            IWC_OfficalAccountsBLL account_BLL = new WC_OfficalAccountsBLL()
            {
                m_Rep = new WC_OfficalAccountsRepository(new Models.DBContainer())
            };

            //获得当前公众号
            WC_OfficalAccountsModel account = account_BLL.GetById(Id);

            IWC_MessageResponseBLL message_BLL = new WC_MessageResponseBLL()
            {
                m_Rep = new WC_MessageResponseRepository(new Models.DBContainer())
            };


            //只获取第一条匹配的条件作为信息
            List <P_WC_GetResponseContent_Result> messageList = message_BLL.GetResponseContent(Id, requestMessage.Content);



            if (messageList.Count() > 0)
            {
                //文本方式
                if (messageList[0].MessageRule == (int)WeChatRequestRuleEnum.Text)
                {
                    var responseMessage = base.CreateResponseMessage <ResponseMessageText>();
                    responseMessage.CreateTime   = ResultHelper.NowTime;
                    responseMessage.ToUserName   = requestMessage.FromUserName;
                    responseMessage.FromUserName = account.OfficalId;
                    responseMessage.Content      = messageList[0].TextContent;
                    return(responseMessage);
                }
                //图文方式
                else if (messageList[0].MessageRule == (int)WeChatRequestRuleEnum.Image)
                {
                    var responseMessage = CreateResponseMessage <ResponseMessageNews>();
                    foreach (var model in messageList)
                    {
                        responseMessage.Articles.Add(new Article()
                        {
                            Title       = model.TextContent,
                            Description = model.ImgTextContext,
                            PicUrl      = WebChatPara.SiteUrl + model.ImgTextUrl,
                            Url         = model.ImgTextLink
                        });
                    }
                    return(responseMessage);
                }//一般很少用到
                else if (messageList[0].MessageRule == (int)WeChatRequestRuleEnum.Voice)
                {
                    var responseMessage = base.CreateResponseMessage <ResponseMessageMusic>();
                    responseMessage.Music.MusicUrl    = WebChatPara.SiteUrl + messageList[0].MeidaUrl;
                    responseMessage.Music.Title       = messageList[0].TextContent;
                    responseMessage.Music.Description = messageList[0].Remark;
                    return(responseMessage);
                }//默认回复
                else if (messageList[0].MessageRule == (int)WeChatRequestRuleEnum.Default)
                {
                    if (messageList[0].Category == (int)WeChatReplyCategory.Text)
                    {
                        var responseMessage = base.CreateResponseMessage <ResponseMessageText>();
                        responseMessage.CreateTime   = ResultHelper.NowTime;
                        responseMessage.ToUserName   = requestMessage.FromUserName;
                        responseMessage.FromUserName = account.OfficalId;
                        responseMessage.Content      = messageList[0].TextContent;
                        return(responseMessage);
                    }
                    //图文方式
                    else if (messageList[0].Category == (int)WeChatReplyCategory.Image)
                    {
                        var responseMessage = CreateResponseMessage <ResponseMessageNews>();
                        foreach (var model in messageList)
                        {
                            responseMessage.Articles.Add(new Article()
                            {
                                Title       = model.TextContent,
                                Description = model.ImgTextContext,
                                PicUrl      = WebChatPara.SiteUrl + model.ImgTextUrl,
                                Url         = model.ImgTextLink
                            });
                        }
                        return(responseMessage);
                    }//一般很少用到
                    else if (messageList[0].Category == (int)WeChatReplyCategory.Voice)
                    {
                        var responseMessage = base.CreateResponseMessage <ResponseMessageMusic>();
                        responseMessage.Music.MusicUrl    = WebChatPara.SiteUrl + messageList[0].MeidaUrl;
                        responseMessage.Music.Title       = messageList[0].TextContent;
                        responseMessage.Music.Description = messageList[0].Remark;
                        return(responseMessage);
                    }
                }
                //下面方式用到才启用
                //视频方式
                //位置
            }
            var errorResponseMessage = requestMessage.CreateResponseMessage <ResponseMessageText>();

            //因为没有设置errorResponseMessage.Content,所以这小消息将无法正确返回。
            return(errorResponseMessage);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 订阅(关注)事件
        /// </summary>
        /// <returns></returns>
        public override IResponseMessageBase OnEvent_SubscribeRequest(RequestMessageEvent_Subscribe requestMessage)
        {
            ValidationErrors errors = new ValidationErrors();

            using (DBContainer db = new DBContainer())
            {
                IWC_OfficalAccountsBLL account_BLL = new WC_OfficalAccountsBLL()
                {
                    m_Rep = new WC_OfficalAccountsRepository(db)
                };

                //获得当前公众号
                WC_OfficalAccountsModel account = account_BLL.GetById(Id);

                //将用户提取到本地数据库
                WC_UserModel userModel = new WC_UserModel();
                IWC_UserBLL  user_BLL  = new WC_UserBLL()
                {
                    m_Rep = new WC_UserRepository(db)
                };

                UserInfoJson userJson = UserApi.Info(account.AccessToken, requestMessage.FromUserName);
                userModel.Id               = ResultHelper.NewId;
                userModel.OpenId           = userJson.openid;
                userModel.NickName         = userJson.nickname;
                userModel.Sex              = userJson.sex;
                userModel.Language         = userJson.language;
                userModel.City             = userJson.city;
                userModel.Province         = userJson.province;
                userModel.Country          = userJson.country;
                userModel.HeadImgUrl       = userJson.headimgurl;
                userModel.SubscribeTime    = ResultHelper.GetTimeByLong(userJson.subscribe_time);
                userModel.UnionId          = userJson.unionid;
                userModel.Remark           = userJson.remark;
                userModel.GroupId          = userJson.groupid.ToString();
                userModel.TagidList        = string.Join(",", userJson.tagid_list.ToArray());
                userModel.Subscribe        = userJson.subscribe;
                userModel.OfficalAccountId = account.Id;
                user_BLL.Create(ref errors, userModel);
                IWC_MessageResponseRepository m_Rep = new WC_MessageResponseRepository(db);
                //订阅回复
                List <WC_MessageResponse> messageList = m_Rep.GetSubscribeResponseContent(account.Id);
                if (messageList.Count() > 0)
                {
                    if (messageList[0].Category == (int)WeChatReplyCategory.Text)
                    {
                        var responseMessage = base.CreateResponseMessage <ResponseMessageText>();
                        responseMessage.CreateTime   = ResultHelper.NowTime;
                        responseMessage.ToUserName   = requestMessage.FromUserName;
                        responseMessage.FromUserName = account.OfficalId;
                        responseMessage.Content      = messageList[0].TextContent;
                        return(responseMessage);
                    }
                    //图文方式
                    else if (messageList[0].Category == (int)WeChatReplyCategory.Image)
                    {
                        var responseMessage = CreateResponseMessage <ResponseMessageNews>();
                        foreach (var model in messageList)
                        {
                            responseMessage.Articles.Add(new Article()
                            {
                                Title       = model.TextContent,
                                Description = model.ImgTextContext,
                                PicUrl      = WebChatPara.SiteUrl + model.ImgTextUrl,
                                Url         = model.ImgTextLink
                            });
                        }
                        return(responseMessage);
                    }    //一般很少用到
                    else if (messageList[0].Category == (int)WeChatReplyCategory.Voice)
                    {
                        var responseMessage = base.CreateResponseMessage <ResponseMessageMusic>();
                        responseMessage.Music.MusicUrl    = WebChatPara.SiteUrl + messageList[0].MeidaUrl;
                        responseMessage.Music.Title       = messageList[0].TextContent;
                        responseMessage.Music.Description = messageList[0].Remark;
                        return(responseMessage);
                    }
                }



                var errorResponseMessage = requestMessage.CreateResponseMessage <ResponseMessageText>();
                return(errorResponseMessage);
            }
        }