Forums

Simple slack app - post gets sent but actual reply isn't sent unless I click "reload THENAME.pythonanywhere.com"

I set up a version of this app and it works fine so long as I click the Reload button on the dashboard every time I want a response. You ping the bot and say something like "hi" and it just replies "Hello" back.

In the access log I can see the request come in, followed by the response about a half second later. The reply won't show up until I click the reload button on the dashboard. I can build up multiple responses and they all suddenly appear once I click Reload. I feel like I must be missing something obvious but I can't get past this.

Hi -- your web app crashes with internal server error, so that is probably the reason why it's not working as you'd expect. Please check the error logs to debug it (you'll find links to them in the "Log files" section of the Web page). Also, I see that the code you try to implement (from the linked tutorial) is using multithreading -- that won't work on PythonAnywhere. We've got some hints on how to work around that: https://help.pythonanywhere.com/pages/AsyncInWebApps/.

Thank you. Removing the thread code got it to work.

However I am running into an issue that doesn't appear to be anything to do with pythonanywhere, but with slack.

I want to have this app do simple things like a query on another site and return the results. It takes up to a couple of seconds from what I can tell but I guess Slack is already closing the socket and I am getting multiple "OSError: write error" and the response is sent 2 to 4 times with the server log having this for each one

SIGPIPE: writing to a closed pipe/socket/fd (probably the client disconnected) on request /slack/events (ip xxx) !!!
2021-03-28 17:08:18 Sun Mar 28 17:08:18 2021 - uwsgi_response_write_headers_do(): Broken pipe [core/writer.c line 248] during POST /slack/events (xxx)

I'm unfamiliar with flask and web apps like this in general.

Edit : ah, I found in the slack doc that it expects a response within 3 seconds. Now I see why the sample wanted to use threading. If there is no threading available, is there a way to limit the retries so that it does not resend the reply multiple times?

If Slack is retrying so that extra messages are sent, check the Slack docs to see if you can set the number of retries there.

Hi @LindyNet,

The EXACT same thing was happening for me. I was banging my head against the wall for a day or two.

The fix is on the Slack client side, having stumbled across this section in the docs. The problem lies with the fact that the Slack API closes the connection before receiving a response from your app (it's trying to send a POST request back to Slack server and fails, hence the SIGPIPE issue). I am not a technical user so I can't begin to explain why this is happening. The fix is simple. When instantiating your App object, set the process_before_response parameter to True.

Try using the official Slack Bolt for Python tutorial to build a web app, as the tutorial you followed in the link above does not make use of the Bolt framework (which sits on top of Slack's Python SDK).

Hey @kaandikmen, thank you so much for that link to the Slack docs! I don't know what I would have done without it!