Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async def test_parsing(default_processor: MessageProcessor):
message = UserMessage('/greet{"name": "boy"}')
parsed = await default_processor._parse_message(message)
assert parsed["intent"]["name"] == "greet"
assert parsed["entities"][0]["entity"] == "name"
@pytest.mark.parametrize("default_intent", DEFAULT_INTENTS)
async def test_default_intent_recognized(
default_processor: MessageProcessor, default_intent: Text
):
message = UserMessage(default_intent)
parsed = await default_processor._parse_message(message)
with pytest.warns(None) as record:
default_processor._log_unseen_features(parsed)
assert len(record) == 0
async def test_http_parsing():
message = UserMessage("lunch?")
endpoint = EndpointConfig("https://interpreter.com")
with aioresponses() as mocked:
mocked.post("https://interpreter.com/model/parse", repeat=True, status=200)
inter = RasaNLUHttpInterpreter(endpoint=endpoint)
try:
await MessageProcessor(inter, None, None, None, None)._parse_message(
message
)
except KeyError:
pass # logger looks for intent and entities, so we except
r = latest_request(mocked, "POST", "https://interpreter.com/model/parse")
assert r
async def test_http_parsing():
message = UserMessage("lunch?")
endpoint = EndpointConfig("https://interpreter.com")
with aioresponses() as mocked:
mocked.post("https://interpreter.com/model/parse", repeat=True, status=200)
inter = RasaNLUHttpInterpreter(endpoint=endpoint)
try:
await MessageProcessor(inter, None, None, None, None)._parse_message(
message
)
except KeyError:
pass # logger looks for intent and entities, so we except
r = latest_request(mocked, "POST", "https://interpreter.com/model/parse")
assert r
def test_int_message_id_in_user_message():
from rasa.core.channels.channel import UserMessage
# noinspection PyTypeChecker
message = UserMessage("B text", message_id=987654321)
assert message.message_id == "987654321"
text = output["text"].split(" ", 1)
text = text[1]
sender_id = output["user_id"]
self.bot_channel = output["channel_id"]
try:
out_channel = MattermostBot(
self.url,
self.team,
self.user,
self.pw,
self.bot_channel,
self.webhook_url,
)
user_msg = UserMessage(
text,
out_channel,
sender_id,
input_channel=self.name(),
metadata=metadata,
)
await on_new_message(user_msg)
except Exception as e:
logger.error("Exception when trying to handle message.{0}".format(e))
logger.debug(e, exc_info=True)
input_channel = self._extract_input_channel(request)
metadata = self.get_metadata(request)
if should_use_stream:
return response.stream(
self.stream_response(
on_new_message, text, sender_id, input_channel, metadata
),
content_type="text/event-stream",
)
else:
collector = BotfrontRestOutput()
# noinspection PyBroadException
try:
await on_new_message(
UserMessage(
text,
collector,
sender_id,
input_channel=input_channel,
metadata=metadata,
)
)
except CancelledError:
logger.error(
"Message handling timed out for "
"user message '{}'.".format(text)
)
except Exception:
logger.exception(
"An exception occured while handling "
"user message '{}'.".format(text)
The parsed message.
Example:
{\
"text": '/greet{"name":"Rasa"}',\
"intent": {"name": "greet", "confidence": 1.0},\
"intent_ranking": [{"name": "greet", "confidence": 1.0}],\
"entities": [{"entity": "name", "start": 6,\
"end": 21, "value": "Rasa"}],\
}
"""
processor = self.create_processor()
message = UserMessage(message_data)
return await processor._parse_message(message, tracker)
input_channel=self.name(),
metadata=metadata,
)
)
await on_new_message(
UserMessage(
"/start",
out_channel,
sender_id,
input_channel=self.name(),
metadata=metadata,
)
)
else:
await on_new_message(
UserMessage(
text,
out_channel,
sender_id,
input_channel=self.name(),
metadata=metadata,
)
)
except Exception as e:
logger.error(f"Exception when trying to handle message.{e}")
logger.debug(e, exc_info=True)
if self.debug_mode:
raise
pass
return response.text("success")
async def record_messages(
endpoint: EndpointConfig,
file_importer: TrainingDataImporter,
sender_id: Text = UserMessage.DEFAULT_SENDER_ID,
max_message_limit: Optional[int] = None,
skip_visualization: bool = False,
):
"""Read messages from the command line and print bot responses."""
try:
try:
domain = await retrieve_domain(endpoint)
except ClientError:
logger.exception(
f"Failed to connect to Rasa Core server at '{endpoint.url}'. "
f"Is the server running?"
)
return
intents = [next(iter(i)) for i in (domain.get("intents") or [])]