最新微信hook二次开发C#Demo之获取消息代码

用【江南鹤微信hook二次开发c#源代码】获取消息:
消息类型有以下几种 1 文本,2 图片,3 表情(gif),4 视频,5 文件,6 名片,7 位置,8 小程序,9 链接,10公众号文章列表
发送方有3种 1 好友,2 群,3 公众号,4 系统

hook程序都已实现上述类型的消息,c#代码直接调用即可,下面是获取的代码

第一步 初始化服务


var HP_Server = new TcpPackServer
{
MaxPackSize = 0x3FFFFF, IpAddress = "0.0.0.0", Port = 0
};
HP_Server.OnReceive += OnReceive;//获取消息事件

if (HP_Server.Start())
{
string szIPAddress = string.Empty;
ushort dwPort = 0;
HP_Server.GetListenAddress(ref szIPAddress, ref dwPort);
if (dwPort == 0)
{
MessageBox.Show("通信端口创建失败,请重试!", "错误");
Environment.Exit(0);
}

NMHelper nmHeler = new NMHelper {Socket_Port = dwPort};
WriteLog("[初始化]" + NewLine + NewLine + "通信端口:" + nmHeler.Socket_Port.ToString());
}
else
{
MessageBox.Show("创建通信服务失败,请重试!", "错误");
Environment.Exit(0);
}

第二步 获取消息事件


HandleResult OnReceive(IServer sender, IntPtr connId, byte[] bytes)
{
    string s_Recv = Encoding.Default.GetString(bytes);
    JObject RecvJson = JObject.Parse(s_Recv);
    int RecvType;
    if(nmHeler.Json_GetInt(RecvJson, "PackType",out RecvType) == true)
    {
        if (RecvType == NMHelper.Recv_Msg)
        {
            Event_RecvMsg((int)connId, RecvJson);
        }
    }
    return HandleResult.Ok;
}

第二步 消息内容分析

需要源码的请+微信 weixinhook


