0
votes

I am starting out with implementing a HTML chatbot that can be used by users to send and receive messages to and fro a Slack channel using Python Flask framework. I have been following the tutorial at https://realpython.com/blog/python/getting-started-with-the-slack-api-using-python-and-flask/

I have a view created which is a simple chatbot ui, but I am stuck with Slack outgoing webhooks. I have reached till

@app.route('/slack', methods=['POST'])
def inbound():
    if request.form.get('token') == SLACK_WEBHOOK_SECRET:
        channel = request.form.get('channel_name')
        username = request.form.get('user_name')
        text = request.form.get('text')
        inbound_message = username + " in " + channel + " says: " + text
        print(inbound_message)
    return Response(), 200

I have been successful in receiving the message and printing it out to the console whenever there is any message posted in my slack channel. But how do I now pass on this string response over to my view?

I assume this has to be through websockets but could not find any relevant example on google (and am a complete newbie with python and websockets).

Kindly assist me with some help on this or direct me to a proper tutorial on receiving slack messages for a html client.

Thanks.

1

1 Answers

0
votes

You can just render the message to your view using render_template.

from flask import render_template
def inbound():
    if request.form.get('token') == SLACK_WEBHOOK_SECRET:
        channel = request.form.get('channel_name')
        username = request.form.get('user_name')
        text = request.form.get('text')
        inbound_message = username + " in " + channel + " says: " + text
        return render_template('index.html', message=inbound_message)

Now, you can retrieve and show the message in your view.

                  <p>{{ message }}</p>

Alternatively, you can store the message in a session, in which case you can access it from the route that renders your view.