Skip to content

Commit

Permalink
Fixed issue (RasaHQ#5657) in MessengerBot.send_custom_json
Browse files Browse the repository at this point in the history
  • Loading branch information
wavymazy committed Mar 22, 2021
1 parent 12ea7a5 commit f515eb2
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 22 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
repos:
- repo: https://github.com/ambv/black
rev: 20.8b1
rev: 19.10b0
hooks:
- id: black
- repo: https://github.com/thlorenz/doctoc
rev: v2.0.0
rev: master
hooks:
- id: doctoc
files: "CONTRIBUTING.md"
14 changes: 2 additions & 12 deletions rasa/core/channels/facebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,18 +279,8 @@ async def send_custom_json(
self, recipient_id: Text, json_message: Dict[Text, Any], **kwargs: Any
) -> None:
"""Sends custom json data to the output."""
if isinstance(json_message, list):
if isinstance(json_message.pop(), list):
recipient_id = json_message.pop().pop() or recipient_id
else:
recipient_id = json_message.pop().pop("id", None) or recipient_id
else:
if isinstance(json_message.pop("sender", {}), list):
recipient_id = json_message.pop("sender", {}).pop("id") or recipient_id
else:
recipient_id = (
json_message.pop("sender", {}).pop("id", None) or recipient_id
)
if isinstance(json_message, dict) and "sender" in json_message.keys():
recipient_id = json_message.pop("sender", {}).pop("id") or recipient_id

self.messenger_client.send(json_message, recipient_id, "RESPONSE")

Expand Down
66 changes: 58 additions & 8 deletions tests/test_facebook.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import logging
from typing import Dict
from unittest.mock import patch, MagicMock
from rasa.core.channels.facebook import MessengerBot
from fbmessenger import MessengerClient

import pytest
from _pytest.monkeypatch import MonkeyPatch
Expand All @@ -27,6 +29,8 @@
logger = logging.getLogger(__name__)

# USED FOR DOCS - don't rename without changing in the docs


def test_facebook_channel():
# START DOC INCLUDE
from rasa.core.channels.facebook import FacebookInput
Expand All @@ -49,13 +53,59 @@ def test_facebook_channel():
assert routes_list["fb_webhook.webhook"].startswith("/webhooks/facebook/webhook")


def test_facebook_send_custon_json_list():
json_with_list = [["example text"]]
json_with_list_else = [{"id": "example text"}]
assert json_with_list.pop().pop() == "example text"
assert json_with_list_else.pop().pop("id", None) == "example text"
async def test_facebook_send_custom_json():
# This function tests cases when the custom json is a list
# The send_custom_json function doesn't return anything. Rather
# it calls an object MessengerClient, that will
# then make a post request.
# Since the purpose is to test the extraction of the recipient_id
# by the MessengerBot.send_custom_json_list we
# modify MessengerClient (from the fbmessenger pypackage) to
# return the recipient ID.

json_without_id = {
"blocks": [
{"type": "title", "text": {"text": "Conversation progress"}},
{
"type": "progression_bar",
"text": {"text": "progression 1", "level": "1"},
},
]
}
json_with_id = {
"blocks": [
{"type": "title", "text": {"text": "Conversation progress"}},
{
"type": "progression_bar",
"text": {"text": "progression 1", "level": "1"},
},
],
"sender": {"id": "test_json_id"},
}

class TestableMessengerClient(MessengerClient):
def __init__(self, page_access_token, **kwargs):
self.recipient_id = ""
super(TestableMessengerClient, self).__init__(page_access_token, **kwargs)

def test_facebook_send_custon_json():
json_without_list = {"sender": {"id": "example text"}}
assert json_without_list.pop("sender", {}).pop("id", None) == "example text"
def send(
self,
payload,
recipient_id,
messaging_type="RESPONSE",
notification_type="REGULAR",
timeout=None,
tag=None,
):
self.recipient_id = recipient_id

messenger_client = TestableMessengerClient(page_access_token="test_token")
messenger_bot = MessengerBot(messenger_client)
await messenger_bot.send_custom_json(
recipient_id="test_id", json_message=json_without_id
)
assert messenger_bot.messenger_client.recipient_id == "test_id"
await messenger_bot.send_custom_json(
recipient_id="test_id", json_message=json_with_id
)
assert messenger_bot.messenger_client.recipient_id == "test_json_id"

0 comments on commit f515eb2

Please sign in to comment.