private GroupMessage make_group_msg(UserMessage userMessage) { WriteLog("make group message"); GroupMessage groupMessage = new GroupMessage(this); groupMessage.setRawMsg(userMessage.getRawMsg()); groupMessage.setMsgId(((JValue)userMessage.getRawMsg()["MsgId"]).Value.ToString()); groupMessage.setFromUserName(((JValue)userMessage.getRawMsg()["FromUserName"]).Value.ToString()); groupMessage.setToUserName(((JValue)userMessage.getRawMsg()["ToUserName"]).Value.ToString()); groupMessage.setMsgType(((JValue)userMessage.getRawMsg()["MsgType"]).Value.ToString()); groupMessage.setText(userMessage.getText()); String content = ((JValue)userMessage.getRawMsg()["Content"]).Value.ToString().Replace("<", "<").Replace(">", ">"); Dictionary <String, String> group = null, src = null; if (groupMessage.getFromUserName().StartsWith("@@")) { //接收到来自群的消息 String g_id = groupMessage.getFromUserName(); groupMessage.setGroupId(g_id); group = this.getGroupById(g_id); if (content.Contains(":<br/>")) { String u_id = content.Split(new string[] { ":<br/>" }, StringSplitOptions.None)[0]; src = getGroupUserById(u_id, g_id); } } else if (groupMessage.getToUserName().StartsWith("@@")) { // 自己发给群的消息 String g_id = groupMessage.getToUserName(); groupMessage.setGroupId(g_id); String u_id = groupMessage.getFromUserName(); src = this.getGroupUserById(u_id, g_id); group = this.getGroupById(g_id); } if (null != src) { groupMessage.setUser_attrstatus(src["AttrStatus"]); groupMessage.setUser_display_name(src["DisplayName"]); groupMessage.setUser_nickname(src["NickName"]); } if (null != group) { groupMessage.setGroup_count(group["MemberCount"]); groupMessage.setGroup_owner_uin(group["OwnerUin"]); groupMessage.setGroup_name(group["ShowName"]); } groupMessage.setTimestamp(((JValue)userMessage.getRawMsg()["CreateTime"]).Value.ToString()); return(groupMessage); }
/// <summary> /// 处理发来的消息 /// </summary> /// <param name="dic"></param> public void handle_msg(JObject dic) { WriteLog("handle message"); if (null != messageHandle) { messageHandle.WxSync(dic); } int intMegCount = dic["AddMsgList"].Count(); if (intMegCount == 0) { return; } WriteLog(Const.LOG_MSG_NEW_MSG, intMegCount); JArray msgs = (JArray)dic["AddMsgList"]; foreach (JObject element in msgs) { //JsonObject msg = element.getAsJsonObject(); String msgType = ((JValue)element["MsgType"]).Value.ToString(); String msgId = ((JValue)element["MsgId"]).Value.ToString(); String content = ((JValue)element["Content"]).Value.ToString().Replace("<", "<").Replace(">", ">"); UserMessage userMessage = new UserMessage(this); userMessage.setRawMsg(element); switch ((EnumMsgType)(int.Parse(msgType))) { case EnumMsgType.MSGTYPE_TEXT: // 地理位置消息 if (content.Contains("pictype=location")) { String location = content.Split(new string[] { "<br/>" }, StringSplitOptions.None)[1]; userMessage.setLocation(location); userMessage.setLog(String.Format(Const.LOG_MSG_LOCATION, location)); } else { // 普通文本 String text = null; if (content.Contains(":<br/>")) { text = content.Split(new string[] { ":<br/>" }, StringSplitOptions.None)[1]; } else { text = content; } userMessage.setText(text); userMessage.setLog(text.Replace("<br/>", "\n")); } break; case EnumMsgType.MSGTYPE_IMAGE: WriteLog(Const.LOG_MSG_PICTURE, ""); break; case EnumMsgType.MSGTYPE_STATUSNOTIFY: WriteLog(Const.LOG_MSG_NOTIFY_PHONE); break; } //// 文本groupMessage //if (conf["MSGTYPE_TEXT"] == msgType) //{ //} //else if (conf["MSGTYPE_STATUSNOTIFY"] == msgType) //{ // WriteLog(Const.LOG_MSG_NOTIFY_PHONE); // return; //} this.show_msg(userMessage); Boolean isGroupMsg = (((JValue)element["FromUserName"]).Value.ToString() + ((JValue)element["ToUserName"]).Value.ToString()).Contains("@@"); if (isGroupMsg) { GroupMessage groupMessage = make_group_msg(userMessage); if (null != messageHandle) { messageHandle.groupMessage(groupMessage); } } else { if (null != messageHandle) { messageHandle.userMessage(userMessage); } } } }