slackapi / python-slack-events-api

Slack Events API adapter for Python (Flask required)
https://api.slack.com/events
MIT License
343 stars 116 forks source link

HTTP 200 Response question #61

Closed ztap345 closed 4 years ago

ztap345 commented 4 years ago

Description

I've read in the documentation and several forum posts about sending a HTTP 200 status back to slack and am unsure of how to do so and cannot find an example of how to do this with the slack event api. What would the proper way to send this response be?

What type of issue is this? (place an x in one of the [ ])

Requirements

Reproducible in:

slackeventsapi version: 2.1.0 python version: 3.7.0 OS version(s): Fedora 30

Steps to reproduce:

  1. use my code to start a slack bot
  2. call the fetch command in slack
  3. wait for duplicates to be sent

Expected result:

I expect only one response to be posted in the slack channel

Actual result:

The bot replies back with multiple responses like its retrying to send the command.

Attachments:

Here is the code producing the issue:

# Initialize a Flask app to host the events adapter
app = Flask(__name__)
slack_events_adapter = SlackEventAdapter(SIGINING_SECRET, "/slack/events", app)

# Initialize a Web API client
slack_web_client = WebClient(token=OAUTH_TOKEN)

def fetch(command):
    num_args = 2
    if len(command) != num_args:
        response = "Incorrect number of arguments ({})".format(num_args)

    else:
        t_id = command[1]

        try:
            response = "Fetched"
        except Exception as e:
            response = ("*Failed!*")
            raise

    return response

@slack_events_adapter.on("app_mention")
def on_mention(payload):
    """Parse commands sent from a mention
        ex command: @Bot fetch 12345
    """
    event = payload.get("event", {})

    if "bot_id" not in event:
        channel_id = event.get("channel")
        command_args = event.get("text").split(" ")
        # strip out the mention and leave the other elements in the list
        mention = command_args[0]
        command_args = command_args[1:] if len(command_args) > 1 else []

        if command_args:
            response = None

            # Check for fetch command
            if command_args[0].lower() == "fetch":
                response = fetch(command_args)

            if response:
                return bot_respond(response, channel_id)
            else:
                return bot_respond("Unrecognized command, use help for more information", channel_id)

def run_bot():
    logger = logging.getLogger()
    logger.setLevel(logging.DEBUG)
    logger.addHandler(logging.StreamHandler())
    ssl_context = ssl_lib.create_default_context(cafile=certifi.where())
    app.run(port=3000)

if __name__ == "__main__":
    run_bot()
ztap345 commented 4 years ago

Seems the issue was from running this script in a thread from a separate script.