void Event_RecvMsg(int ConnID, JObject RecvJson)
{
    NMHelper.nmMsgInfo MsgInfo = new NMHelper.nmMsgInfo
    {
        RecvWxid = nmHeler.Json_GetString(RecvJson, "RecvWxid", true),
        Type = nmHeler.Json_GetIntEx(RecvJson, "Type"),
        State = nmHeler.Json_GetIntEx(RecvJson, "State"),
        Time = nmHeler.Json_GetIntEx(RecvJson, "Time"),
        IsSend = nmHeler.Json_GetIntEx(RecvJson, "Sender"),
        LocalMsgID = nmHeler.Json_GetIntEx(RecvJson, "LocalID"),
        ServerMsgID = nmHeler.Json_GetString(RecvJson, "ServerID", false),
        Wxid_1 = nmHeler.Json_GetString(RecvJson, "Wxid_1", true),
        Nick_1 = nmHeler.Json_GetString(RecvJson, "Nick_1", true),
        Wxid_2 = nmHeler.Json_GetString(RecvJson, "Wxid_2", true),
        Nick_2 = nmHeler.Json_GetString(RecvJson, "Nick_2", true),
        Msg = nmHeler.Json_GetString(RecvJson, "Msg", true),
        Source = nmHeler.Json_GetString(RecvJson, "Source", true)
    };
    //File.AppendAllText("msg.txt",JsonConvert.SerializeObject(MsgInfo));
    string outLog = (MsgInfo.IsSend > 0 ? "[发出消息]" : "[接收消息]") + MsgInfo.RecvWxid + NewLine;
    outLog += "类型:" + MsgInfo.Type.ToString() + "  状态:" + MsgInfo.State.ToString() + "    时间:" + nmHeler.Timestamp2DataTime(MsgInfo.Time) + NewLine;
    outLog += "本地ID:" + MsgInfo.LocalMsgID.ToString() + "    服务器ID:" + MsgInfo.ServerMsgID + NewLine;
    outLog += "Wxid1:" + MsgInfo.Wxid_1 + (MsgInfo.Nick_1 == String.Empty ? string.Empty : "(" + MsgInfo.Nick_1 + ")") + NewLine;
    outLog += "Wxid2:" + MsgInfo.Wxid_2 + (MsgInfo.Nick_2 == String.Empty ? string.Empty : "(" + MsgInfo.Nick_2 + ")") + NewLine;
    outLog += "消息内容:" + MsgInfo.Msg + NewLine + "附加消息:" + MsgInfo.Source + NewLine;

    if (MsgInfo.Type == 43 || MsgInfo.Type == 49)
    {
        string FilePath = nmHeler.Json_GetString(RecvJson, "File", true);
        if (FilePath != string.Empty)
        {
            outLog += "文件路径:" + FilePath;
        }
    }
    else if(MsgInfo.Type == 3 || MsgInfo.Type == 34)
    {
        string FileData = nmHeler.Json_GetString(RecvJson, "File", false);
        if (FileData != string.Empty)
        {
            byte[] FileBytes = Convert.FromBase64String(FileData);
            string FilePath = nmHeler.SaveFile(MsgInfo.RecvWxid, MsgInfo.Wxid_1, (MsgInfo.Type == 3 ? "IMAGE" : "VOICE"), MsgInfo.ServerMsgID, (MsgInfo.Type == 3 ? "jpg" : "silk"), FileBytes);
            outLog += "文件路径:" + FilePath;
        }
    }

    if (checkBox1.Checked == true && MsgInfo.Type == 37) {
        nmCore.NM_PassApply(ConnID, MsgInfo.Msg);
    }

    WriteLog(outLog);

    //自动填充消息内容,用于撤回消息
    if (chk_RevokeFill.Checked == true && MsgInfo.IsSend == 1 && ConnID == nmHeler.Socket_ConnId) 
    {
        text_Recvke_Wxid.Text = MsgInfo.Wxid_1;
        text_Recvke_Sid.Text = MsgInfo.ServerMsgID;
        text_Recvke_Message.Text = MsgInfo.Msg;
    }
    //转账自动收款
    if(chk_GetMoney.Checked == true && MsgInfo.IsSend == 0 && MsgInfo.Type == 49)
    {
        if(MsgInfo.Msg.IndexOf("微信转账") != -1 && MsgInfo.Msg.IndexOf("收到转账") != -1)
        {
            string State = nmHeler.StringMid(MsgInfo.Msg, "", "");
            string Transid = nmHeler.StringMid(MsgInfo.Msg, "transferid", "");
            int dwTime = 0;
            int.TryParse(nmHeler.StringMid(MsgInfo.Msg, "", ""), out dwTime);
            if (State == "1" && Transid != string.Empty) 
            {
                nmCore.NM_GetMoney(ConnID, MsgInfo.Wxid_1, Transid, dwTime);
            }
        }
    }
    //名片消息自动填充数据
    if(chk_CardFill.Checked == true && MsgInfo.IsSend == 0 && MsgInfo.Type == 42 && ConnID == nmHeler.Socket_ConnId)
    {
        string v1 = nmHeler.StringMid(MsgInfo.Msg, "username=\"", "\" nickname=");
        string v2 = nmHeler.StringMid(MsgInfo.Msg, "antispamticket=\"", "\" /");
        text_Friend_v1.Text = v1;
        text_Friend_v2.Text = v2;
    }
    //自动接受群聊邀请
    if(chk_PassInvite.Checked == true && MsgInfo.IsSend == 0 && MsgInfo.Type == 49)
    {
        if (MsgInfo.Msg.IndexOf("邀请你加入群聊") != -1 && MsgInfo.Msg.IndexOf("mmsupport-bin/addchatroombyinvite") != -1)
        {
            string InviteURL = nmHeler.StringMid(MsgInfo.Msg, "", "");
            if(InviteURL != string.Empty)
            {
                nmCore.NM_GetA8Key(ConnID, MsgInfo.Wxid_1, InviteURL);
            }
        }
    }
}

划重点!!!

通过对接受到的消息进行分析,可以做成多种功能

1 微信聊天机器人
对消息进行关键词分析,针对关键词进行回复。可以建立一套关键词和回复内容的库。用程序自动化处理,就是一个微信机器人了。
2 微信公众号文章采集
微信公众号发布文章时,程序收到消息,是xml格式,包含文章的标题,摘要,发布时间,封面,文章链接等内容,可以抓取文章的详细内容。
3 文件自动保存
当收到图片,语音,视频,文件消息时程序会自动保存到本地
4 可对群进行监控统计
5 自动收款

您可能还喜欢...