def send_message(self, message): """ 发送消息到Chatbot :param message: :return: """ self.clt = client.AcsClient( self.accessKeyId, self.accessKeySecret, load_yaml_settings()['aliyun']['chatbot']['region']) self.request = ChatRequest.ChatRequest() self.request.set_Utterance(message) self.request.set_InstanceId( load_yaml_settings()['aliyun']['chatbot']['instance']) if self.chatbot_session_id is not None: self.request.set_SessionId(self.chatbot_session_id) result_str = self.clt.do_action_with_exception( self.request).decode('utf-8') self._logger.info('返回数据:' + result_str) result_json = json.loads(result_str) if self.chatbot_session_id is None: self.chatbot_session_id = result_json['SessionId'] if len(result_json['Messages']) >= 1: return result_json['Messages'][0]['Text']['Content'] else: return ''
def load_custom_plugins(self): """ 加载自定义的热词模型和插件 :return: """ user_hotwords_dir = os.path.expanduser(load_yaml_settings()['custom']['hotwords']) user_plugins_dir = os.path.expanduser(load_yaml_settings()['custom']['plugins']) if os.path.isdir(user_hotwords_dir) is False: os.makedirs(user_hotwords_dir) if os.path.isdir(user_plugins_dir) is False: os.makedirs(user_plugins_dir) os.sys.path.append(user_plugins_dir)
def get_plugins(cls): """ 动态加载所有的插件,并通过优先级排序。如果插件没有定义优先级则以0看待 """ custom_dir = os.path.expanduser( load_yaml_settings()['custom']['plugins']) locations = [PLUGINS_PATH] if os.path.isdir(custom_dir): locations.append(custom_dir) logger = logging.getLogger() plugins = [] # plugins that are not allow to be call via Wechat or Email logger.debug("Looking for plugins in: %s", ', '.join(["'%s'" % location for location in locations])) for finder, name, ispkg in pkgutil.walk_packages(locations): try: loader = finder.find_module(name) mod = loader.load_module(name) except Exception: logger.warning("Skipped plugin '%s' due to an error.", name, exc_info=True) else: if hasattr(mod, 'WORDS'): logger.debug("Found plugin '%s' with words: %r", name, mod.WORDS) plugins.append(mod) else: logger.warning( "Skipped plugin '%s' because it misses the WORDS constant.", name) plugins.sort(key=lambda mod: mod.PRIORITY if hasattr(mod, 'PRIORITY') else 0, reverse=True) return plugins
def test_connection(self): entity_id = load_yaml_settings()['hass']['entities']['light'] hass = Hass.get_instance() hass.conn() hass.xiaomi_gateway_light(entity_id=entity_id, state='turn_on', rgb_color=[255, 100, 100], brightness=10) time.sleep(0.5) hass.xiaomi_gateway_light(entity_id=entity_id, state='turn_off') hass.xiaomi_print_entities()
def plugin_output(text, mic, robot_says, force_ding=False): """ 插件输出 :param text: :param mic: :param robot_says: :param force_ding: 此参数为True时,返回消息肯定会发送到钉钉 :return: """ if load_yaml_settings()['dingtalk']['enable'] or force_ding: DingRobot.dingtalk_handle(text, robot_says, force_ding) mic.say(robot_says)
def __init__(self, mic, profile, iot_client): self.mic = mic self.profile = profile self._plugin_name_list = [] self.watchdog_event_handler = MyFileSystemEventHander self.plugins = None self._logger = logging.getLogger() self._iot_client = iot_client self.handling = False self.chatbot = Chatbot.get_instance() self._custom_plugin_path = os.path.expanduser(load_yaml_settings()['custom']['plugins']) self.get_plugins()
def plugin_output(text, mic, robot_says, force_ding=False, ding_content=None): """ 插件输出 :param text: :param mic: :param robot_says: :param force_ding: 此参数为True时,返回消息肯定会发送到钉钉 :param ding_content: 钉钉消息内容 :return: """ ding_says = robot_says if load_yaml_settings()['dingtalk']['enable'] or force_ding: if ding_content is not None: ding_says = ding_content DingRobot.dingtalk_handle(text, ding_says, force_ding) mic.say(robot_says)
def handle(text, mic, profile, iot_client=None, chatbot=None): """ 内部实现完整的对话逻辑,对原来的逻辑无侵入 :param text: :param mic: :param profile: :return: """ logger.info('机器人回复') if load_yaml_settings()['aliyun']['chatbot']['enable']: logger.info('云小蜜已开启,通过云小蜜回复') return_text = chatbot.send_message(text) else: return_text = random.choice([ '', ]) mic.say(return_text)
def get_hotword_models(): """ 读取当前有多少个可用模型,返回 """ custom_dir = os.path.expanduser(load_yaml_settings()['custom']['hotwords']) file_list = [] if os.path.exists(custom_dir): file_list = file_list + os.listdir(custom_dir) file_list = file_list + os.listdir(HOTWORD_MODEL_PATH) files = [] for file in file_list: if file.endswith('umdl') or file.endswith('pmdl'): # 限制模型类型 if os.path.isfile(HOTWORD_MODEL_PATH + '/' + file): files.append(HOTWORD_MODEL_PATH + '/' + file) if os.path.isfile(custom_dir + '/' + file): files.append(custom_dir + '/' + file) if len(files) == 0: raise Exception('你至少应该有一个热词模型,请参照README.md') return files
def __init__(self): self.yaml_settings = load_yaml_settings()
# -*- coding: utf-8-*- from src import config yaml_settings = config.load_yaml_settings() """ 保存用户信息 """ myname = yaml_settings['profile']['myname'] city = yaml_settings['profile']['city'] # 请包含"市"、"区"字样,例如:南京市、建邺区 timezone = yaml_settings['profile']['timezone'] """ 子账号需要IOT和语音权限 """ ak_id = yaml_settings['aliyun']['ak_id'] ak_secret = yaml_settings['aliyun']['ak_secret'] ali_appcode = yaml_settings['aliyun']['api_market']['appcode'] """ 阿里云函数计算服务 https://fc.console.aliyun.com/overview/cn-shanghai """ aliyun_fc_endpoint = yaml_settings['aliyun']['fc']['endpoint'] aliyun_fc_service_name = yaml_settings['aliyun']['fc']['service_name'] """ 远端控制服务 """ remote_control_service_enable = yaml_settings['remote_control_service'][ 'enable'] remote_control_password = yaml_settings['remote_control_service']['passwd'] remote_control_api_token = yaml_settings['remote_control_service']['api_token'] """ 表格存储 """