/// <summary> /// 获取未接入的会话列表 /// </summary> /// <param name="userName">公众号</param> /// <param name="errorMessage">返回获取是否成功</param> /// <returns>返回未接入的会话列表</returns> public static WaitCaseSession GetWaitCase(string userName, out ErrorMessage errorMessage) { return(HttpHelper.RequestParsableResult <WaitCaseSession>(urlForGettingWaitCase, userName, out errorMessage, null, httpMethodForGettingWaitCase, null)); }
/// <summary> /// 获取客服的会话列表 /// </summary> /// <param name="userName">公众号</param> /// <param name="kfAccount">客服账号</param> /// <param name="errorMessage">返回获取是否成功</param> /// <returns>返回客服的会话列表</returns> public static CustomerServiceSession[] GetSessionList(string userName, string kfAccount, out ErrorMessage errorMessage) { string responseContent = HttpHelper.RequestResponseContent(urlForGettingSessionList, userName, new object[] { kfAccount }, httpMethodForGettingSessionList, null); if (string.IsNullOrWhiteSpace(responseContent)) { errorMessage = new ErrorMessage(ErrorMessage.ExceptionCode, "请求失败。"); return(null); } else if (ErrorMessage.IsErrorMessage(responseContent)) { errorMessage = ErrorMessage.Parse(responseContent); return(null); } else { JObject jo = JObject.Parse(responseContent); JToken jt; if (jo.TryGetValue("sessionlist", out jt) && jt.Type == JTokenType.Array) { JArray ja = (JArray)jt; CustomerServiceSession[] sessions = new CustomerServiceSession[ja.Count]; for (int i = 0; i < ja.Count; i++) { sessions[i] = new CustomerServiceSession(); sessions[i].Parse((JObject)ja[i]); } errorMessage = new ErrorMessage(ErrorMessage.SuccessCode, "请求成功。"); return(sessions); } else { errorMessage = new ErrorMessage(ErrorMessage.ExceptionCode, "解析结果失败。"); return(null); } } }
/// <summary> /// 获取客户的会话状态 /// </summary> /// <param name="userName">公众号</param> /// <param name="openId">客户账号</param> /// <param name="errorMessage">返回获取是否成功</param> /// <returns>返回客户的会话状态</returns> public static CustomerSession GetSession(string userName, string openId, out ErrorMessage errorMessage) { return(HttpHelper.RequestParsableResult <CustomerSession>(urlForGettingSession, userName, out errorMessage, new object[] { openId }, httpMethodForGettingSession, null)); }
/// <summary> /// 获取公众号中的客服聊天记录列表 /// </summary> /// <param name="userName">公众号</param> /// <param name="openId">普通用户的标识</param> /// <param name="startTime">查询开始时间</param> /// <param name="endTime">查询结束时间</param> /// <param name="pageSize">每页大小,每页最多拉取1000条</param> /// <param name="pageIndex">查询第几页,从1开始</param> /// <param name="errorMessage">返回获取是否成功</param> /// <returns>返回在线客服接待信息列表;如果获取失败,返回null。</returns> public static List <CustomerServiceRecord> GetRecord(string userName, string openId, DateTime startTime, DateTime endTime, int pageSize, int pageIndex, out ErrorMessage errorMessage) { List <CustomerServiceRecord> records = null; errorMessage = new ErrorMessage(ErrorMessage.ExceptionCode, ""); if (startTime > endTime) { errorMessage.errmsg = "查询开始时间不能大于查询结束时间。"; return(null); } if (pageSize <= 0 || pageSize > 1000) { errorMessage.errmsg = "每页大小错误。"; return(null); } if (pageIndex < 1) { errorMessage.errmsg = "查询页码错误。"; return(null); } var postData = new { starttime = MyWay.Areas.WeiXin.Models.Utility.ToWeixinTime(startTime), endtime = MyWay.Areas.WeiXin.Models.Utility.ToWeixinTime(endTime), openid = openId ?? string.Empty, pagesize = pageSize, pageindex = pageIndex }; string json = JsonConvert.SerializeObject(postData); string responseContent = HttpHelper.RequestResponseContent(urlForGettingRecord, userName, null, json); if (string.IsNullOrWhiteSpace(responseContent)) { errorMessage.errmsg = "从微信服务器获取响应失败。"; } else if (ErrorMessage.IsErrorMessage(responseContent)) { errorMessage = ErrorMessage.Parse(responseContent); } else { var recordlist = new { recordlist = new List <CustomerServiceRecord>() }; var recordList = JsonConvert.DeserializeAnonymousType(responseContent, recordlist); records = recordlist.recordlist; errorMessage = new ErrorMessage(ErrorMessage.SuccessCode, "请求成功。"); } return(records); }
/// <summary> /// 获取公众号中的在线客服接待信息列表 /// </summary> /// <param name="userName">公众号</param> /// <param name="errorMessage">返回获取是否成功</param> /// <returns>返回在线客服接待信息列表;如果获取失败,返回null。</returns> public static List <CustomerServiceOnlineInfo> GetOnlineKfList(string userName, out ErrorMessage errorMessage) { List <CustomerServiceOnlineInfo> infos = null; string responseContent = HttpHelper.RequestResponseContent(urlForGettingOnlineKfList, userName, null, httpMethodForGettingOnlineKfList, null); if (string.IsNullOrWhiteSpace(responseContent)) { errorMessage = new ErrorMessage(ErrorMessage.ExceptionCode, "请求失败。"); } else if (ErrorMessage.IsErrorMessage(responseContent)) { errorMessage = ErrorMessage.Parse(responseContent); } else { var kf_online_list = new { kf_online_list = new List <CustomerServiceOnlineInfo>() }; var kfOnlineList = JsonConvert.DeserializeAnonymousType(responseContent, kf_online_list); infos = kfOnlineList.kf_online_list; errorMessage = new ErrorMessage(ErrorMessage.SuccessCode, "获取在线客服接待信息成功。"); } return(infos); }
/// <summary> /// 从xml字符串解析消息 /// </summary> /// <param name="xml">xml字符串</param> /// <returns>返回消息</returns> public static RequestBaseMessage Parse(string xml) { try { RequestBaseMessage msg = null; //将xml字符串解析成JObject对象 XmlDocument doc = new XmlDocument(); doc.LoadXml(xml); string json = JsonConvert.SerializeXmlNode(doc); JObject jo = (JObject)JObject.Parse(json)["xml"]; string MsgType = (string)jo["MsgType"][CDATA_KEY]; WXLog.Write(MsgType, "进来" + MsgType + "事件了"); //解析消息基类 msg = ParseBaseMessage(jo); ErrorMessage e = null; BatchMeterial b = MyWay.Areas.WeiXin.Meterial.Meterial.BatchGet("测试公众号", MultiMediaTypeEnum.news, 0, 2, out e); //获取各分类的字段,并构造消息 switch (msg.MsgType) { case RequestMessageTypeEnum.text: string content = (string)jo["Content"][CDATA_KEY]; if (content == "天气") { msg = ParseImageMessage1(msg, jo, "https://mmbiz.qlogo.cn/mmbiz/D1aYATIFatWdxfb99IPptNDj3vqCiaSLpspegmdU4IFpRd40oqctN9gI9g2EZE4qq5eTqEAP2Xjb9J0onD5nNWQ/0?wx_fmt=jpeg", "nxoGNdQHF7AWPDlpfGZQ9_TkxdCdC3rUe0CbGZ5Ss5Y"); } else if (content == "时间") { msg = ParseTextMessage1(msg, jo); } else { msg = ParseTextMessage(msg, jo); } break; case RequestMessageTypeEnum.image: msg = ParseImageMessage(msg, jo); break; case RequestMessageTypeEnum.voice: msg = ParseVoiceMessage(msg, jo); break; case RequestMessageTypeEnum.video: msg = ParseVideoMessage(msg, jo); break; case RequestMessageTypeEnum.location: msg = ParseLocationMessage(msg, jo); break; case RequestMessageTypeEnum.link: msg = ParseLinkMessage(msg, jo); break; case RequestMessageTypeEnum.Event: msg = ParseEventMessage(msg, jo); break; default: throw new NotImplementedException(string.Format("未实现消息类型{0:g}解析。", msg.MsgType)); } //返回 return(msg); } catch (Exception ex) { Log.WriteFile(ex); throw; } }