skill.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # -*- coding: utf-8 -*-
  2. from __future__ import division
  3. from .__load__ import *
  4. class Skill(object):
  5. def init(self, robot):
  6. # vecan机器人
  7. self.robot = robot
  8. self.logger = Log.init(__name__)
  9. self.plugins = []
  10. #self.plugins = plugin_loader.get_plugins(self.robot)
  11. self.handling = False
  12. def isImmersive(self, plugin, text, parsed):
  13. return self.robot.getImmersiveMode() == plugin.SLUG and \
  14. plugin.isValidImmersive(text, parsed)
  15. def printPlugins(self):
  16. plugin_list = []
  17. for plugin in self.plugins:
  18. plugin_list.append(plugin.SLUG)
  19. self.logger.info('已激活插件:{}'.format(plugin_list))
  20. def query(self, text):
  21. """
  22. query 模块
  23. Arguments:
  24. text -- 用户输入
  25. """
  26. args = {
  27. "service_id": "S13442",
  28. "api_key": 'w5v7gUV3iPGsGntcM84PtOOM',
  29. "secret_key": 'KffXwW6E1alcGplcabcNs63Li6GvvnfL'
  30. }
  31. parsed = self.robot.doParse(text, **args)
  32. print parsed
  33. return parsed
  34. for plugin in self.plugins:
  35. if not plugin.isValid(text, parsed) and not self.isImmersive(plugin, text, parsed):
  36. continue
  37. self.logger.info("'{}' 命中技能 {}".format(text, plugin.SLUG))
  38. self.robot.matchPlugin = plugin.SLUG
  39. if plugin.IS_IMMERSIVE:
  40. self.robot.setImmersiveMode(plugin.SLUG)
  41. continueHandle = False
  42. try:
  43. self.handling = True
  44. continueHandle = plugin.handle(text, parsed)
  45. self.handling = False
  46. except Exception:
  47. self.logger.critical('Failed to execute plugin',
  48. exc_info=True)
  49. reply = u"抱歉,插件{}出故障了,晚点再试试吧".format(plugin.SLUG)
  50. self.robot.say(reply, plugin=plugin.SLUG)
  51. else:
  52. self.logger.debug("Handling of phrase '%s' by " +
  53. "plugin '%s' completed", text,
  54. plugin.SLUG)
  55. finally:
  56. if not continueHandle:
  57. return True
  58. self.logger.debug("No plugin was able to handle phrase {} ".format(text))
  59. return False
  60. def restore(self):
  61. """ 恢复某个技能的处理 """
  62. if not self.robot.immersiveMode:
  63. return
  64. for plugin in self.plugins:
  65. if plugin.SLUG == self.robot.immersiveMode and plugin.restore:
  66. plugin.restore()
  67. def pause(self):
  68. """ 暂停某个技能的处理 """
  69. if not self.robot.immersiveMode:
  70. return
  71. for plugin in self.plugins:
  72. if plugin.SLUG == self.robot.immersiveMode and plugin.pause:
  73. plugin.pause()
  74. def understand(self, fp):
  75. if self.robot and self.robot.tool['asr']:
  76. return self.robot.tool['asr'].asr(fp)
  77. return None
  78. def say(self, msg, cache=False):
  79. if self.robot and self.robot.tool['tts']:
  80. self.robot.tool['tts'].tts(msg)