Python-slack-sdk: Getting multiple response for a query in slack Event API

Created on 17 Jul 2018  路  10Comments  路  Source: slackapi/python-slack-sdk

Description

I am getting the same response many times for a query when I do other API calls inside my method

Describe your issue here.

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

  • [ ] bug
  • [ ] enhancement (feature request)
  • [ X] question
  • [ ] documentation related
  • [ ] testing related
  • [ ] discussion

Requirements (place an x in each of the [ ])

  • [ ] I've read and understood the Contributing guidelines and have done my best effort to follow them.
  • [ ] I've read and agree to the Code of Conduct.
  • [ X] I've searched for any related issues and avoided creating a duplicate issue.

Bug Report

Filling out the following details about bugs will help us solve your issue sooner.

Reproducible in:

slackclient version:

python version:

OS version(s):

Steps to reproduce:

1.
2.
3.

Expected result:

What you expected to happen

Actual result:

What actually happened

Attachments:

Logs, screenshots, screencast, sample project, funny gif, etc.

Most helpful comment

@Sambeth if you can check the headers of those duplicate requests, you'll see a retry header with a retry number. It sounds like your app is either a) not responding with a 200 or b) not responding within 3 seconds.

With each retry attempt, you'll also be given a X-Slack-Retry-Num HTTP header indicating the attempt number: 1, 2, or 3.

https://api.slack.com/events-api#responding_to_events#error_handling

We'll tell you why we're retrying the request in the X-Slack-Retry-Reason HTTP header.

https://api.slack.com/events-api#responding_to_events

Your app should respond to the event request with an HTTP 2xx within three seconds. If it does not, we'll consider the event delivery attempt failed. After a failure, we'll retry three times, backing off exponentially.

Maintain a response success rate of at least 5% of events per 60 minutes to prevent automatic disabling.

Respond to events with a HTTP 200 OK as soon as you can. Avoid actually processing and reacting to events within the same process. Implement a queue to handle inbound events after they are received.

All 10 comments

@thangaraj14 would you mind posting the code for the wrapper method you're using? 馃

Rereading this, it sounds like this might be related to the Events API library, rather than this one.

https://github.com/slackapi/python-slack-events-api

Also, it might be related to Events API retries: https://api.slack.com/events-api#graceful_retries

I am getting multiple responses for an API call. Sometimes I am creating a slack bot that takes an input and give a certain response depending on the input. But I keep getting multiple responses and sometimes I get responses for old inputs even after I have restarted my server. How is that even possible?

@Sambeth would you mind posting your event jangling code (without tokens, etc)?

```
def _event_handler(event_type, message):
"""
A helper function that routes events from Slack to our Bot
by event type and subtype.
Parameters
----------
event_type : dict
type of event received from Slack
message : dict
JSON response from a Slack reaction event
Returns
----------
obj
Response object with 200 - ok or 500 - No Event Handler error
"""
# team_id = slack_event["team_id"]
# ================ Team Join Events =============== #
# When the user first joins a team, the type of event will be team_join

# create bot node
global text
nodes.bot_node(bot.name)

if event_type["type"] == "message":

    if event_type.get("channel_type") == "im":
        channel = message.get('event')['channel']

        # get text and create message node
        text = message.get('event')['text']
        nodes.messages_nodes(text)

        # get user and create user node
        user = message.get('event')['user']
        nodes.user_node(user)

        # create sends & sent_to relationships
        relationships.sends(user, text)

        # send response to user and create sent_to relationships
        bot.send(text, channel)
        relationships.sent_to(text, bot.name)

        return Response(status=status.HTTP_200_OK)
    return Response(status=status.HTTP_200_OK)```

```class EventsAPI(APIView):

def post(self, request, *args, **kwargs):
    message = request.data

    if message.get('token') != bot.verification:
        return Response(status=status.HTTP_403_FORBIDDEN)

    # verification challenge
    elif message.get('type') == 'url_verification':
        return Response(data=message,
                        status=status.HTTP_200_OK)

    elif 'event' in message:
        event_type = message.get('event')
        if event_type.get("subtype") == "bot_message":
            bot_message = message.get('event')['text']
            nodes.messages_nodes(bot_message)

            # create reply and reply to relationships
            relationships.reply(bot_message, bot.name)
            relationships.reply_to(bot_message, text)
        else:
            _event_handler(event_type, message)

    return Response(status=status.HTTP_200_OK)```

@Sambeth if you can check the headers of those duplicate requests, you'll see a retry header with a retry number. It sounds like your app is either a) not responding with a 200 or b) not responding within 3 seconds.

With each retry attempt, you'll also be given a X-Slack-Retry-Num HTTP header indicating the attempt number: 1, 2, or 3.

https://api.slack.com/events-api#responding_to_events#error_handling

We'll tell you why we're retrying the request in the X-Slack-Retry-Reason HTTP header.

https://api.slack.com/events-api#responding_to_events

Your app should respond to the event request with an HTTP 2xx within three seconds. If it does not, we'll consider the event delivery attempt failed. After a failure, we'll retry three times, backing off exponentially.

Maintain a response success rate of at least 5% of events per 60 minutes to prevent automatic disabling.

Respond to events with a HTTP 200 OK as soon as you can. Avoid actually processing and reacting to events within the same process. Implement a queue to handle inbound events after they are received.

@Sambeth you may also be interested in our Events API Adapter library: https://github.com/slackapi/python-slack-events-api/tree/master/example

Thanks very much for your help. It's make a lot of sense. I'm trying to figure out how to run the _event_handler after returning the response within 3 secs.

I have found a workaround. I had to run the logical process in a thread so the API view will not wait for the logical process and immediately send a response to slack ones it gets an event.

I have found a workaround. I had to run the logical process in a thread so the API view will not wait for the logical process and immediately send a response to slack ones it gets an event.

Hey Sambeth, could you please provide an example code of that? It would help a lot. Thanks

Was this page helpful?
0 / 5 - 0 ratings