Browse Source

Bug fix and add user_type to msg.

liuwons 9 years ago
parent
commit
beb95e6cfb
3 changed files with 48 additions and 11 deletions
  1. 13 3
      README.md
  2. 2 2
      test.py
  3. 33 6
      wxbot.py

+ 13 - 3
README.md

@@ -15,7 +15,7 @@ pip install pyqrcode
 ## 快速开发
 ### 代码
 
-利用 **wxBot** 最简单的方法就是继承WXBot类并实现handle_msg_all或者schedule函数,然后实例化子类并run,如下的代码对所有的文本消息回复 "hi", 并不断向好友tb发送"schedule"。
+利用 **wxBot** 最简单的方法就是继承WXBot类并实现handle_msg_all或者schedule函数,然后实例化子类并run,如下的代码对所有来自好友的文本消息回复 "hi", 并不断向好友tb发送"schedule"。
 handle_msg_all函数用于处理收到的每条消息,而schedule函数可以做一些任务性的事情(例如不断向好友推送信息或者一些定时任务)。
 
 ```python
@@ -27,8 +27,8 @@ from wxbot import *
 
 class MyWXBot(WXBot):
     def handle_msg_all(self, msg):
-        if msg['msg_type_id'] == 5:
-            self.send_msg(msg['user_name'], 'hi')
+        if msg['msg_type_id'] == 5 and msg['user_type'] == 'contact':
+            self.send_msg_by_uid('hi', msg['user_id'])
 
     def schedule(self):
         self.send_msg('tb', 'schedule')
@@ -77,12 +77,22 @@ handle_msg_all函数的参数msg是代表一条消息的字典。一般包含以
 
 | 字段名 | 意义 |
 | ----- | --- |
+| user_type | 用户类型,具体见用户类型表 |
 | msg_id | 消息id,微信内部数据 |
 | msg_type_id | 消息类型,具体见消息类型表 |
 | user_id | 发送消息的用户的id,微信内部数据 |
 | user_name | 发送消息的用户的名字,为备注名或者微信用户名 |
 | content | 消息体,不同类型消息的此字段内容不同,具体见消息类型表 |
 
+用户类型表:
+| 类型名 | 意义 |
+| ----- | ---- |
+| contact | 好友 |
+| public | 公众号 |
+| group | 群 |
+| special | 特殊账号 |
+| unknown | 未知 |
+
 消息类型表:
 
 | 类型号 | 类型名称 | 类型描述 | content |

+ 2 - 2
test.py

@@ -6,8 +6,8 @@ from wxbot import *
 
 class MyWXBot(WXBot):
     def handle_msg_all(self, msg):
-        if msg['msg_type_id'] == 5:
-            self.send_msg(msg['user_name'], 'hi')
+        if msg['msg_type_id'] == 5 and msg['user_type'] == 'contact':
+            self.send_msg_by_uid('hi', msg['user_id'])
 '''
     def schedule(self):
         self.send_msg('tb', 'schedule')

+ 33 - 6
wxbot.py

@@ -37,8 +37,10 @@ class WXBot:
         self.sync_key = []
         self.user = []
         self.member_list = []
-        self.contact_list = []
-        self.group_list = []
+        self.contact_list = []  # contact list
+        self.public_list = []   # public account list
+        self.group_list = []    # group chat list
+        self.special_list = []  # special list account
         self.sync_host = ''
         self.session = requests.Session()
         self.session.headers.update({'User-Agent': 'Mozilla/5.0 (X11; Linux i686; U;) Gecko/20070322 Kazehakase/0.4.5'})
@@ -172,18 +174,24 @@ class WXBot:
         for contact in contact_list:
             if contact['VerifyFlag'] & 8 != 0: # public account
                 contact_list.remove(contact)
+                self.public_list.append(contact)
             elif contact['UserName'] in SpecialUsers: # special account
                 contact_list.remove(contact)
+                self.special_list.append(contact)
             elif contact['UserName'].find('@@') != -1: # group
-                self.group_list.append(contact)
                 contact_list.remove(contact)
+                self.group_list.append(contact)
             elif contact['UserName'] == self.user['UserName']: # self
                 contact_list.remove(contact)
         self.contact_list = contact_list
 
         if self.DEBUG:
-            with open('contactlist.json', 'w') as f:
+            with open('contact_list.json', 'w') as f:
                 f.write(json.dumps(self.contact_list))
+            with open('special_list.json', 'w') as f:
+                f.write(json.dumps(self.special_list))
+            with open('group_list.json', 'w') as f:
+                f.write(json.dumps(self.group_list))
 
         return True
 
@@ -298,8 +306,24 @@ class WXBot:
                 return member['UserName']
         return None
 
+    def get_user_type(self, wx_user_id):
+        for account in self.contact_list:
+            if wx_user_id == account['UserName']:
+                return 'contact'
+        for account in self.public_list:
+            if wx_user_id == account['UserName']:
+                return 'public'
+        for account in self.special_list:
+            if wx_user_id == account['UserName']:
+                return 'special'
+        for account in self.group_list:
+            if wx_user_id == account['UserName']:
+                return 'group'
+        return 'unknown'
+
     '''
     msg:
+        user_type
         msg_id
         msg_type_id
         user_id
@@ -331,7 +355,10 @@ class WXBot:
         for msg in r['AddMsgList']:
             mtype = msg['MsgType']
 
-            name = self.get_user_remark_name(msg['FromUserName'])
+            wx_user_id = msg['FromUserName']
+            user_type = self.get_user_type(wx_user_id)
+
+            name = self.get_user_remark_name(wx_user_id)
             content = msg['Content'].replace('&lt;','<').replace('&gt;','>')
             msg_id = msg['MsgId']
             msg_type_id = 99
@@ -445,7 +472,7 @@ class WXBot:
                 if self.DEBUG:
                     print '[Unknown] : %s' % str(mtype)
                     print msg
-            message = {'msg_id':msg_id, 'msg_type_id': msg_type_id, 'content': content, 'user_id': msg['FromUserName'], 'user_name': name}
+            message = {'user_type': user_type, 'msg_id':msg_id, 'msg_type_id': msg_type_id, 'content': content, 'user_id': msg['FromUserName'], 'user_name': name}
             self.handle_msg_all(message)
 
     def schedule(self):