123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- # -*- coding: utf-8 -*-
- from __future__ import division
- from .__load__ import *
- class Skill(object):
- def init(self, robot):
- # vecan机器人
- self.robot = robot
- self.logger = Log.init(__name__)
- self.plugins = []
- #self.plugins = plugin_loader.get_plugins(self.robot)
- self.handling = False
- def isImmersive(self, plugin, text, parsed):
- return self.robot.getImmersiveMode() == plugin.SLUG and \
- plugin.isValidImmersive(text, parsed)
- def printPlugins(self):
- plugin_list = []
- for plugin in self.plugins:
- plugin_list.append(plugin.SLUG)
- self.logger.info('已激活插件:{}'.format(plugin_list))
- def query(self, text):
- """
- query 模块
- Arguments:
- text -- 用户输入
- """
- args = {
- "service_id": "S13442",
- "api_key": 'w5v7gUV3iPGsGntcM84PtOOM',
- "secret_key": 'KffXwW6E1alcGplcabcNs63Li6GvvnfL'
- }
- parsed = self.robot.doParse(text, **args)
- print parsed
- return parsed
- for plugin in self.plugins:
- if not plugin.isValid(text, parsed) and not self.isImmersive(plugin, text, parsed):
- continue
- self.logger.info("'{}' 命中技能 {}".format(text, plugin.SLUG))
- self.robot.matchPlugin = plugin.SLUG
- if plugin.IS_IMMERSIVE:
- self.robot.setImmersiveMode(plugin.SLUG)
- continueHandle = False
- try:
- self.handling = True
- continueHandle = plugin.handle(text, parsed)
- self.handling = False
- except Exception:
- self.logger.critical('Failed to execute plugin',
- exc_info=True)
- reply = u"抱歉,插件{}出故障了,晚点再试试吧".format(plugin.SLUG)
- self.robot.say(reply, plugin=plugin.SLUG)
- else:
- self.logger.debug("Handling of phrase '%s' by " +
- "plugin '%s' completed", text,
- plugin.SLUG)
- finally:
- if not continueHandle:
- return True
- self.logger.debug("No plugin was able to handle phrase {} ".format(text))
- return False
- def restore(self):
- """ 恢复某个技能的处理 """
- if not self.robot.immersiveMode:
- return
- for plugin in self.plugins:
- if plugin.SLUG == self.robot.immersiveMode and plugin.restore:
- plugin.restore()
- def pause(self):
- """ 暂停某个技能的处理 """
- if not self.robot.immersiveMode:
- return
- for plugin in self.plugins:
- if plugin.SLUG == self.robot.immersiveMode and plugin.pause:
- plugin.pause()
- def understand(self, fp):
- if self.robot and self.robot.tool['asr']:
- return self.robot.tool['asr'].asr(fp)
- return None
- def say(self, msg, cache=False):
- if self.robot and self.robot.tool['tts']:
- self.robot.tool['tts'].tts(msg)
|