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 {}
def send_msg(m): # wxpy还不支持未命名的群聊消息 # 先忽略腾讯新闻之类发的信息 if m.receiver.name is None or m.sender is None: return msg_type = TYPE_TO_ID_MAP.get(m.type, 0) if isinstance(m.sender, Group): sender_id = m.member.puid group_id = m.chat.puid elif isinstance(m.sender, _MP): sender_id = m.sender.puid group_id = 0 msg_type = TYPE_TO_ID_MAP.get('MP') else: sender_id = m.sender.puid group_id = 0 receiver_id = m.receiver.puid from views.api import json_api as app with app.app_context(): msg = Message.create(sender_id=sender_id, receiver_id=receiver_id, content=m.text, url=m.url, type=msg_type, receive_time=m.receive_time, group_id=group_id) if m.type in (PICTURE, RECORDING, ATTACHMENT, VIDEO): _, ext = os.path.splitext(m.file_name) m.get_file(os.path.join(UPLOAD_PATH, '{}{}'.format(msg.id, ext))) msg.file_ext = ext db.session.commit() Notification.add(receiver_id, msg.id) if isinstance(m.sender, _MP): for mp_id, ids in settings.mp_forward: if m.sender.puid == mp_id: groups = map(lambda x: bot.groups().search(puid=x)[0], ids) sync_message_in_groups(m, groups) return
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 {}
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 {}
def all_users(): all_ids = set([u.puid for u in sum( [g.members for g in current_bot.groups()], [])]) friend_ids = set([u.puid for u in current_bot.friends()]) ids = all_ids.difference(friend_ids) users = [u.to_dict() for u in db.session.query(User).filter( User.id.in_(ids)).all()] return {'users': users}
def all_users(): all_ids = set([u.puid for u in sum( [g.members for g in current_bot.groups()], [])]) friend_ids = set([u.puid for u in current_bot.friends()]) # ids = all_ids.difference(friend_ids) ids = [u.id for u in db.session.query(User).all()] users = [u.to_dict() for u in db.session.query(User).filter( User.id.in_(ids)).all()] return {'users': users}
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 {}
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 {}
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 {}
def invite(user): groups = sorted(bot.groups(update=True).search('Python'), key=lambda x: x.name) if len(groups) > 0: for group in groups: if len(group.members) == 500: continue if user in group: content = "您已经加入了{} [微笑]".format(group.nick_name) user.send(content) else: group.add_members(user, use_invitation=True) return else: next_topic = group_tmpl.format(re.search(r'\d+', s).group() + 1) new_group = bot.create_group(get_creators(), topic=next_topic) else: print('Invite Failed')
def invite(user, pattern): groups = sorted(bot.groups(update=True).search(pattern), key=lambda x: x.name) if len(groups) > 0: for group in groups: if len(group.members) == GROUP_MEMBERS_LIMIT: continue if user in group: content = "您已经加入了{} [微笑]".format(group.nick_name) user.send(content) else: group.add_members(user, use_invitation=True) return else: next_topic = settings.pattern_map[pattern].format( re.search(r'\d+', s).group() + 1) new_group = bot.create_group(get_creators(), topic=next_topic) new_group.add_members(user, use_invitation=True) new_group.send_msg('创建 [{}] 成功'.format(next_topic)) else: next_topic = settings.pattern_map[pattern].format(1) new_group = bot.create_group(get_creators(), topic=next_topic) new_group.add_members(user, use_invitation=True) new_group.send_msg('创建 [{}] 成功'.format(next_topic))
uid = bot.self.puid settings = SettingWrapper(uid) new_member_regex = re.compile(r'^"(.+)"通过|邀请"(.+)"加入') kick_member_regex = re.compile(r'^(移出|移除|踢出|T)(\s*)@(.+?)(?:\u2005?\s*$)') all_types = [ k.capitalize() for k in dir(consts) if k.isupper() and k != 'SYSTEM' ] here = os.path.abspath(os.path.dirname(__file__)) UPLOAD_PATH = os.path.join(here, '../static/img/uploads') if not os.path.exists(UPLOAD_PATH): os.mkdir(UPLOAD_PATH) KICK_KEY = 'kick:members' KICK_SENDER_KEY = 'kick:senders' groups = [g for g in bot.groups() if g.is_owner] def get_creators(): creator_ids = settings.creators try: creators = list( map(lambda x: bot.friends().search(puid=x)[0], creator_ids)) except IndexError: from views.api import json_api with json_api.app_context(): users = [ u.to_dict() for u in db.session.query(User).filter( User.id.in_(creator_ids)).all() ] creators = list(
from libs.consts import * from libs.globals import current_bot as bot from models.admin import GroupSettings from models.messaging import Message, Notification, db uid = bot.self.puid settings = GroupSettings.get(uid) new_member_regex = re.compile(r'^"(.+)"通过|邀请"(.+)"加入|') all_types = [k.capitalize() for k in dir(consts) if k.isupper() and k != 'SYSTEM'] here = os.path.abspath(os.path.dirname(__file__)) UPLOAD_PATH = os.path.join(here, '../static/img/uploads') if not os.path.exists(UPLOAD_PATH): os.mkdir(UPLOAD_PATH) groups = [g for g in bot.groups() if g.owner.puid == uid] def get_creators(): creator_ids = settings.creators try: creators = map(lambda x: bot.friends().search(puid=x)[0], creator_ids) except IndexError: users = [u.to_dict() for u in db.session.query(User).filter( User.id.in_(creator_ids)).all()] creators = map(lambda x: bot.friends().search( u['nick_name'], Sex=u['sex'], Signature=u['signature'])[0], users) return list(creators)
uid = bot.self.puid settings = SettingWrapper(uid) new_member_regex = re.compile(r'^"(.+)"通过|邀请"(.+)"加入') kick_member_regex = re.compile(r'^(移出|移除|踢出|T)(\s*)@(.+?)(?:\u2005?\s*$)') all_types = [k.capitalize() for k in dir(consts) if k.isupper() and k != 'SYSTEM'] here = os.path.abspath(os.path.dirname(__file__)) UPLOAD_PATH = os.path.join(here, '../static/img/uploads') if not os.path.exists(UPLOAD_PATH): os.mkdir(UPLOAD_PATH) KICK_KEY = 'kick:members' KICK_SENDER_KEY = 'kick:senders' groups = [g for g in bot.groups() if g.owner.puid == uid] def get_creators(): creator_ids = settings.creators try: creators = list(map(lambda x: bot.friends().search(puid=x)[0], creator_ids)) except IndexError: from views.api import json_api with json_api.app_context(): users = [u.to_dict() for u in db.session.query(User).filter( User.id.in_(creator_ids)).all()] creators = list(map(lambda u: bot.friends().search( u['nick_name'], Sex=u['sex'], Signature=u['signature'])[0], users))