public void DispatchText(WXReceiveTextMessage receiveMessage) { ConcurrentQueue <WXReceiveMessage> queueOfMessages; if (!Global.theMessagesFromWeiXin.TryGetValue(receiveMessage.FromUserName, out queueOfMessages)) { queueOfMessages = new ConcurrentQueue <WXReceiveMessage>(); Global.theMessagesFromWeiXin.TryAdd(receiveMessage.FromUserName, queueOfMessages); } queueOfMessages.Enqueue(receiveMessage); }
public string ResponseText(WXReceiveTextMessage receiveMessage) { string result = ""; if (receiveMessage.Content.StartsWith("103/002/")) { string[] arrFields = receiveMessage.Content.Split("/".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); if (arrFields.Length == 12) { string lat = arrFields[3]; //当前位置纬度 string log = arrFields[2]; //当前位置经度 string mapAPIKey = "00001"; if (!string.IsNullOrEmpty(GlobalManager.TheServiceAccount.MapAPI)) { if (GlobalManager.TheMapAPIOfInverseAdressParse.ContainsKey(GlobalManager.TheServiceAccount.MapAPI)) { mapAPIKey = GlobalManager.TheServiceAccount.MapAPI; } } string antiGeocodingUrl = GlobalManager.TheMapAPIOfInverseAdressParse[mapAPIKey].ReplaceWithKeys(new { lat = lat, log = log }.ToStringObjectDictionary()); HttpAdapter.apiJ(antiGeocodingUrl, (ret, res) => { if (ret.status == 0) { string detailAddress = ret.result.address; if (mapAPIKey == "00002") { detailAddress = string.IsNullOrEmpty(ret.result.street_number) ? ret.result.formatted_address + "附近" : ret.result.formatted_address; } result = XmlSerializeAdapter.Serialize <WXResponseTextMessage>(new WXResponseTextMessage { ToUserName = receiveMessage.FromUserName, FromUserName = receiveMessage.ToUserName, CreateTime = DateTimeAdapter.ConvertDateTimeUnix(DateTime.Now), MsgType = "text", Content = detailAddress }, true); //result = string.Format("<xml><ToUserName>{0}</ToUserName><FromUserName>{1}</FromUserName><CreateTime>{2}</CreateTime><MsgType>text</MsgType><Content>{3}</Content></xml>", requestMessage.FromUserName, requestMessage.ToUserName, DateTimeAdapter.ConvertDateTimeUnix(DateTime.Now), detailAddress); } }); } } //else //{ // result = FormatOutputText(receiveMessage.FromUserName, receiveMessage.ToUserName, "/:coffee"); //} return(result); }
public string DesideDispatchWho(WXReceiveTextMessage receiveMessage) { string result = ""; string extNo; if (!Global.theWXClientMappingExtNos.TryGetValue(receiveMessage.FromUserName, out extNo)) { //不存在任何映射将当前OpenId和最空闲的座席映射起来 if (Global.theExtNoProcessingNormalUserCount.Count > 0) { extNo = Global.theExtNoProcessingNormalUserCount.OrderBy(item => item.Value).ToList().First().Key; Global.theWXClientMappingExtNos.TryAdd(receiveMessage.FromUserName, extNo); Global.theExtNoProcessingNormalUserCount[extNo] = Global.theWXClientMappingExtNos.Where(item => item.Value == extNo).Count(); result = FormatOutputText(receiveMessage.FromUserName, receiveMessage.ToUserName, extNo + "为您服务"); } } return(result); }
public Stream ReceiveWXMessage(Stream s) { string result = ""; using (StreamReader sr = new StreamReader(s)) { result = sr.ReadToEnd(); } //result = OperationContext.Current.RequestContext.RequestMessage.ToString(); if (result != "") { WXReceiveMessage receiveMessage = XmlSerializeAdapter.Deserialize <WXReceiveMessage>(result); /* * XmlSerializer serializer = new XmlSerializer(typeof(WXReceiveMessage)); * using (StringReader rdr = new StringReader(result)) * { * WXReceiveMessage requestMessage = serializer.Deserialize(rdr) as WXReceiveMessage; * * } */ if (receiveMessage.MsgType.Equals("text")) { WXReceiveTextMessage requestTextMessage = XmlSerializeAdapter.Deserialize <WXReceiveTextMessage>(result); result = GlobalManager.TheWeiXinResponser.ResponseText(requestTextMessage); if (string.IsNullOrEmpty(result)) { result = GlobalManager.TheWeiXinDispatcher.DesideDispatchWho(requestTextMessage); GlobalManager.TheWeiXinDispatcher.DispatchText(requestTextMessage); } } else if (receiveMessage.MsgType.Equals("event")) { WXReceiveEventMessage requestEventMessage = XmlSerializeAdapter.Deserialize <WXReceiveEventMessage>(result); if (requestEventMessage.Event.Equals("subscribe")) { #region 关注 NormalAccount normalAccount = BuilderFactory.DefaultBulder(GlobalManager.getConnectString()).List <NormalAccount>(new NormalAccount { OpenId = requestEventMessage.FromUserName, AccountCode = GlobalManager.TheServiceAccount.AccountCode }).FirstOrDefault(); if (normalAccount == null) { //第一次关注 BuilderFactory.DefaultBulder(GlobalManager.getConnectString()).Create <NormalAccount>(new NormalAccount { AccountCode = GlobalManager.TheServiceAccount.AccountCode, OpenId = requestEventMessage.FromUserName, Gender = "F", SubscribeTime = DateTime.Now }); result = GlobalManager.TheWeiXinResponser.FormatOutputText(requestEventMessage.FromUserName, requestEventMessage.ToUserName, "欢迎加入" + GlobalManager.TheServiceAccount.Name); } else { //曾经关注过 BuilderFactory.DefaultBulder(GlobalManager.getConnectString()).Update <NormalAccount>(new NormalAccount { FollowStatus = 1, SubscribeTime = DateTime.Now, Id = normalAccount.Id }); result = GlobalManager.TheWeiXinResponser.FormatOutputText(normalAccount.OpenId, requestEventMessage.ToUserName, "欢迎重新加入" + GlobalManager.TheServiceAccount.Name); } _RefreshWXUserInfo(requestEventMessage.FromUserName, (ret) => { string gender = "N"; int iGender = int.Parse(ret.sex.ToString()); if (iGender == 1) { gender = "M"; } else if (iGender == 2) { gender = "F"; } BuilderFactory.DefaultBulder(GlobalManager.getConnectString()).ExecuteNativeSqlNoneQuery(new IBatisNetBatchStatement { StatementName = "NormalAccount_UpdateByOpenId", ParameterObject = new NormalAccount { OpenId = ret.openid, NickName = ret.nickname, Gender = gender, Language = ret.language, Country = ret.country, Province = ret.province, City = ret.city, HeadImgUrl = ret.headimgurl }.ToStringObjectDictionary(false), Type = SqlExecuteType.UPDATE }); }, null); #endregion } else if (requestEventMessage.Event.Equals("unsubscribe")) { #region 取消关注 NormalAccount normalAccount = BuilderFactory.DefaultBulder(GlobalManager.getConnectString()).List <NormalAccount>(new NormalAccount { OpenId = requestEventMessage.FromUserName, AccountCode = GlobalManager.TheServiceAccount.AccountCode }).FirstOrDefault(); if (normalAccount != null) { BuilderFactory.DefaultBulder(GlobalManager.getConnectString()).Update <NormalAccount>(new NormalAccount { FollowStatus = 0, SubscribeTime = DateTime.Now, Id = normalAccount.Id }); } #endregion } else if (requestEventMessage.Event.Equals("LOCATION")) { WXReceiveLocationMessage requestLocationMessage = XmlSerializeAdapter.Deserialize <WXReceiveLocationMessage>(result); var spParam = new { SourceTable = new NormalAccount().GetMappingTableName(), SourceId = requestLocationMessage.FromUserName, LocateType = "WX-Location-Report", Longitude = requestLocationMessage.Longitude, Latitude = requestLocationMessage.Latitude }.ToSPParam(); try { BuilderFactory.DefaultBulder().ExecuteSPNoneQuery("SP_Pub_InsertPubLocation", spParam); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } else { result = GlobalManager.TheWeiXinResponser.ResponseEvent(requestEventMessage); } } else if (receiveMessage.MsgType.Equals("location")) { } else if (receiveMessage.MsgType == "image") { } else if (receiveMessage.MsgType == "voice") { } } else { result = "hello"; } byte[] resultBytes = Encoding.UTF8.GetBytes(result); WebOperationContext.Current.OutgoingResponse.ContentType = "text/plain"; return(new MemoryStream(resultBytes)); }