C# PC版微信消息监听自动回复 - 小赫赫加油 - 博客园

来源: C# PC版微信消息监听自动回复 – 小赫赫加油 – 博客园

最近有个微商客户需要搞个 个人微信监听群消息关键字并实现自动回复功能, 因为他有很多群  很多买家咨询的话 一个个回复太麻烦, 客户要求 比如群里有人发 关键字 产品1  则自动回复产品1的相关描述

 

首先设置关键字,将关键字和回复内容存到一个txt就行

 

然后就是微信消息监听(windows的消息  句柄不懂的自己 google):

上监听的关键代码 :

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/// 消息主要接受程序
       /// </summary>
       /// <param name="m"></param>
       protected override void DefWndProc(ref Message m)
       {
           switch (m.Msg)
           {
               case 74:    //74代表WM_COPYDATA                   
                   string message = "";
                   int f = (int)m.WParam;
                   try
                   {
                       COPYDATASTRUCT2 cds2 = (COPYDATASTRUCT2)m.GetLParam(typeof(COPYDATASTRUCT2));
                       message = Marshal.PtrToStringAnsi(cds2.lpData);
                   }
                   catch (Exception e)
                   {
                       LogHelper.WriteLog("解析微信消息报错", e);  //发文字带空格报错
                   }
                   switch (f)
                   {
                       //登录时解析用户信息
                       case 10003:
                           try
                           {
                               if (!string.IsNullOrEmpty(message))
                               {
                                   MessageHandler.Parse10003(message);
                               }
                               this.Invoke(new MethodInvoker(() => { richTextBox1.AppendText("【" + MessageHandler.myUserInfo.UserName + "】登录成功!\r\n"); }));
                           }
                           catch (Exception e1)
                           {<br>                                LogHelper.WriteLog("解析微信登录用户信息错误", e1);
                           }
                           return;<br>
                       //文字,图片,视频,文件 解析
                       case 10012:
                           try
                           {
                               if (message.Contains("msgtyp:"))
                               {
                                   string[] datas = Regex.Split(message, "msgtyp:", RegexOptions.IgnoreCase);
                                   if (datas.Count() >= 2)
                                   {
                                       string msgtyp = datas[1].Split(',')[0];
                                       switch (msgtyp.Replace(" """))
                                       {
                                           case "1":  //文字
                                               if (message.Contains("[群-->]") && message.Contains("[消息内容-->]"))
                                               {
                                                   string word = "";      //发送内容
                                                   //取消息内容
                                                   string[] wordArr = Regex.Split(message, @"消息内容-->]", RegexOptions.IgnoreCase);
                                                   string[] aa = wordArr[1].Split(new string[] { "||" }, StringSplitOptions.RemoveEmptyEntries);
                                                   if (aa[0].StartsWith(" "))
                                                   {
                                                       word = aa[0].Substring(1);
                                                   }
                                                   else
                                                   {
                                                       word = aa[0];
                                                   }
                                                   string groupId = WxMessageHelper.GetChatRoom(message);
                                                   string groupName = WxMessageHelper.GetGroupName(message);
                                                   string nickName = WxMessageHelper.GetNickName(message);
                                                   string memberpuid = WxMessageHelper.GetWxId(message);
                                                   List<string> data = ReadFile();
                                                   foreach(var str in data)
                                                   {
                                                       string key = str.Split(';')[0];
                                                       string value = str.Split(';')[1];
                                                       if (word.Contains(key))
                                                       {
                                                           string[] sarr = Regex.Split(word, str, RegexOptions.IgnoreCase);
                                                           this.Invoke(new MethodInvoker(() => { richTextBox1.AppendText("【" + nickName + "】在【" + groupName + "】发送了关键词消息:"+ sarr[0]);
                                                               richTextBox1.AppendTextColorful(key, Color.Red, false);
                                                               richTextBox1.AppendText(key + "\r\n");
                                                           }));
                                                           //调用回复
                                                           SendMessage(groupId + "||" + value, 20001);
                                                       }
                                                   }
                                               }
                                               break;
                                       }
                                   }
                               }
                           }
                           catch (Exception e2)
                           {
                               this.Invoke(new MethodInvoker(() => { richTextBox1.AppendText(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "10012 : 解析微信群信息错误\r\n\r\n"); }));
                           }
                           break;
                       default:
                           break;
                   }
                   //消息内容  cds.lpData
                   break;
               default:
                   base.DefWndProc(ref m);
                   break;
           }
       }

 

 

1
WParam=10003 时,监听的是微信登录的操作, 这时给前台窗体一个提示, 提示登录成功 如下图

 

登录时message 消息体的值  ||分割  第一个就是微信昵称,  wxid_ 开头的就是微信唯一标识 , 最后是登录用户的头像

 

 

 

然后用 另一个号往群里发消息 (当前登录用户和那个号必须在一个群 )

发送  : 产品1  这时观察message 的值

 

@chatroom 结尾的是 群的唯一标识id   群聊就是群名称,   发送人昵称也有 发送的消息内容也有, 拿正则取一下就完事。

 

1
word就是提取的发送内容, 然后判断如果发送的内容包含关键字 则调用SendMessage方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/// <summary>
       /// 发送消息
       /// </summary>
       /// <param name="message">消息内容</param>
       /// <param name="wParam">通讯号</param>
       /// <returns></returns>
       private int SendMessage(string message, int wParam)
       {
           byte[] sarr = System.Text.Encoding.Default.GetBytes(message);
           int len = sarr.Length;
           COPYDATASTRUCT cds;
           cds.dwData = (IntPtr)Convert.ToInt16(1);    //可以是任意值
           cds.cbData = len + 1;   //指定lpData内存区域的字节数
           cds.lpData = message;  //发送给目标窗口所在进程的数据
           return myapi.SendMessage(common.微信句柄, 74, wParam, ref cds);  //74代表WM_COPYDATA
       }

 

 

1
2
3
4
5
6
7
8
9
10
[StructLayout(LayoutKind.Sequential, Size=1)]
public struct myapi
{
    [DllImport("user32", EntryPoint="SetWindowLong")]
    public static extern int SetWindowLongA(int hwnd, int nIndex, int dwNewLong);
    [DllImport("User32.dll")]
    public static extern int SendMessage(int hwnd, int msg, int wParam, ref COPYDATASTRUCT IParam);
    [DllImport("User32.dll")]
    public static extern int FindWindow(string lpClassName, string lpWindowName);
}

 

来2个效果图

 

 

 

 

这是用客户微信号  在群里 发 产品1   则当前微信立马回复  前面设置的回复内容

 

博客经个人辛苦努力所得,如有转载会特别申明,博客所有权归本人和博客园所有,如有转载请在显著位置给出博文链接和作者姓名,否则本人将付诸法律

原文地址: https://www.cnblogs.com/alonglonga/p/11876300.html    小赫赫首发

有问题联系Q: 591811930

赞(0) 打赏
分享到: 更多 (0)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