def get_msg_from_info(self, msg_from):
        if msg_from == self.seller_id:
            filter_dict = {CONST.SELLER_ID: msg_from}
            msg_from_info = mongo_op.find_one(self.seller_coll,
                                              filter_dict) or {}
        else:
            filter_dict = {CONST.BUYER_ID: msg_from}
            msg_from_info = mongo_op.find_one(self.buyer_coll,
                                              filter_dict) or {}

        msg_from_info[CONST.NAME] = msg_from_info.get(CONST.NAME, msg_from)
        msg_from_info[CONST.EMAIL] = msg_from_info.get(CONST.EMAIL, msg_from)
        return msg_from_info
Ejemplo n.º 2
0
    def get(self):
        seller_id = self.get_argument(CONST.SELLER_ID, default='')
        keyword = self.get_argument(CONST.KEYWORD, default='')
        filter_dict = {
            CONST.SELLER_ID: seller_id
        }
        if keyword:
            filter_dict['$or'] = [
                {CONST.NAME: {'$regex': keyword, '$options': 'i'}},
                {CONST.EMAIL: {'$regex': keyword, '$options': 'i'}}
            ]
        projection = {
            CONST.ID: 0,
        }
        cursor = mongo_op.find(self.buyer_coll, filter_dict, projection=projection)
        count = mongo_op.count_documents(self.buyer_coll, filter_dict)
        buyer_list = []
        for buyer in cursor:
            buyer_temp = deepcopy(buyer)
            # 避免数据库存了 None 的情况
            buyer_id = buyer.get(CONST.BUYER_ID, '')
            chat_room_key = join_key(seller_id, buyer_id)
            filter_dict = {
                CONST.CHAT_ROOM_KEY: chat_room_key
            }
            last_msg = mongo_op.find_one(
                self.chat_msg_record_coll,
                filter_dict,
                sort=[(CONST.ID, -1)]
            ) or {}
            buyer_temp[CONST.LAST_MSG] = last_msg.get(CONST.MSG_CONTENT)
            buyer_temp[CONST.LAST_TIME] = last_msg.get(CONST.SEND_TIME)
            buyer_list.append(buyer_temp)

        self.success(**{CONST.BUYER_LIST: buyer_list, CONST.SELLER_ID: seller_id, CONST.COUNT: count})
Ejemplo n.º 3
0
 def post(self):
     param = self.request.body.decode('utf8')
     json_dict = tornado.escape.json_decode(param)
     seller_id = json_dict.get(CONST.SELLER_ID)
     seller = mongo_op.find_one(self.seller_coll, {CONST.SELLER_ID: seller_id})
     if not seller:
         mongo_op.insert_one(self.seller_coll, json_dict)
     self.success()
