Search code examples
pythonflaskpipslack

Why is my python code in cloud function not executing slack bot slash command


I am stuck with a basic python script. where I am integrating it with a slack bot and using slash command my slash command is /delete-session when this command is triggered in slack I would expect cloud function in GCP to return a message.How ever I tried to post message in this slack channel and it did post. I am not able to figure out what is causing this issue here code

import os
from flask import Flask, request, Response
from slack_sdk import WebClient

app = Flask(__name__)

# Set your Slack token
slack_token = "abcdev-xxx-xxxxx"


# Initialize Slack client
client = WebClient(token=slack_token)

@app.route('/delete-session', methods=['POST'])
def delete_session():
    data = request.form
    channel_id = data.get('channel_id')

    # Handle the /delete-session command
    response_message = "I am ready"

    # Send the response back to the channel
    client.chat_postMessage(channel=channel_id, text=response_message)

    return Response(), 200

Error that I am getting in cloud function

  File "/layers/google.python.pip/pip/lib/python3.10/site-packages/flask/app.py", line 2073, in wsgi_app
    response = self.full_dispatch_request()
  File "/layers/google.python.pip/pip/lib/python3.10/site-packages/flask/app.py", line 1518, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/layers/google.python.pip/pip/lib/python3.10/site-packages/flask/app.py", line 1516, in full_dispatch_request
    rv = self.dispatch_request()
  File "/layers/google.python.pip/pip/lib/python3.10/site-packages/flask/app.py", line 1502, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
  File "/layers/google.python.pip/pip/lib/python3.10/site-packages/functions_framework/__init__.py", line 99, in view_func
    return function(request._get_current_object())
TypeError: delete_session() takes 0 positional arguments but 1 was given

Solution

  • Per the documentation, Python Cloud Functions in Google Cloud are non-optionally invoked with a single parameter request:

    Your HTTP handler function must accept a Flask request object as an argument and return a value that Flask can convert into an HTTP response object.

    Redefine your function definition to accept this parameter as you see fit, i.e.:

    def delete_session(request):
        # ...