/// <summary> /// 客服接受访客对话请求 /// </summary> /// <param name="operatorId"></param> /// <param name="chatId"></param> /// <returns></returns> public static int AcceptChatRequest(string operatorId, string chatId) { Trace.WriteLine(string.Format("ChatService.AcceptChatRequest(OperatorId={0},ChatId={1})", operatorId, chatId)); Chat chat = GetChatById(chatId); if (chat == null) { Trace.WriteLine(string.Format("Error: AcceptChatRequest(OperatorId={0},ChatId={1}) error, can't find this chat", operatorId, chatId)); return(AcceptChatRequestReturn_Error_Others); } if (chat.Status == ChatStatus.Accepted) { return(AcceptChatRequestReturn_Error_AcceptedByOthers); } else if (chat.Status == ChatStatus.Requested) { chat.Status = ChatStatus.Accepted; chat.OperatorId = operatorId; chat.AcceptTime = DateTime.Now; if (VisitorChatRequestAccepted != null) { VisitorChatRequestAccepted(null, new VisitorChatRequestAcceptedEventArgs(new VisitorChatRequestEventArgs(chat.VisitorId, chat))); } Message m1 = new Message(); m1.ChatId = chat.ChatId; m1.SentDate = DateTime.Now; m1.Type = MessageType.SystemMessage_ToVisitor; m1.Text = string.Format("客服:{0}已经接受您的对话请求", OperatorService.GetOperatorById(operatorId).NickName); SendMessage(m1); Message m2 = new Message(); m2.ChatId = chat.ChatId; m2.SentDate = DateTime.Now; m2.Type = MessageType.SystemMessage_ToOperator; m2.Text = string.Format("你已经接受访客{0}的对话请求", VisitorService.GetVisitorById(chat.VisitorId).Name); SendMessage(m2); OperatorService.SetOperatorStatus(operatorId, OperatorStatus.Chatting); VisitSession s = VisitSessionService.GetSessionById(VisitorService.GetVisitorById(chat.VisitorId).CurrentSessionId); s.OperatorId = operatorId; s.Status = VisitSessionStatus.Chatting; s.ChatingTime = DateTime.Now; return(AcceptChatRequestReturn_OK); } else if (chat.Status == ChatStatus.Closed) { return(AcceptChatRequestReturn_Error_ChatRequestCanceled); } else { Trace.WriteLine(string.Format("ChatService.AccpetChatRequest({0},{1}) error,status illegal: ChatStatus={2}", operatorId, chatId, chat.Status)); return(AcceptChatRequestReturn_Error_Others); } }
/// <summary> /// 页面请求对话 /// </summary> /// <param name="CurrentVisitor"></param> /// <returns>ChatId</returns> public static string ChatPageRequestChat(Visitor visitor) { Trace.WriteLine(string.Format("ChatService.ChatPageRequestChat(Visitor = {0})", visitor)); if (!string.IsNullOrEmpty(visitor.Name)) { Visitor vs = VisitorService.GetVisitorById(visitor.VisitorId); vs.Name = visitor.Name; VisitorService.UpdateVisitor(vs); } Chat chatRequest = new Chat(); chatRequest.AccountId = visitor.AccountId; chatRequest.CreateTime = DateTime.Now; chatRequest.Status = ChatStatus.Requested; chatRequest.VisitorId = visitor.VisitorId; chats.Add(chatRequest); Provider.AddChat(chatRequest); VisitSession s = VisitSessionService.GetSessionById(visitor.CurrentSessionId); s.ChatRequestTime = DateTime.Now; s.Status = VisitSessionStatus.ChatRequesting; Message m = new Message(); m.ChatId = chatRequest.ChatId; m.Text = "正在接入客服,请稍等..."; m.Type = MessageType.SystemMessage_ToVisitor; m.Source = "系统"; m.Destination = "访客"; SendMessage(m, DateTime.Now.AddMilliseconds(100)); if (VisitorChatRequest != null) { VisitorChatRequestEventArgs req = new VisitorChatRequestEventArgs(visitor.VisitorId, chatRequest); visitorChatRequests.Add(req); VisitorChatRequest(null, req); } return(chatRequest.ChatId); }
public void TestGetSessionById() { MyVisitSessionServiceProvider provider = new MyVisitSessionServiceProvider(); VisitSessionService.Provider = provider; VisitSession v = new VisitSession(); v.SessionId = "aaaaa"; VisitSessionService.NewSession(v); Assert.AreEqual(v, VisitSessionService.GetSessionById(v.SessionId)); Assert.AreEqual(1, VisitSessionService.Sessions.Count); for (int i = 0; i < 20; i++) { VisitSession vv = new VisitSession(); vv.SessionId = "aaaaa"; VisitSessionService.NewSession(vv); } Assert.AreEqual(1, VisitSessionService.Sessions.Count); }
/// <summary> /// 接受客服邀请 /// </summary> /// <param name="chatId"></param> public static void AcceptOperatorInvitation(string chatId) { Trace.WriteLine(string.Format("ChatService.AcceptOperatorInvitation(ChatId = {0})", chatId)); Chat chat = GetChatById(chatId); if (chat != null) { chat.Status = ChatStatus.Accepted; if (chat.OperatorId != null) { OperatorService.GetOperatorById(chat.OperatorId).Status = OperatorStatus.Chatting; } VisitSession s = VisitSessionService.GetSessionById(VisitorService.GetVisitorById(chat.VisitorId).CurrentSessionId); s.Status = VisitSessionStatus.Chatting; //将访客状态改为对话中 s.ChatingTime = DateTime.Now; OperatorChatRequestEventArgs opq = new OperatorChatRequestEventArgs(chat.OperatorId, chat.VisitorId, chat); //tao OperatorChatRequestAccepted(null, new OperatorChatRequestAcceptedEventArgs(opq)); //tao } //SendMessage(new Message(chat.ChatId, "访客已接受对话邀请!", MessageType.SystemMessage_ToOperator)); SendMessage(new Message(chat.ChatId, "您已接受对话邀请!", MessageType.SystemMessage_ToVisitor)); }
//开始对话 protected void btnStarChat_Click(object sender, EventArgs e) { string chatId = CurrentVisitor.CurrentSessionId; if (CurrentChat == null || CurrentChat.Status == ChatStatus.Closed) { if (!string.IsNullOrEmpty(txtName.Text) && CurrentVisitor.Name == VisitSessionService.GetSessionById(CurrentVisitor.CurrentSessionId).IP) { CurrentVisitor.Name = txtName.Text; } if (!string.IsNullOrEmpty(txtEmail.Text) && !string.IsNullOrEmpty(CurrentVisitor.Email)) { CurrentVisitor.Email = txtEmail.Text; } VisitorName = CurrentVisitor.Name; VName = CurrentVisitor.Name; chatId = ChatService.ChatPageRequestChat(CurrentVisitor); setCookie(chatId); } pnlChat.Visible = true; pnlRequest.Visible = false; }