# Buggy code
async def process_message(
    self,
    request: Request,
    on_new_message: Callable[[UserMessage], Awaitable[Any]],
    text,
    sender_id: Optional[Text],
    metadata: Optional[Dict],
) -> Any:
    """Slack retries to post messages up to 3 times based on
    failure conditions defined here:
    https://api.slack.com/events-api#failure_conditions
    """
    retry_reason = request.headers.get(self.retry_reason_header)
    retry_count = request.headers.get(self.retry_num_header)
    if retry_count and retry_reason in self.errors_ignore_retry:
        logger.warning(
            f"Received retry #{retry_count} request from slack due to {retry_reason}."
        )

        return response.text(None, status=201, headers={"X-Slack-No-Retry": 1})

    if metadata is not None:
        output_channel = metadata.get("out_channel")
    else:
        output_channel = None

    try:
        user_msg = UserMessage(
            text,
            self.get_output_channel(output_channel),
            sender_id,
            input_channel=self.name(),
            metadata=metadata,
        )

        await on_new_message(user_msg)
    except Exception as e:
        logger.error(f"Exception when trying to handle message.{e}")
        logger.error(str(e), exc_info=True)

    return response.text("")


# Traceback (ValueError)
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

# Fixed code
async def process_message(
    self,
    request: Request,
    on_new_message: Callable[[UserMessage], Awaitable[Any]],
    text,
    sender_id: Optional[Text],
    metadata: Optional[Dict],
) -> Any:
    """Slack retries to post messages up to 3 times based on
    failure conditions defined here:
    https://api.slack.com/events-api#failure_conditions
    """
    retry_reason = request.headers.get(self.retry_reason_header)
    retry_count = request.headers.get(self.retry_num_header)
    if retry_count and retry_reason in self.errors_ignore_retry:
        logger.warning(
            f"Received retry #{retry_count} request from slack due to {retry_reason}."
        )

        return response.text(None, status=201, headers={"X-Slack-No-Retry": 1})

    if metadata is not None:
        output_channel = metadata.get("out_channel")
        if self.use_threads:
            thread_id = metadata.get("ts")
        else:
            thread_id = None
    else:
        output_channel = None
        thread_id = None

    try:
        user_msg = UserMessage(
            text,
            self.get_output_channel(output_channel, thread_id),
            sender_id,
            input_channel=self.name(),
            metadata=metadata,
        )

        await on_new_message(user_msg)
    except Exception as e:
        logger.error(f"Exception when trying to handle message.{e}")
        logger.error(str(e), exc_info=True)

    return response.text("")