Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1from sanic.response import text, json 

2 

3 

4def get_intent_examples(plugin_config, intent_id): 

5 for intent in plugin_config.get('intents', []): 

6 if intent['intent_id'] == intent_id: 

7 return intent['examples'] 

8 

9 return [] 

10 

11 

12def routes(app, responder, plugin_configs): 

13 @app.route("/", methods=['GET', 'OPTIONS']) 

14 async def hello(request): 

15 return text("Hi, there! I think you might be in the wrong place... Bob.") 

16 

17 async def handle_query(message, sender): 

18 if not message or not sender: 

19 return json({ 

20 "error": "Both a 'message' and a 'sender' must be provided." 

21 }) 

22 

23 if not message.isprintable(): 

24 return json({ 

25 "error": "The message must contain printable characters." 

26 }) 

27 

28 if not sender.isprintable(): 

29 return json({ 

30 "error": "The sender must contain printable characters." 

31 }) 

32 

33 return json({ 

34 "query": message, 

35 "messages": [ 

36 response async for response in responder.handle(message, sender)] 

37 }) 

38 

39 @app.route("/query", methods=['GET']) 

40 async def query_get(request): 

41 message = request.args.get('message') 

42 sender = request.args.get('sender') 

43 

44 return await handle_query(message, sender) 

45 

46 @app.route("/query", methods=['POST', 'OPTIONS']) 

47 async def query(request): 

48 message = request.form.get('message') 

49 sender = request.form.get('sender') 

50 

51 return await handle_query(message, sender) 

52 

53 @app.route("/skills", methods=['GET', 'OPTIONS']) 

54 async def skills(request): 

55 return json({ 

56 'plugins': [ 

57 { 

58 'plugin': plugin_config.get('plugin', 'miscellaneous'), 

59 'description': plugin_config.get('description', ''), 

60 'author': plugin_config.get('author', ''), 

61 'icon': plugin_config.get('icon', '') 

62 } 

63 for plugin_config in plugin_configs], 

64 'skills': [ 

65 { 

66 'plugin': plugin_config.get('plugin', 'miscellaneous'), 

67 'category': skill.get('category', 'miscellaneous'), 

68 'description': skill.get('description', ''), 

69 'examples': get_intent_examples(plugin_config, skill['intent']) 

70 } 

71 for plugin_config in plugin_configs 

72 for skill in plugin_config['skills'] 

73 if 'description' in skill and skill['intent'] != 'nlu_fallback' 

74 ] 

75 })