Ejemplo n.º 4
0
 def get_buyer(self, buyer_id, seller_id):
     filter_dict = {
         CONST.BUYER_ID: buyer_id,
         CONST.SELLER_ID: seller_id
     }
     projection = {
         CONST.ID: False
     }
     return mongo_op.find_one(self.buyer_coll, filter_dict, projection)
 def copy_flow_to_status(self, keyword='unittest'):
     filter_dict = {
         '{}.{}'.format(CONST.KEYWORD_LIST, CONST.KEYWORD): keyword
     }
     flow = mongo_op.find_one(self.auto_flow_coll, filter_dict)
     if flow:
         flow[CONST.BUYER_ID] = self.buyer_id
         flow[CONST.STATUS] = 'processing'
         flow[CONST.KEYWORD] = keyword
         flow['origin_flow_id'] = str(flow.pop(CONST.ID, ''))
         for action in flow.get(CONST.ACTION_LIST, []):
             if action.get('action_type') == 'flow_start':
                 flow['current_action_id'] = action.get('action_id')
         old_action_list = flow['action_list']
         flow['action_list'] = {i['action_id']: i for i in old_action_list}
         print(flow)
         mongo_op.insert_one(self.auto_flow_coll_status, flow)
         return flow
 def is_new_user(self, seller_id, buyer_id):
     chat_room_key = self.get_chat_room_key(seller_id, buyer_id)
     result = mongo_op.find_one(self.chat_msg_record_coll,
                                {CONST.CHAT_ROOM_KEY: chat_room_key})
     return True if not result else False
    def check_robot_reply(self, msg):
        logger.info(
            'ready check_robot_reply, seller_id:{}, buyer_id:{}'.format(
                self.seller_id, self.buyer_id))
        if msg[CONST.MSG_FROM] != self.buyer_id:
            return
        filter_dict = {
            CONST.BUYER_ID: self.buyer_id,
            # CONST.SELLER_ID: self.seller_id,
            CONST.STATUS: 'processing',
        }
        flow = mongo_op.find_one(self.auto_flow_coll_status,
                                 filter_dict,
                                 sort=[(CONST.ID, -1)])
        if not flow:
            logger.info("Can't find flow")
            return
        keyword = msg[CONST.MSG_CONTENT]
        action_dict = flow[CONST.ACTION_LIST]
        action = action_dict[flow['current_action_id']]
        action_data = action['action_data']
        action = action_dict[action['next_id_list'][0]]
        # 判断关键词
        # if action_data.get('is_reply') == 'T':
        #     for i in action_data.get('short_reply', []):
        #         if i[CONST.KEYWORD] == keyword:
        #             action = action_dict[i['action_id']]
        #             break
        #     # for...else... 表示 for循环正常执行,没有 Break 才会执行的语句
        #     else:
        #         return

        while True:
            # 先判断关键词
            action_data = action['action_data']
            logger.info(action_data)
            if action['action_type'] == 'send_img':
                robot_msg = self.build_msg(self.seller_id,
                                           self.buyer_id,
                                           [{
                                               "url": action_data['file_url']
                                           }],
                                           msg_type='image')
                self.reply_by_robot(robot_msg)

            elif action['action_type'] == 'send_msg':
                if action_data['is_reply'] == 'T':
                    robot_msg = self.build_msg(
                        self.seller_id,
                        self.buyer_id,
                        action_data['message'],
                        msg_type='reply',
                        reply_content=[
                            i[CONST.KEYWORD]
                            for i in action_data['short_reply']
                        ])
                else:
                    robot_msg = self.build_msg(self.seller_id, self.buyer_id,
                                               action_data['message'])
                self.reply_by_robot(robot_msg)

            if action_data.get('send_continue',
                               'T') == 'T' and action['next_id_list']:
                if action_data.get('is_reply') == 'T':
                    for i in action_data.get('short_reply'):
                        if i[CONST.KEYWORD] == keyword:
                            action = action_dict[i['action_id']]
                else:
                    action = action_dict[action['next_id_list'][0]]
            else:
                update_dict = {'current_action_id': action['action_id']}
                if not action['next_id_list']:
                    update_dict['status'] = 'done'
                mongo_op.update_one(self.auto_flow_coll_status, filter_dict,
                                    update_dict)
                break
    def check_robot_reply(self, msg):
        if msg[CONST.MSG_FROM] != self.buyer_id:
            return
        filter_dict = {
            CONST.BUYER_ID: self.buyer_id,
            CONST.SELLER_ID: self.seller_id,
            CONST.STATUS: 'processing',
        }
        flow = mongo_op.find_one(self.auto_flow_coll_status, filter_dict)
        if not flow:
            return
        keyword = msg[CONST.MSG_CONTENT]
        action_dict = flow[CONST.ACTION_LIST]
        action = action_dict[flow['current_action_id']]
        action_data = action['action_data']

        # 判断关键词
        if action_data.get('is_reply') == 'T':
            for i in action_data.get('short_reply', []):
                if i[CONST.KEYWORD] == keyword:
                    action = action_dict[i['action_id']]
                    break
            else:
                return

        while True:
            # 先判断关键词
            action_data = action['action_data']
            # if action_data.get('is_reply') == 'T':
            #     if keyword not in [i[CONST.KEYWORD] for i in action_data['short_reply']]:
            #         if action['action_type'] != 'flow_start':
            #             break
            if action['action_type'] == 'send_img':
                robot_msg = self.build_msg(self.seller_id, self.buyer_id, action_data['file_url'], msg_type='image')
                self.reply_by_robot(robot_msg)

            elif action['action_type'] == 'send_msg':
                if action_data['is_reply'] == 'T':
                    robot_msg = self.build_msg(self.seller_id,
                                               self.buyer_id,
                                               action_data['message'],
                                               msg_type='reply',
                                               reply_content=[i[CONST.KEYWORD] for i in action_data['short_reply']]
                                               )
                else:
                    robot_msg = self.build_msg(self.seller_id,
                                               self.buyer_id,
                                               action_data['message'])
                self.reply_by_robot(robot_msg)

            if action_data.get('send_continue', 'T') == 'T' and action['next_id_list']:
                if action_data.get('is_reply') == 'T':
                    for i in action_data.get('short_reply'):
                        if i[CONST.KEYWORD] == keyword:
                            action = action_dict[i['action_id']]
                else:
                    action = action_dict[action['next_id_list'][0]]
            else:
                update_dict = {
                    'current_action_id': action['action_id']
                }
                if not action['next_id_list']:
                    update_dict['status'] = 'done'
                mongo_op.update_one(self.auto_flow_coll_status, filter_dict, update_dict)
                break