public CommonMessage(Msg msg, PermissionLevel level = PermissionLevel.Public) { Message = msg.Message; UserId = msg.UserId.ToString(); MessageId = msg.MessageId; PermissionLevel = level; switch (msg) { case PrivateMsg privateMsg: MessageType = MessageType.Private; Private = privateMsg; break; case DiscussMsg discussMsg: MessageType = MessageType.Discuss; Discuss = discussMsg; DiscussId = discussMsg.DiscussId.ToString(); break; case GroupMsg groupMsg: MessageType = MessageType.Group; Group = groupMsg; GroupId = groupMsg.GroupId.ToString(); break; } }
/// <summary> /// 群消息 /// </summary> /// <param name="msg"></param> /// <returns>0不拦截 1拦截消息</returns> public override int GroupMsgProcess(GroupMsg msg, long CurrentQQ) { Console.WriteLine($"GroupMsgProcess {CurrentQQ}\n" + JsonConvert.SerializeObject(msg)); if (msg.FromGroupId != 516141713) { return(0); } if (msg.MsgType == MsgType.PicMsg) { var picContent = msg.GetPic(); Apis.SendGroupMsg(msg.FromGroupId, picContent.Content + CodeUtils.At(msg.FromUserId) + CodeUtils.Pic_Http(picContent.FriendPic.FirstOrDefault().Url)); } else if (msg.MsgType == MsgType.VoiceMsg) { var voiceContent = msg.GetVoice(); Apis.SendGroupMsg(msg.FromGroupId, voiceContent.Content + CodeUtils.Voice_Http(voiceContent.Url)); } else { Apis.SendGroupMsg(msg.FromGroupId, msg.Content + CodeUtils.At(msg.FromUserId)); } Apis.RevokeMsg(new RevokeMsgReq { GroupID = msg.FromGroupId, MsgRandom = msg.MsgRandom, MsgSeq = msg.MsgRandom }); return(0); }
public CommonMessage(GroupMsg groupMsg, PermissionLevel level = PermissionLevel.Public) { Message = groupMsg.Message; UserId = groupMsg.UserId.ToString(); GroupId = groupMsg.GroupId.ToString(); MessageId = groupMsg.MessageId; MessageType = MessageType.Group; PermissionLevel = level; Group = groupMsg; }
public string InsertGroupMsgs(InsertDataViewModel data) { try { using (SalesERPDAL salesDal = new SalesERPDAL()) { GroupMsg result = salesDal.GroupMsgs.Add(data.groupMsg); FileMsg fResult = salesDal.FileMsgs.Add(data.fileMsg); salesDal.SaveChanges(); return "Success"; } } catch (Exception ex) { return "Fail:" + ex.Message; } }
/// <summary> /// 群消息 /// </summary> /// <param name="msg">消息体</param> /// <param name="currentQQ">当前机器人QQ</param> /// <returns>0不拦截 1拦截消息</returns> public abstract int GroupMsgProcess(GroupMsg msg, long currentQQ);
public override CommonMessageResponse Message_Received(CommonMessage messageObj) { // 接收的信息 string message = messageObj.Message; // 发送者的QQ string userId = messageObj.UserId; UserId = userId; // 发送者所在群ID(若是私聊或讨论组则为null) string groupId = messageObj.GroupId; // 发送者所在讨论组ID(若是私聊或群则为null) string discussId = messageObj.DiscussId; // 包含消息种类,分别为Group, Discuss, Private,省去判断以上是否为null来判定消息种类 MessageType type = messageObj.MessageType; if (type != MessageType.Private) { Logger.Error("不能非私聊"); //return null; // 返回null则不返回消息 } // 接收的信息的Id,用于撤回等操作 long msgId = messageObj.MessageId; // 若是命令,下面两个字段不为null,当接收消息是 "/demo asdf 1234" 的情况: // 此字段为 "demo" string command = messageObj.Command; // 此字段为 "asdf 1234" string argString = messageObj.ArgString; // 当前处于的权限状态,默认为Public(即开放权限) // 当用户执行 /sudo 或 /root 会分别触发 Admin(对应群的管理员)和 Root(系统管理员),以此做出对应权限所对应的功能 PermissionLevel level = messageObj.PermissionLevel; if (level == PermissionLevel.Public) { Logger.Info("当前所用权限:Public"); } else if (level == PermissionLevel.Admin) { Logger.Info("当前所用权限:Admin"); } else if (level == PermissionLevel.Root) { Logger.Info("当前所用权限:Root"); } // 暂无实际用处,当前为框架所用,后续会有变动 string fullcmd = messageObj.FullCommand; // 包含json所传原生参数,通常只有少数情况会使用(获取字体,发送时间,匿名情况等) PrivateMsg privateObj = messageObj.Private; DiscussMsg discussObj = messageObj.Discuss; GroupMsg groupObj = messageObj.Group; // 假设做一个定时报告程序(此仅为全局共享,对于用户用途不大) if (string.IsNullOrEmpty(argString)) { return(new CommonMessageResponse("请填写参数", messageObj, enableAt: true)); } string[] param = argString.Split(" "); if (param.Length > 2) { return(new CommonMessageResponse("参数不正确", messageObj, enableAt: true)); } if (param[0] == "start" && int.TryParse(param[1], out int sleepTime)) { // 默认处理机制为单线程,返回一个对象主线程继续工作 // 若需新建线程,则手动处理: if (_tThread != null && _tThread.IsAlive) { return(new CommonMessageResponse("计时器正在工作,请先停止", messageObj, enableAt: true)); } _tThread = new Thread(new ParameterizedThreadStart(MultiThread)); _tThread.Start(sleepTime); string reply = "启动了计时器"; // 当所用参数为(string,CommonMessage),则自动返回给所在群(组)的所在成员(通常不用其他重载,为框架所用) return(new CommonMessageResponse(reply, messageObj, enableAt: true)); } else if (param[0] == "stop" && param.Length == 1) { if (_tThread != null && _tThread.IsAlive) { _tThread.Abort(); } string reply = "计时器已经停止"; return(new CommonMessageResponse(reply, messageObj, enableAt: true)); } else { return(new CommonMessageResponse("参数不正确", messageObj, enableAt: true)); } // 若需回复至别处,需以下实现 string reply2 = "回复到了别处"; string userId2 = "xxxxxxx"; long groupId2 = 123456; SendMessage(new CommonMessageResponse(reply2, new Identity(groupId2, MessageType.Group), userId2)); return(null); }
public void Json_Received(object sender, JsonReceivedEventArgs args) { string json = args.JsonString; dynamic obj = JsonConvert.DeserializeObject(json); // 判断post类别 try { if (obj.post_type == "message") { // 私聊 if (obj.message_type == "private") { PrivateMsg parsedObj = JsonConvert.DeserializeObject <PrivateMsg>(json); Core.ReceiveMessage(parsedObj); } // 群聊 else if (obj.message_type == "group") { GroupMsg parsedObj = JsonConvert.DeserializeObject <GroupMsg>(json); Core.ReceiveMessage(parsedObj); } // 讨论组 else if (obj.message_type == "discuss") { DiscussMsg parsedObj = JsonConvert.DeserializeObject <DiscussMsg>(json); Core.ReceiveMessage(parsedObj); } } else if (obj.post_type == "notice") { // 群文件上传 if (obj.notice_type == "group_upload") { GroupFileUpload parsedObj = JsonConvert.DeserializeObject <GroupFileUpload>(json); // TODO } // 群管理员变动 else if (obj.notice_type == "group_admin") { GroupAdminChange parsedObj = JsonConvert.DeserializeObject <GroupAdminChange>(json); // TODO } // 群成员增加/减少 else if (obj.notice_type == "group_decrease" || obj.notice_type == "group_increase") { GroupMemberChange parsedObj = JsonConvert.DeserializeObject <GroupMemberChange>(json); // TODO } // 好友添加 else if (obj.notice_type == "friend_add") { FriendAdd parsedObj = JsonConvert.DeserializeObject <FriendAdd>(json); // TODO } } else if (obj.post_type == "request") { // 加好友请求 if (obj.request_type == "friend") { FriendRequest parsedObj = JsonConvert.DeserializeObject <FriendRequest>(json); // TODO } // 加群请求/邀请 else if (obj.request_type == "group") { GroupInvite parsedObj = JsonConvert.DeserializeObject <GroupInvite>(json); // TODO } } } catch (Exception ex) { Logger.Exception(ex); } }
public void Json_Received(object sender, JsonReceivedEventArgs args) { string json = args.JsonString; dynamic obj = JsonConvert.DeserializeObject(json); // 判断post类别 try { if (obj.post_type == "message") { // 私聊 if (obj.message_type == "private") { PrivateMsg parsedObj = JsonConvert.DeserializeObject <PrivateMsg>(json); Core.ReceiveMessage(parsedObj); } // 群聊 else if (obj.message_type == "group") { GroupMsg parsedObj = JsonConvert.DeserializeObject <GroupMsg>(json); Core.ReceiveMessage(parsedObj); } // 讨论组 else if (obj.message_type == "discuss") { DiscussMsg parsedObj = JsonConvert.DeserializeObject <DiscussMsg>(json); Core.ReceiveMessage(parsedObj); } } else if (obj.post_type == "notice") { // 群文件上传 if (obj.notice_type == "group_upload") { GroupFileUpload parsedObj = JsonConvert.DeserializeObject <GroupFileUpload>(json); // TODO } // 群管理员变动 else if (obj.notice_type == "group_admin") { GroupAdminChange parsedObj = JsonConvert.DeserializeObject <GroupAdminChange>(json); // TODO } // 群成员增加/减少 else if (obj.notice_type == "group_decrease" || obj.notice_type == "group_increase") { GroupMemberChange parsedObj = JsonConvert.DeserializeObject <GroupMemberChange>(json); // TODO } // 好友添加 else if (obj.notice_type == "friend_add") { FriendAdd parsedObj = JsonConvert.DeserializeObject <FriendAdd>(json); // TODO } } else if (obj.post_type == "request") { // 加好友请求 if (obj.request_type == "friend") { FriendRequest parsedObj = JsonConvert.DeserializeObject <FriendRequest>(json); // TODO,临时 CqApi.SendPrivateMessage("2241521134", string.Format("{0} ({1})邀请加我为好友", CqApi.GetStrangerInfo(parsedObj.UserId.ToString()).Data?.Nickname, parsedObj.UserId)); } // 加群请求/邀请 else if (obj.request_type == "group") { GroupInvite parsedObj = JsonConvert.DeserializeObject <GroupInvite>(json); // TODO,临时 if (parsedObj.SubType == "invite") { CqApi.SendPrivateMessage("2241521134", string.Format("{0} ({1})邀请我加入群{2}", CqApi.GetStrangerInfo(parsedObj.UserId.ToString()).Data?.Nickname, parsedObj.UserId, parsedObj.GroupId)); } } } } catch (Exception ex) { Logger.Exception(ex); } }