Rasa version: 1.9.6
Python version: Python 3.7.6
Operating system (windows, osx, ...): windows
Issue: The Slack connector is not able to handle the button payload response from the slack.
Error (including full traceback):
File "c:\users\jitesh\appdata\local\programs\python\python37\lib\site-packages\sanic\request.py", line 173, in load_json
self.parsed_json = loads(self.body)
ValueError: Expected object or value
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "c:\users\jitesh\appdata\local\programs\python\python37\lib\site-packages\sanic\app.py", line 976, in handle_request
response = await response
File "c:\users\jitesh\appdata\local\programs\python\python37\lib\site-packages\rasa\core\channels\slack.py", line 356, in webhook
metadata = self.get_metadata(request)
File "c:\users\jitesh\appdata\local\programs\python\python37\lib\site-packages\rasa\core\channels\slack.py", line 329, in get_metadata
slack_event = request.json
File "c:\users\jitesh\appdata\local\programs\python\python37\lib\site-packages\sanic\request.py", line 167, in json
self.load_json()
File "c:\users\jitesh\appdata\local\programs\python\python37\lib\site-packages\sanic\request.py", line 177, in load_json
raise InvalidUsage("Failed when parsing body as json")
sanic.exceptions.InvalidUsage: Failed when parsing body as json
Below is the output of the error in slack:
when I tried to find where the issue lies, I got to know that the request object that was received from slack was in the req.form and the issue occurs when getting the metadata value on the below line:
https://github.com/RasaHQ/rasa/blob/002b892f573541b42a05b783df64424f959ff9e8/rasa/core/channels/slack.py#L356
since the get_metadata function uses req.json to set the values for metadata and req.json isn't there so it throws the above error.
So as a workaround, I tried to update the connector and it worked for me:
@slack_webhook.route("/webhook", methods=["GET", "POST"])
async def webhook(request: Request) -> HTTPResponse:
if request.form:
output = request.form
payload = json.loads(output["payload"][0])
if self._is_interactive_message(payload):
sender_id = payload["user"]["id"]
text = self._get_interactive_response(payload["actions"][0])
if text is not None:
# metadata = self.get_metadata(request)
out_channel = payload["channel"]["id"]
users = payload["user"]["id"]
metadata = {'out_channel': out_channel, 'users': users}
return await self.process_message(
request, on_new_message, text, sender_id, metadata
)
elif payload["actions"][0]["type"] == "button":
# link buttons don't have "value", don't send their clicks to bot
return response.text("User clicked link button")
return response.text(
"The input message could not be processed.", status=500
)
I am getting the values for metadata from the payload.
Below is the output from the slack after updating the connector:
I don't know whether it's a reliable workaround solution馃槄 would like to know your thoughts on this 馃槃
I am also having this same exact issue when processing a Slack button. I'll try and test out the workaround as well to see if it works for me.
I was also facing the same issue with slack, so when I was looking at where the problem lies, I got to know the same thing which @JiteshGaikwad pointed out, as the metadata option was an optional one for the functions thereafter so I commented that line and initialized metadata to an empty dictionary and that seems to work for me. Please look into this matter.
Also, I think if you are initializing the metadata as done for request.json you should do like,
lst = [ ]
lst.append( payload["message"]["user"] )
out_channel = payload["channel"]["id"]
users = lst
should be used, you can see the similarity with the get_metadata function for request.json
@JiteshGaikwad @vipuldarkknight
thanks a lot for reporting the bug! I can also reproduce the exception. Since you already found a fix, would you consider creating a PR with a fix?
@JiteshGaikwad @vipuldarkknight
thanks a lot for reporting the bug! I can also reproduce the exception. Since you already found a fix, would you consider creating a PR with a fix?
how can we do that?
@vipuldarkknight standard Github way - you fork the project, commit a change, and open the PR towards Rasa-master 馃槃
@vipuldarkknight standard Github way - you fork the project, commit a change, and open the PR towards Rasa-master
yeah I have opened a PR proposing the same changes that I suggested earlier