Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duplicate events with WebClient and direct messages #801

Closed
alexattia opened this issue Sep 8, 2020 · 2 comments
Closed

Duplicate events with WebClient and direct messages #801

alexattia opened this issue Sep 8, 2020 · 2 comments
Labels
question M-T: User needs support to use the project

Comments

@alexattia
Copy link

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

The Slack SDK version

slackclient==2.8.2

Python runtime version

Python 3.6.12

OS info

#108~16.04.1-Ubuntu

Steps to reproduce:

I am trying to build a bot that will respond to direct messages. Thus, I have followed the tutorial. These are parts of my code:

app = Flask(__name__)
slack_client = WebClient(token=token)
slack_events_adapter = SlackEventAdapter(event_token, "/slack/events", app)

def handle_command(message, user):
    slack_client.chat_postMessage(
        channel = user,
        text = message,
    )

@slack_events_adapter.on("message")
def parse_bot_commands(payload):
    event = payload.get("event", {})
    time.sleep(0.5)
    if event["type"] == "message" and not "subtype" in event :
        message = event["text"].lower()
        if 'hello' in message.lower():
            handle_command("Hello you", event['user'])

if __name__ == "__main__":
    print("Starter Bot connected and running!")
    app.run(host='0.0.0.0', port=3000)

Then, when I go on Slack and send a direct message to the bot.

Expected result:

I should have only one answer ("Hello you" in this case)

Actual result:

But, the received events are duplicated. When printing the events, I see multiple times the same event and so, the responses from the bots are duplicated.

My goal is to only have one message for each request.

Thank you very much in advance.

@seratch
Copy link
Member

seratch commented Sep 8, 2020

I'm afraid that your app might not respond to requests from Slack within 3 seconds.

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.

https://api.slack.com/events-api#the-events-api__responding-to-events

Unfortunately, the slackeventsapi PyPI package doesn't do any magical things for you. All your code in the Flask app must complete within less than 3 seconds.

As a better solution, we are actively working on a new package - slack-bolt. https://pypi.org/project/slack-bolt/

This new framework provides a much easier way to deal with this issue. A good thing is that you can use it along with the Flask framework (actually with any frameworks).

Bolt for Python it's still in alpha but it should already work fine for most cases. That said, if you're not comfortable with trying slack-bolt's alpha version releases out, please make sure if your code in the Flask app always returns HTTP status within 3 seconds and/or consider using multi threads/processes to deal with the issue.

@seratch seratch added question M-T: User needs support to use the project and removed untriaged labels Sep 8, 2020
@alexattia
Copy link
Author

Thank you very much for your detailed answer.
Even if slack-bolt is in alpha and not a lot documented, it's working great for me right now!
My code is now :

app = App()

def handle_command(message, say):
    say(message)

@app.event("message")
def parse_bot_commands(payload, say):
    event = payload
    if event["type"] == "message" and not "subtype" in event :
        message = event["text"].lower()
        if 'hello' in message.lower():
            handle_command("Hello you", say)

if __name__ == "__main__":
    print("Starter Bot connected and running!")
    app.start(port=3000)

Thanks for the great work on this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question M-T: User needs support to use the project
Projects
None yet
Development

No branches or pull requests

2 participants