public override IResponseMessageBase OnEvent_SubscribeRequest(RequestMessageEvent_Subscribe requestMessage) { //var appId = System.Configuration.ConfigurationManager.AppSettings["WeixinAppId"]; //var accessToken = AccessTokenContainer.GetAccessToken(Config.AppId); var userInfo = Senparc.Weixin.MP.AdvancedAPIs.UserApi.Info(Config.AppId, base.WeixinOpenId); string nickName = userInfo.nickname; string title = userInfo.sex == 1 ? "先生" : (userInfo.sex == 2 ? "女士" : ""); var responseMessage = requestMessage.CreateResponseMessage <ResponseMessageText>(); responseMessage.Content = "欢迎 【{0}{1}】 关注《微信公众号+小程序快速开发》课程!".FormatWith(nickName, title); return(responseMessage); }
/// <summary> /// 关注后,欢迎消息 /// </summary> /// <param name="requestMessage"></param> /// <returns></returns> public override IResponseMessageBase OnEvent_SubscribeRequest(RequestMessageEvent_Subscribe requestMessage) { //获取关注人员信息 // var accessToken = AccessTokenContainer.GetAccessToken(Config.AppId); var userInfo = Senparc.Weixin.MP.AdvancedAPIs.UserApi.Info(Config.AppId, base.WeixinOpenId); var nickName = userInfo.nickname; var title = userInfo.sex == 1 ? "先生" : (userInfo.sex == 0 ? "女士" : ""); var responseMessage = requestMessage.CreateResponseMessage <ResponseMessageText>(); responseMessage.Content = "欢迎{0}{1} 关注!".FormatWith(nickName, title); return(responseMessage); //System.Threading.Thread.Sleep(1000); //CustomApi.SendText(Config.AppId, base.WeixinOpenId, "客服消息,马上回复.进入<a href=\"https://www.baidu.com\">进入百度</a>"); //return new ResponseMessageNoResponse(); //不返回任何消息 }
/// <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); } }
/// <summary> /// 订阅(关注)事件 /// </summary> /// <returns></returns> public override IResponseMessageBase OnEvent_SubscribeRequest(RequestMessageEvent_Subscribe requestMessage) { ValidationErrors errors = new ValidationErrors(); WC_OfficalAccountsBLL account_BLL = new WC_OfficalAccountsBLL(); //获得当前公众号 WC_OfficalAccounts account = account_BLL.GetCurrentAccount(); //将用户提取到本地数据库 WC_User userModel = new WC_User(); WC_UserBLL user_BLL = new WC_UserBLL(); account.AccessToken = Senparc.Weixin.MP.CommonAPIs.CommonApi.GetToken(account.AppId, account.AppSecret).access_token; UserInfoJson userJson = UserApi.Info(account.AccessToken, requestMessage.FromUserName); userModel.OpenId = userJson.openid; userModel.NickName = userJson.nickname; userModel.Sex = userJson.sex.ToString(); 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).ToString(); 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.ToString(); userModel.OfficalAccountId = account.Id.ToString(); user_BLL.m_Rep.Create(userModel); WC_MessageResponseRepository m_Rep = new WC_MessageResponseRepository(); //订阅回复 List <WC_MessageResponse> messageList = m_Rep.FindList(a => a.OfficalAccountId == account.Id.ToString() && a.MessageRule == WeChatRequestRuleEnum.Subscriber.ToString() && a.IsDefault == "true").ToList(); if (messageList.Count() > 0) { if (messageList[0].Category == WeChatReplyCategory.Text.ToString()) { 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 == WeChatReplyCategory.Image.ToString()) { 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 == WeChatReplyCategory.Voice.ToString()) { 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); }