def test_custom_action_handler(home_assistant_nlp): """Test Application.custom_action handle""" app = Application("home_assistant") app.lazy_init(home_assistant_nlp) app.custom_action_config = {"url": "some-url"} app.custom_action(intent="set_thermostat", action="set-thermostat") app.custom_action(default=True, action="times-and-dates") with patch("requests.post") as mock_object: mock_object.return_value = Mock() mock_object.return_value.status_code = 200 mock_object.return_value.json.return_value = { "directives": [{ "payload": "set-thermostat-action" }] } # invoke set thermostat intent res = app.app_manager.parse("turn it to 70 degrees") assert res.directives == [{"payload": "set-thermostat-action"}] assert mock_object.call_args[1]["url"] == "some-url" assert mock_object.call_args[1]["json"]["action"] == "set-thermostat" mock_object.return_value.json.return_value = { "directives": [{ "payload": "time-and-dates-action" }] } # invoke time & dates intent res = app.app_manager.parse("change my alarm to 9") assert res.directives == [{"payload": "time-and-dates-action"}] assert mock_object.call_args[1]["url"] == "some-url" assert mock_object.call_args[1]["json"]["action"] == "times-and-dates"
async def test_custom_action_handler_async(home_assistant_nlp): """Test Application.custom_action handle with async mode""" app = Application("home_assistant", async_mode=True) app.lazy_init(home_assistant_nlp) app.custom_action_config = {"url": "some-url"} app.custom_action(intent="set_thermostat", action="set-thermostat", async_mode=True) app.custom_action(default=True, action="times-and-dates", async_mode=True) with patch("mindmeld.components.CustomAction.post_async") as mock_object: async def mock_coroutine(): return 200, {"directives": ["set-thermostat-action"]} mock_object.return_value = mock_coroutine() # invoke set thermostat intent res = await app.app_manager.parse("turn it to 70 degrees") assert res.directives == ["set-thermostat-action"]
def test_custom_action_sequence(home_assistant_nlp): """Test Application.custom_action handle for a sequence of actions""" app = Application("home_assistant") app.lazy_init(home_assistant_nlp) app.custom_action_config = {"url": "some-url"} app.custom_action( intent="set_thermostat", actions=["set-thermostat", "clear-thermostat"] ) with patch("requests.post") as mock_object: mock_object.return_value = Mock() mock_object.return_value.status_code = 200 mock_object.return_value.json.return_value = {"directives": ["some-directive"]} # invoke set thermostat intent and we should expect two directives res = app.app_manager.parse("turn it to 70 degrees") assert res.directives == ["some-directive", "some-directive"] assert mock_object.call_args[1]["url"] == "some-url"
# -*- coding: utf-8 -*- """This module contains a template MindMeld application""" from mindmeld import Application import datetime as dt import histopy app = Application(__name__) __all__ = ['app'] @app.handle(default=True) def default(request, responder): """This is a default handler.""" responder.reply('Hello there!') @app.handle(intent='greet') def greet(request, responder): responder.reply('Hello, {name}. Ask me about what happend on a day in history.') responder.listen() @app.handle(intent='on_day') def on_day(request, responder): time_stamp = next((e for e in request.entities if e['type'] == 'sys_time'), None) if time_stamp: responder.slots['date'] = time_stamp['value'] date_str = time_stamp['value'][0]['value'] date_str = date_str.split("T")[0] date_obj = dt.datetime.strptime(date_str, "%Y-%m-%d")
# -*- coding: utf-8 -*- """This module contains the Kwik-E-Mart MindMeld demo application""" from mindmeld import Application app = Application(__name__, async_mode=True) @app.handle(intent="greet") async def welcome(request, responder): try: responder.slots["name"] = request.context["name"] prefix = "Hello, {name}. " except KeyError: prefix = "Hello. " responder.reply( prefix + "I can help you find store hours " "for your local Kwik-E-Mart. How can I help?" ) responder.listen() @app.handle(intent="exit") async def say_goodbye(request, responder): responder.reply(["Bye", "Goodbye", "Have a nice day."]) @app.handle(intent="help") async def provide_help(request, responder): prompts = [ "I can help you find store hours for your local Kwik-E-Mart. For example, you can "
# -*- coding: utf-8 -*- """This module contains the MindMeld video discovery blueprint application""" import datetime import logging import random from mindmeld import Application app = Application(__name__) KB_INDEX_NAME = 'videos' GENERAL_REPLIES = ['I can help you find movies and TV shows. What do you feel like watching today?', 'Tell me what you would like to watch today.', 'Talk to me to browse movies and TV shows.'] GENERAL_SUGGESTIONS = [{'text': 'Most popular', 'type': 'text'}, {'text': 'Most recent', 'type': 'text'}, {'text': 'Movies', 'type': 'text'}, {'text': 'TV Shows', 'type': 'text'}, {'text': 'Action', 'type': 'text'}, {'text': 'Dramas', 'type': 'text'}, {'text': 'Sci-Fi', 'type': 'text'}] # Convert timestamps from mallard format to date # Example: '2002-01-01T00:00:00.000-07:00' to '2002-01-01' MALLARD_DATE_SPLIT_INDEX = 10 # Map entity names to field names in knowledge base ENTITY_TO_FIELD = {
def lazy_init(self, nlp=None): Application.lazy_init(self, nlp) self._server = create_skill_server(self.app_manager, self.secret, self.private_key)