Example #1
0
def onQQMessage(bot, contact, member, content):
    # 当收到 QQ 消息时被调用
    # bot     : QQBot 对象,提供 List/SendTo/GroupXXX/Stop/Restart 等接口,详见文档第五节
    # contact : QContact 对象,消息的发送者
    # member  : QContact 对象,仅当本消息为 群或讨论组 消息时有效,代表实际发消息的成员
    # content : str 对象,消息内容
    INFO('test groups %s', bot.List('group'))
    INFO('bot.conf %s', bot.conf)
    print '========================================================================recive qq message start'
    print 'contact.============.', contact
    print contact.qq, contact.nick
    print '-----------------------------------------------------------------------------'

    if contact.ctype == 'group':
        INFO('群的 QQ.. %s', contact.qq)  # #NULL
        INFO('群的昵称.. %s', contact.nick)  # 嘿哼哈
        INFO('成员的 QQ.. %s', member.qq)  # #NULL
        INFO('成员的昵称.. %s', member.nick)  # /石沫沫
        INFO('最后发言时间.. %s', member.last_speak_time)  # -1
        INFO('消息.. %s', content)  # test内容

        last_speak_time = int(time.time())
        print 'last_speak_time..', last_speak_time
        if content == '':
            INFO('您发了一张图片或假消息... %s', content)
        else:
            sen_value, sen_words = sensitive_check(
                content
            )  # sen_words包含sensitive_words_string:北京&达赖和sensitive_words_dict
            if sen_value != 0:
                sen_flag = 1  #该条信息是敏感信息
            else:
                sen_flag = 0
            # qq_item = {
            #     'xnr_qq_number': bot.session.qq,
            #     'xnr_nickname': bot.session.nick,
            #     'timestamp': member.last_speak_time,
            #     'speaker_qq_number': member.qq,
            #     'text': content,
            #     'sensitive_flag':sen_flag,
            #     'sensitive_value': sen_value,
            #     'sensitive_words_string': sen_words['sensitive_words_string'],
            #     'speaker_nickname': member.nick,
            #     'qq_group_number': contact.qq,
            #     'qq_group_nickname': contact.nick
            # }
            qq_item = {
                'xnr_qq_number': bot.session.qq,
                'xnr_nickname': bot.session.nick,
                'timestamp': last_speak_time,
                'speaker_qq_number': '',
                'text': content,
                'sensitive_flag': sen_flag,
                'sensitive_value': sen_value,
                'sensitive_words_string': sen_words['sensitive_words_string'],
                'speaker_nickname': member.nick,
                'qq_group_number': '',
                'qq_group_nickname': contact.nick
            }
            qq_json = json.dumps(qq_item)
            print 'qq_json=====:', qq_json
            # 判断该qq群是否在redis的群set中
            #qq_number  = qq_item['xnr_qq_number']
            #qq_group_number = qq_item['qq_group_number']

            # r_qq_group_set = r_qq_group_set_pre + qq_number
            # qq_group_set = r.smembers(r_qq_group_set)
            #test
            #qq_group_set = set(['531811289'])

            #if qq_group_number in qq_group_set:

            conMD5 = string_md5(content)

            nowDate = datetime.datetime.now().strftime('%Y-%m-%d')
            index_name = group_message_index_name_pre + str(nowDate)
            print 'INDEX NAME-=-------------=-=-'
            print index_name
            #index_id = bot.conf.qq + '_' + contact.qq + '_' + str(member.last_speak_time) + '_' + conMD5
            # 让系统随机分配 _id
            if not es.indices.exists(index=index_name):
                print 'get mapping'
                print group_message_mappings(bot.session.qq, nowDate)
            print 'qq_item.....', qq_item
            print es.index(index=index_name,
                           doc_type=group_message_index_type,
                           body=qq_item)
Example #2
0
 def proc_msg(self, msg):
     group_puid = msg.sender.puid
     if group_puid in self.groups_list:
         msg_type = msg.type
         save_flag = 0
         data = {}
         if msg_type in ['Text', 'Picture', 'Recording']:
             save_flag = 1
             data = {
                 'xnr_id': self.self.puid,
                 'xnr_name': self.self.name,
                 'group_id': group_puid,
                 'group_name': msg.sender.name,
                 'timestamp': msg.raw['CreateTime'],
                 # 'speaker_id': msg.member.puid,
                 'speaker_id': self.load_member_id(msg.member),
                 'speaker_name': msg.member.name,
                 'msg_type': msg_type
             }
             nowDate = datetime.datetime.now().strftime('%Y-%m-%d')
             index_name = wx_group_message_index_name_pre + str(nowDate)
         if msg_type == 'Text':
             text = msg.text
             data['text'] = text
             try:
                 sen_value, sen_words = sensitive_check(text.encode('utf8'))
                 if sen_value != 0:
                     sen_flag = 1  #该条信息是敏感信息
                 else:
                     sen_flag = 0
                 if msg.is_at:
                     at_flag = 1  #被@到
                 else:
                     at_flag = 0
                 data['at_flag'] = at_flag
                 data['sensitive_flag'] = sen_flag
                 data['sensitive_value'] = sen_value
                 data['sensitive_words_string'] = sen_words[
                     'sensitive_words_string']
             except Exception, e:
                 print e
         elif msg_type == 'Picture':
             '''
             #保存到七牛(已弃用,2018-1-2,hanmc)
             try:
                 #save picture
                 filename = str(msg.id) + '.png'
                 filepath = os.path.join(self.data_path, filename)
                 msg.get_file(filepath)
                 #upload picture to qiniu.com
                 token = self.qiniu.upload_token(qiniu_bucket_name, filename, 3600)
                 ret, info = put_file(token, filename, filepath,)
                 data['text'] = qiniu_bucket_domain + '/' + filename 
                 os.remove(filepath)
             except Exception,e:
                 print e
             '''
             #保存到本地
             filename = str(msg.id) + '.png'
             filepath = os.path.join(WX_IMAGE_ABS_PATH,
                                     ts2datetime(time.time()))
             if not os.path.isdir(filepath):
                 os.mkdir(filepath)
                 remove_wx_media_old_files(WX_IMAGE_ABS_PATH, period=30)
             print msg.get_file(save_path=os.path.join(filepath, filename))
             data['text'] = os.path.join(filepath, filename)
         elif msg_type == 'Recording':
             filename = str(msg.id) + '.mp3'
             filepath = os.path.join(WX_VOICE_ABS_PATH,
                                     ts2datetime(time.time()))
             if not os.path.isdir(filepath):
                 os.mkdir(filepath)
                 remove_wx_media_old_files(WX_VOICE_ABS_PATH, period=30)
             print msg.get_file(save_path=os.path.join(filepath, filename))
             data['text'] = os.path.join(filepath, filename)
         #存储msg到es中
         if save_flag:
             if not es_xnr.indices.exists(index=index_name):
                 print 'get mapping'
                 print wx_group_message_mappings(index_name)
             es_xnr.index(index=index_name,
                          doc_type=wx_group_message_index_type,
                          body=data)
         #自动回复监听的群组中@自己的消息
         if msg.is_at:
             time.sleep(random.random())
             m = msg.reply(u'知道啦~')
             self.save_sent_msg(m=m,
                                to_puid=msg.sender.puid,
                                to_name=msg.sender.name)