def test_add_filter(): import werobot.testing import re robot = WeRoBot() def test_register(): return "test" robot.add_filter(test_register, ["test", re.compile(u".*?啦.*?")]) tester = werobot.testing.WeTest(robot) assert tester.send_xml(_make_xml("test"))._args["content"] == "test" assert tester.send_xml(_make_xml(u"我要测试啦"))._args["content"] == "test" assert tester.send_xml(_make_xml(u"我要测试")) is None with pytest.raises(ValueError) as e: robot.add_filter("test", ["test"]) assert e.value.args[0] == "test is not callable" with pytest.raises(ValueError) as e: robot.add_filter(test_register, "test") assert e.value.args[0] == "test is not list" with pytest.raises(TypeError) as e: robot.add_filter(test_register, [["bazinga"]]) assert e.value.args[0] == "[\'bazinga\'] is not a valid rule"
'tag': r['tag'] }) @myrobot.voice def voicedispatch(message): keyword = message.recognition[:-1] if (keyword == u"简介"): return about(message) elif (keyword == u"统计"): return count(message) elif (keyword == u"帮助"): return help(message) elif (keyword == u"魔法"): return magic(message) elif (keyword == u"结束留言"): return over(message) elif (keyword == u"我要留言"): return wish(message) else: return article(message) myrobot.add_filter(func=about, rules=["A", "a", "about"]) myrobot.add_filter(func=count, rules=["C", "c", "count"]) myrobot.add_filter(func=help, rules=["H", "h", "help", "Y", "y", "yunerself"]) myrobot.add_filter(func=magic, rules=["M", "m", "magic"]) myrobot.add_filter(func=over, rules=["O", "o", "over"]) myrobot.add_filter(func=wish, rules=["W", "w", "wish"]) myrobot.add_filter(func=article, rules=[re.compile(u"[A-Za-z]|[0-9]|[\u0000-\uFFFF]")])