Beispiel #1
0
def send_message():
    data = request.get_json()
    type = data['type']
    ids = data['ids']
    group_id = data['gid']
    files = data['files']
    if type == 'group':
        send_type = data['send_type']
        groups = current_bot.groups()
        if send_type == 'contact':
            group = groups.search(puid=group_id)
            if not group:
                raise ApiException(errors.not_found)
            group = group[0]
            users = group.members
        else:
            users = sum([groups.search(puid=id) for id in ids], [])
    else:
        users = current_bot.friends()
    users = [u for u in users if u.puid in ids]
    for user in users:
        user.send_msg(content)
        for filename in files:
            suffix = filename.partition('.')[-1]
            file = os.path.join(config.UPLOAD_FOLDER, filename)
            if suffix in config.PIC_TYPES:
                user.send_image(file)
            else:
                user.send_file(file)
    unexpected = ids.difference(set([u.id for u in users]))
    if unexpected:
        raise ApiException(errors.not_found,
                           '如下puid用户未找到: {}'.format(','.join(unexpected)))
    return {}
Beispiel #2
0
 def delete(self, id):
     type = request.args.get('type')
     if type == 'contact':
         raise ApiException(errors.unimplemented_error, 'ItChat不支持删除好友')
     elif type == 'group':
         group_id = request.args.get('gid')
         group = current_bot.groups().search(puid=group_id)
         if not group:
             raise ApiException(errors.not_found)
         group = group[0]
         group.remove_members(group.members.search(puid=id))
         return {}
Beispiel #3
0
 def put(self):
     verify_content = request.args.get('verifyContent', '')
     ids = set(request.args.getlist('wxid[]'))
     users = [u for u in sum([g.members for g in current_bot.groups()], [])
              if u.puid in ids]
     if users is None:
         raise ApiException(errors.not_found)
     for user in users:
         current_bot.add_friend(user, verify_content)
     unexpected = ids.difference(set([u.id for u in users]))
     if unexpected:
         raise ApiException(
             errors.not_found,
             '如下puid用户未找到: {}'.format(','.join(unexpected)))
     return {}
Beispiel #4
0
def all_groups():
    uid = current_bot.self.puid
    user = db.session.query(User).filter_by(id=uid).first()
    if not user:
        raise ApiException(errors.not_found)
    groups = [group.to_dict() for group in user.groups]
    return {'groups': groups}
Beispiel #5
0
 def put(self, id):
     verify_content = request.args.get('verifyContent', '')
     user = next((u for u in sum(
         [g.members for g in current_bot.groups()], [])
         if u.puid == id), None)
     if user is None:
         raise ApiException(errors.not_found)
     current_bot.add_friend(user, verify_content)
     return {}