-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Treat list custom json facebook #9567
Changes from all commits
2cebc45
f69f4d2
728dc91
d2e50c2
9f3bfcc
d8c8b66
aa7b4eb
3966ece
a21deeb
f007a30
fcf7893
9ae8c35
a87ab3d
773e821
0006416
317dc7b
de28d04
0b7485f
cef6c77
4b6e650
1dcfd33
0d05a9d
4993ea1
d1a03d1
ee0ddb2
49174dd
8a642d2
831e3a3
841e6e7
f8b3a28
0214197
895a068
a11a6b2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add list handling in the `send_custom_json` method on `channels/facebook.py`. | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,7 @@ | |
import rasa.shared.utils.io | ||
from sanic import Blueprint, response | ||
from sanic.request import Request | ||
from typing import Text, List, Dict, Any, Callable, Awaitable, Iterable, Optional | ||
from typing import Text, List, Dict, Any, Callable, Awaitable, Iterable, Optional, Union | ||
|
||
from rasa.core.channels.channel import UserMessage, OutputChannel, InputChannel | ||
from sanic.response import HTTPResponse | ||
|
@@ -276,11 +276,14 @@ async def send_elements( | |
self.messenger_client.send(payload, recipient_id, "RESPONSE") | ||
|
||
async def send_custom_json( | ||
self, recipient_id: Text, json_message: Dict[Text, Any], **kwargs: Any | ||
self, | ||
recipient_id: Text, | ||
json_message: Union[List, Dict[Text, Any]], | ||
**kwargs: Any, | ||
) -> None: | ||
"""Sends custom json data to the output.""" | ||
|
||
recipient_id = json_message.pop("sender", {}).pop("id", None) or recipient_id | ||
if isinstance(json_message, dict) and "sender" in json_message.keys(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This also needs a check if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done, except the part regarding |
||
recipient_id = json_message.pop("sender", {}).pop("id", recipient_id) | ||
|
||
self.messenger_client.send(json_message, recipient_id, "RESPONSE") | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import logging | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you please shift this file into the correct subdirectory: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
from rasa.core.channels.facebook import MessengerBot | ||
from fbmessenger import MessengerClient | ||
|
||
from _pytest.monkeypatch import MonkeyPatch | ||
|
||
import rasa.core.run | ||
from rasa.core import utils | ||
|
||
|
||
# this is needed so that the tests included as code examples look better | ||
from tests.utilities import json_of_latest_request, latest_request | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def test_facebook_channel(): | ||
from rasa.core.channels.facebook import FacebookInput | ||
|
||
input_channel = FacebookInput( | ||
fb_verify="YOUR_FB_VERIFY", | ||
# you need tell facebook this token, to confirm your URL | ||
fb_secret="YOUR_FB_SECRET", # your app secret | ||
fb_access_token="YOUR_FB_PAGE_ACCESS_TOKEN" | ||
# token for the page you subscribed to | ||
) | ||
|
||
s = rasa.core.run.configure_app([input_channel], port=5004) | ||
|
||
routes_list = utils.list_routes(s) | ||
|
||
assert routes_list["fb_webhook.health"].startswith("/webhooks/facebook") | ||
assert routes_list["fb_webhook.webhook"].startswith("/webhooks/facebook/webhook") | ||
|
||
|
||
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 = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test should also check that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
"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 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" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this changelog entry should actually be a
bugfix
rather thanimprovement
based on the original issue. It would also be great if you could give more details around the bug since it isn't very clear for me in which circumstances would thejson_message
be of typelist
rather thandict
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok. Done in a new branch
rebase-treat-custom-json-fb
.I haven't pushed it yet, but soon I will and I'll also open the new PR.