def post(self): try: # POSTed data as json j = request.get_json(force=True) # Response payload resp = {'reply': '', 'at_sender': False} suffix = cfg.suffix if 'message' in j: j['message'] = html.unescape(j['message']) # QQ Requests if j['post_type'] == 'request': if (j['request_type'] == 'group' and j['sub_type'] == 'invite') or j['request_type'] == 'friend': resp = {'approve': cfg.approve} return resp, 200 # Logs log_msg = True if log_msg and j['post_type'] == 'message': if j['message_type'] == 'group': misc.msg_log(j['message_id'], j['group_id'], j['sender']['user_id'], j['message']) else: misc.msg_log(j['message_id'], '0', j['sender']['user_id'], j['message']) # All queries from banned senders and bot themselves are directly # dropped banned_sender = cfg.banned_sender if j['sender']['user_id'] == j['self_id'] or j['sender'][ 'user_id'] in banned_sender: return '', 204 # CQ tag for @sender if message type is group title = '' if hasattr(cfg, 'custom_title') and j['message_type'] == 'group': if j['sender']['user_id'] in cfg.custom_title: if j['group_id'] in cfg.custom_title[j['sender'] ['user_id']]: title = cfg.custom_title[j['sender']['user_id']][ j['group_id']] elif 'default' in cfg.custom_title[j['sender']['user_id']]: title = cfg.custom_title[j['sender'] ['user_id']]['default'] elif 'default' in cfg.custom_title: title = cfg.custom_title['default'] at_sender = '{}[CQ:at,qq={}]:\n'.format( title, j['sender']['user_id']) if j['message_type'] == 'group' else '' # Determine which keyword got called matched_keyword = None for keyword, function in bot_command.items(): # Is it a regex? if function[5] == C.REGEX: match = re.match(keyword, j['message']) if match: matched_keyword = keyword break # Is it a full match? elif function[4] == C.NO_MSG: if j['message'] == keyword: matched_keyword = keyword break # Is it a partial match? elif j['message'].startswith(keyword): matched_keyword = keyword break # Check filter if matched_keyword is not None: if j['message_type'] == 'group': if bot_command[matched_keyword][1] == C.BLACKLIST: if j['group_id'] in bot_command[matched_keyword][2]: matched_keyword = None else: if j['group_id'] not in bot_command[matched_keyword][ 2]: matched_keyword = None # If we got a keyword if matched_keyword is not None: # Check cooldown if matched_keyword in stats: if time.time() - stats[matched_keyword] < bot_command[ matched_keyword][3]: resp['reply'] = at_sender + internal.cooldown() return resp, 200 else: stats[matched_keyword] = time.time() else: stats[matched_keyword] = time.time() # Handle command j['keyword'] = matched_keyword try: if bot_command[matched_keyword][4] == C.NO_MSG: msg = bot_command[matched_keyword][0]() else: msg = bot_command[matched_keyword][0](j) except BaseException: msg = '' if msg != '': # Is keyword suppressed? if bot_command[matched_keyword][6] == C.SUPPRESSED: resp['reply'] = msg elif bot_command[matched_keyword][4] == C.NO_MSG: resp['reply'] = msg + suffix else: resp['reply'] = at_sender + msg return resp, 200 # Custom replies if j['message'].lower().replace(' ', '') in wf.data_dict['CR']: resp['reply'] = wf.data_dict['CR'][ j['message'].lower().replace(' ', '')] return resp, 200 # Autoban try: if j['message_type'] == 'group': ban_word = cfg.ban_word for word in ban_word: if word in j['message'].replace(' ', ''): resp['ban'] = True resp['ban_duration'] = cfg.ban_duration return resp, 200 except BaseException: pass # Trivia try: if j['message_type'] == 'group': resp['reply'] = trivia.triviabot(j) if resp['reply'] != '': return resp, 200 except BaseException: pass try: if j['message'].lower().strip() == trivia.curr[ j['group_id']]['answer'].lower().strip(): return '', 204 except BaseException: pass # "Execute" person nobody cared about within 120 seconds # The Nature of Humanity, will override Execution if j['message_type'] == 'group': resp['reply'] = misc.msg_executioner( j) if misc.msg_executioner( j) != '' else misc.msg_nature_of_humanity(j) if resp['reply'] != '': return resp, 200 else: return '', 204 except Exception as e: print(repr(e)) return '', 204
def post(self): try: # POSTed data as json j = request.get_json(force=True) # Response payload resp = {'reply': '', 'at_sender': 'false'} suffix = '\n更多命令请输入"帮助"。' # QQ Requests if j['post_type'] == 'request': if j['request_type'] == 'group': resp = {'approve': 'true'} return resp, 200 # Logs if j['post_type'] == 'message': if j['message_type'] == 'group': misc.msg_log(j['message_id'], j['group_id'], j['sender']['user_id'], j['message']) else: misc.msg_log(j['message_id'], '0', j['sender']['user_id'], j['message']) if j['message_type'] == 'group': if 'card' in j['sender'] and j['sender']['card'] != '': log.info('[%s][%s(%s)]:%s' % (j['group_id'], j['sender']['card'], j['sender']['user_id'], j['message'])) logging.info('[%s][%s(%s)]:%s' % (j['group_id'], j['sender']['card'], j['sender']['user_id'], j['message'])) elif 'nickname' in j[ 'sender'] and j['sender']['nickname'] != '': log.info('[%s][%s(%s)]:%s' % (j['group_id'], j['sender']['nickname'], j['sender']['user_id'], j['message'])) logging.info('[%s][%s(%s)]:%s' % (j['group_id'], j['sender']['nickname'], j['sender']['user_id'], j['message'])) else: log.info( '[%s][%s]:%s' % (j['group_id'], j['sender']['user_id'], j['message'])) logging.info( '[%s][%s]:%s' % (j['group_id'], j['sender']['user_id'], j['message'])) else: log.info('[%s]:%s' % (j['sender']['user_id'], j['message'])) logging.info('[%s]:%s' % (j['sender']['user_id'], j['message'])) # All queries from banned senders are directly dropped banned_sender = [''] if j['sender']['user_id'] == j['self_id'] or str( j['sender']['user_id']) in banned_sender: return '', 204 # CQ tag for @sender if message type is group at_sender = '[CQ:at,qq={}]:\n'.format( j['sender']['user_id']) if j['message_type'] == 'group' else '' # Check query cooldown for cd in command_cooldown_full: if cd in j['message']: if cd in stats: if time.time() - stats[cd] < command_cooldown_full[cd]: resp['reply'] = at_sender + wf.cooldown() return resp, 200 else: stats[cd] = time.time() else: stats[cd] = time.time() for cd in command_cooldown_partial: if j['message'].startswith(cd): if cd in stats: if time.time( ) - stats[cd] < command_cooldown_partial[cd]: resp['reply'] = at_sender + wf.cooldown() return resp, 200 else: stats[cd] = time.time() else: stats[cd] = time.time() # Handle commands for command in command_full: if j['message'] == command: msg = command_full.get(command, lambda: '')() if msg != '': if command in command_suppress: resp['reply'] = msg else: resp['reply'] = msg + suffix return resp, 200 for command in command_partial: if j['message'].startswith(command): msg = command_partial.get(command, lambda: '')(j) if msg != '': if command in command_suppress: resp['reply'] = msg else: resp['reply'] = at_sender + msg return resp, 200 # Custom replies if j['message'].lower().replace(' ', '') in wf.data_dict['CR']: resp['reply'] = wf.data_dict['CR'][ j['message'].lower().replace(' ', '')] return resp, 200 # Autoban if j['message_type'] == 'group': autoban(j['message'], j['group_id'], j['user_id']) resp['reply'] = misc.msg_executioner(j) if resp['reply'] != '': return resp, 200 else: return '', 204 except Exception as e: log.error(repr(e)) return '', 204