Search code examples
pythonpython-2.7slackslack-api

Slack Slash Command removes the message of the person who did the slash command


I wrote a simple Slack Slash command that will just repeat back what the person says. For example, if you type "/say Test!" the bot will reply with "Test!".

Every time you do this, however, the message of the person doing the slash command does not get posted to the channel, so you are unable to see who prompted the message.

For example, If you type "/say Test!" your message is never posted in the channel, but the Bot's is. I would prefer if you could see that person's message before the bot replies. Here is my code.

from flask import Flask, request, Response
import os
from slackclient import SlackClient

SLACK_DEV_TOKEN = <TOKEN>  # Put your API dev token here

slack_client = SlackClient(SLACK_DEV_TOKEN)

app = Flask (__name__)

def send_message(channel_id, message):
    slack_client.api_call('chat.postMessage',
                          channel=channel_id,
                          text=message,
                          username='Bot',
                          icon_emoji=':Anguished:')




@app.route('/say', methods=['POST'])
def say():
    channel_id = request.form.get('channel_id')
    input_text = request.form.get('text').lower()
    #return "Test"
    send_message(channel_id,input_text)
    return Response(),200


if __name__ == '__main__':
    port = int(os.environ.get('PORT',5000))
    app.run(host='0.0.0.0',port=port,debug=True)

Is there anything I am doing wrong, or is it just the method that I chose to write this code that prevents the person posting the slash command from being visible?


Solution

  • I do not know much about Pyhthon, but it looks to me like you are sending a message back with the API method chat.postMessage. That is however not how you should reply to a slash commands. Instead you should send reply directly to the POST request from Slack with a JSON formated message. Then you can also set the property response_type to in_channel. That will have the effect that both the initial command and the response is visible to all users in the channel.

    See also Responding to a slash command in the Slack API documentation.