def pick_commands(): assert_user_signed_in() checkbox_prefix = 'checkbox_' checked = {} error = None if request.method == 'POST': try: params = get_params(request.form) user = get_current_user() for k in params.iterkeys(): if k.startswith(checkbox_prefix): command = builtin_commands.get(k[len(checkbox_prefix):]) if command != None: checked[command.name] = True for k in sorted(checked.keys()): user.commands.add(builtin_commands.get(k)) user.put() return redirect(COMMANDS) except ParamException as e: error = e.message return render( 'pick_commands.html', builtin_command_groups=builtin_command_groups, checkbox_prefix=checkbox_prefix, checked=checked, error=error, csrf_token=get_csrf_token(), )
def home(): params = get_params(request.args) query = params.get('q') if query == None: return render('home.html', sample_queries=get_sample_queries()) default_command = params.get('default') try: commands = builtin_commands if user_signed_in(): user = get_current_user() commands = user.commands default_command = user.getDefaultCommand() return redirect(commands.getRedirectUrl(query, default_command)) except (NotEnoughArgumentsException, UnknownCommandException, UnknownDefaultCommandException) as e: return render( 'command_error.html', exception_type=e.__class__.__name__, exception=e, ) except MalformedQueryException: return redirect(HOME)
async def chat_content(message): # 聊天的API地址 url = "https://api.ai.qq.com/fcgi-bin/nlp/nlp_textchat" # 获取请求参数 message = message.encode('utf-8') payload = utils.get_params(question=message, session='10000') # r = requests.get(url, params=payload) r = requests.post(url, data=payload) if int(r.json()['ret']) != 0: return "不听不听" return r.json()["data"]["answer"]
def set_default_command(): assert_user_signed_in() params = get_params(request.form) default_command = params.get('default_command') user = get_current_user() user.setDefaultCommand(default_command) user.put() return redirect(COMMANDS)
def test_get_params(self): params = { 'param1': mock_unicode_string('str1'), 'param2': mock_unicode_string('str2'), } out = utils.get_params(params) assert len(out) == 2 assert out['param1'] == 'str1' assert out['param2'] == 'str2' for v in params.values(): v.encode.assert_called_once_with('utf-8')
def delete_command(): assert_user_signed_in() params = get_params(request.form) user = get_current_user() command = user.commands.get(params.get('delete')) if command != None: user.commands.remove(command.name) user.put() return redirect(COMMANDS)
async def translation_content(target_language, message): url = "https://api.ai.qq.com/fcgi-bin/nlp/nlp_texttranslate" language_dict = { "日语": 'jp', "英语": 'en', "韩语": 'kr' } message = message.encode('utf-8') target_language = language_dict[target_language] payload = utils.get_params(target=target_language, source='zh', text=message) r = requests.post(url, data=payload) if int(r.json()['ret']) != 0: return "我不会吖(ㄒoㄒ)" return r.json()["data"]["target_text"]
def edit_command(command_name=None): assert_user_signed_in() if command_name == None: return redirect(NEW_COMMAND_ONLY) user = get_current_user() command = user.commands.get(command_name) if command == None: return redirect(NEW_COMMAND_ONLY + command_name) params = None if request.method == 'POST': # remove command, it might be renamed user.commands.remove(command.name) params = get_params(request.form) try: save_command(**params) return redirect(COMMANDS) except ParamException as e: params['error'] = e.message # add the command back, some other code might use it # before response is finalized user.commands.add(command) else: params = { 'name': command.name, 'description': command.description, 'url_pattern': command.url_pattern, 'default_url': command.default_url, } params['action'] = EDIT_COMMAND_ONLY + command_name params['original_command'] = command_name params['csrf_token'] = get_csrf_token() params['modifiers'] = modifiers return render('new_command.html', **params)
def new_command(command_name=None): assert_user_signed_in() params = {} if command_name != None: user = get_current_user() if user.commands.get(command_name) != None: return redirect(EDIT_COMMAND_ONLY + command_name) params['name'] = command_name if request.method == 'POST': params = get_params(request.form) try: save_command(**params) return redirect(COMMANDS) except ParamException as e: params['error'] = e.message params['action'] = NEW_COMMAND_ONLY params['csrf_token'] = get_csrf_token() params['modifiers'] = modifiers return render('new_command.html', **params)