Browse Source

bot.py 修复自定义表情等不回复bug

liuyuqi-dellpc 8 years ago
parent
commit
20c728a834
4 changed files with 18 additions and 7 deletions
  1. 1 0
      .gitignore
  2. 14 6
      bot.py
  3. 2 0
      conf.ini.bak
  4. 1 1
      wxbot.py

+ 1 - 0
.gitignore

@@ -69,3 +69,4 @@ qr.png
 /*.jpg
 /*.jpg
 *.ini
 *.ini
 *.un~
 *.un~
+/temp/wxqr.png

+ 14 - 6
bot.py

@@ -20,7 +20,7 @@ class TulingWXBot(WXBot):
         except Exception:
         except Exception:
             pass
             pass
         print 'tuling_key:', self.tuling_key
         print 'tuling_key:', self.tuling_key
-
+# 自动回复函数self:发送方用户id, uid, msg
     def tuling_auto_reply(self, uid, msg):
     def tuling_auto_reply(self, uid, msg):
         if self.tuling_key:
         if self.tuling_key:
             url = "http://www.tuling123.com/openapi/api"
             url = "http://www.tuling123.com/openapi/api"
@@ -35,7 +35,7 @@ class TulingWXBot(WXBot):
                 result = respond['url']
                 result = respond['url']
             elif respond['code'] == 302000:
             elif respond['code'] == 302000:
                 for k in respond['list']:
                 for k in respond['list']:
-                    result = result + u"【" + k['source'] + u"】 " +\
+                    result = result + u"【" + k['source'] + u"】 " + \
                         k['article'] + "\t" + k['detailurl'] + "\n"
                         k['article'] + "\t" + k['detailurl'] + "\n"
             else:
             else:
                 result = respond['text'].replace('<br>', '  ')
                 result = respond['text'].replace('<br>', '  ')
@@ -43,7 +43,7 @@ class TulingWXBot(WXBot):
             print '    ROBOT:', result
             print '    ROBOT:', result
             return result
             return result
         else:
         else:
-            return u"知道啦"
+            return u"知道啦,我是天问机器人,主人马上就来!"
 
 
     def auto_switch(self, msg):
     def auto_switch(self, msg):
         msg_data = msg['content']['data']
         msg_data = msg['content']['data']
@@ -59,12 +59,13 @@ class TulingWXBot(WXBot):
                 if i == msg_data:
                 if i == msg_data:
                     self.robot_switch = True
                     self.robot_switch = True
                     self.send_msg_by_uid(u'[Robot]' + u'机器人已开启!', msg['to_user_id'])
                     self.send_msg_by_uid(u'[Robot]' + u'机器人已开启!', msg['to_user_id'])
-
+# msg   dict: {'content': {'data': u'\u53bb', 'type': 0}, 'msg_id': u'3808887025959048251', 'msg_type_id': 4, 'to_user_id': u'@97745d23f2d3bbe5241cede14a3d8baa45809a1842e762b5a33fc1353f6efa3a', 'user': {'id': u'@f95a572b1284a0f75dd550a4ed9a93d788ded42a856db023cd3494faab623311', 'name': u'\u5929\u95ee'}}
     def handle_msg_all(self, msg):
     def handle_msg_all(self, msg):
         if not self.robot_switch and msg['msg_type_id'] != 1:
         if not self.robot_switch and msg['msg_type_id'] != 1:
             return
             return
         if msg['msg_type_id'] == 1 and msg['content']['type'] == 0:  # reply to self
         if msg['msg_type_id'] == 1 and msg['content']['type'] == 0:  # reply to self
             self.auto_switch(msg)
             self.auto_switch(msg)
+            # msg_type_id=4 发送文字
         elif msg['msg_type_id'] == 4 and msg['content']['type'] == 0:  # text message from contact
         elif msg['msg_type_id'] == 4 and msg['content']['type'] == 0:  # text message from contact
             self.send_msg_by_uid(self.tuling_auto_reply(msg['user']['id'], msg['content']['data']), msg['user']['id'])
             self.send_msg_by_uid(self.tuling_auto_reply(msg['user']['id'], msg['content']['data']), msg['user']['id'])
         elif msg['msg_type_id'] == 3 and msg['content']['type'] == 0:  # group text message
         elif msg['msg_type_id'] == 3 and msg['content']['type'] == 0:  # group text message
@@ -92,8 +93,15 @@ class TulingWXBot(WXBot):
                     else:
                     else:
                         reply += u"对不起,只认字,其他杂七杂八的我都不认识,,,Ծ‸Ծ,,"
                         reply += u"对不起,只认字,其他杂七杂八的我都不认识,,,Ծ‸Ծ,,"
                     self.send_msg_by_uid(reply, msg['user']['id'])
                     self.send_msg_by_uid(reply, msg['user']['id'])
-
-
+        else:
+            src_name = msg['content']['user']['name']
+            reply = 'to ' + src_name + ': '
+            reply += u"对不起,只认字,其他杂七杂八的我都不认识,,,Ծ‸Ծ,,"
+            self.send_msg_by_uid(reply, msg['user']['id'])
+
+'''
+主函数,执行TulingWXBot类里面的run方法
+'''
 def main():
 def main():
     bot = TulingWXBot()
     bot = TulingWXBot()
     bot.DEBUG = True
     bot.DEBUG = True

+ 2 - 0
conf.ini.bak

@@ -0,0 +1,2 @@
+[main]  
+key:07f207980fe741dc90e71bf3b874f88a

+ 1 - 1
wxbot.py

@@ -1277,7 +1277,7 @@ class WXBot:
             if retcode == '0':
             if retcode == '0':
                 return True
                 return True
         return False
         return False
-
+#服务器轮值,获取新消息
     def sync_check(self):
     def sync_check(self):
         params = {
         params = {
             'r': int(time.time()),
             'r': int(time.time()),