Keep on getting raw content and not JSON in my Python Slack Interactive Endpoint

27 Views Asked by At

I have my 1st method which is posting a message to a Slack user with both Approve and Deny buttons and when the user clicks on any of the buttons it triggers my following method using an interactive endpoint configured on Slack i.e. Slack API -> Features -> Interactivity & Shortcuts (as shown in the attached screenshot).

Screenshot 2023-12-13 at 7.26.57 PM

This triggers my following method described below but the problem is that I always get the raw content and not JSON and this leads to the following error:-

Error handling interaction: Expecting value: line 1 column 1 (char 0)
INFO:   44.199.241.66:0 - "POST /interactive-endpoint HTTP/1.1" 500 Internal Server Error

My code:-

@app.post("/interactive-endpoint")

async def interactive_endpoint(request: Request):

    try:

        # Log the raw content of the request body

        raw_content = await request.body()

        print(f"Raw Content: {raw_content.decode()}")



        payload = await request.json()

        token = payload.get("token", "")



        # Verify the Slack token

        if token != SLACK_VERIFICATION_TOKEN:

            raise HTTPException(status_code=401, detail="Invalid Slack token")



        callback_id = payload.get("callback_id", "")



        if callback_id == "approval_request":

            # Handle approval request actions

            actions = payload.get("actions", [])

            for action in actions:

                if action.get("action_id") == "approve_button":

                    # Handle approve action

                    user_id = payload.get("user", {}).get("id")

                    # Trigger your backend action for approval



                elif action.get("action_id") == "deny_button":

                    # Handle deny action

                    user_id = payload.get("user", {}).get("id")

                    # Trigger your backend action for denial



        return JSONResponse(content={"message": "Interaction handled"}, status_code=200)



    except Exception as e:

        print(f"Error handling interaction: {e}")

        raise HTTPException(status_code=500, detail="Error handling interaction")

I'm stuck on this for a long time and did not find any useful material to solve this issue. Kindly advise

0

There are 0 best solutions below