Skip to content

Commit

Permalink
Ignore messages if custom json property 'ignore' is set to True.
Browse files Browse the repository at this point in the history
  • Loading branch information
joern19 committed Mar 8, 2024
1 parent 3071a95 commit 5897768
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion rasa_vier_cvg/cvg.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ def __init__(self, callback_base_url: Text, on_message: Callable[[UserMessage],
self.base_url = callback_base_url.rstrip('/')
self.proxy = proxy

# This is a very specific workaround to ignore messages received by this channel.
# When a custom rasa action returns a response, it is sent to the current channel and gets added to the tracker.
# However, returning an Event, it should only get added to the tracker, not sent to the channel. I believe this is a bug.
# If you want to maintain an accurate history in the tracker when using the CVG API directly, we have to send a response or event.
# Now, to prevent sending the message to CVG twice, this flag can be set to true so that channel drops the message.
def _is_ignored(self, custom_json) -> bool:

This comment has been minimized.

Copy link
@pschichtel

pschichtel Mar 8, 2024

Member

I don't like the way the comment is phrased. This is in itself not a workaround, it's a functionality to ignore certain messages. It is used as a workaround for dialog setups that produce messages that should not be forwarded, but still be tracked.

return custom_json is not None and "ignore" in custom_json and custom_json["ignore"] == True

async def _perform_request(self, path: str, method: str, data: Optional[any], dialog_id: Optional[str], retries: int = 0) -> (Optional[int], any):
url = f"{self.base_url}{path}"
try:
Expand Down Expand Up @@ -112,7 +120,10 @@ async def _say(self, dialog_id: str, text: str):
if len(text.strip()) > 0:
await self._perform_request("/call/say", method="POST", data={DIALOG_ID_FIELD: dialog_id, "text": text}, dialog_id=dialog_id)

async def send_text_message(self, recipient_id: Text, text: Text, **kwargs: Any) -> None:
async def send_text_message(self, recipient_id: Text, text: Text, custom, **kwargs: Any) -> None:
if self._is_ignored(custom):
return

reseller_token, project_token, dialog_id = parse_recipient_id(recipient_id)
logger.info(f"{dialog_id} - Sending text to say: {text}")
await self._say(dialog_id, text)
Expand Down Expand Up @@ -208,6 +219,9 @@ async def do_nothing(*args):
logger.info(f"{dialog_id} - Operation {operation_name} complete")

async def send_custom_json(self, recipient_id: Text, json_message: Dict[Text, Any], **kwargs: Any) -> None:
if self._is_ignored(json_message):
return

for operation_name, body in json_message.items():
if operation_name[:len(OPERATION_PREFIX)] == OPERATION_PREFIX:
await asyncio.sleep(0.050)
Expand Down

0 comments on commit 5897768

Please sign in to comment.