1

I'm trying to get my Python Slack bot to automatically reply in a thread if I post it commands in one. However, regardless of where I post my commands - in a thread or otherwise, it still replies as a general message.

I wish to get it to reply in a thread. Here's my code so far (I've truncated most of the initializing and startup code for the sake of brevity):

import os, time, re, inspect, functools
from slackclient import SlackClient

class Bot(object):
    def __init__(self, token): 
        ...

    def startup(self):
        ...

    def parse_bot_commands(self, slack_events):
        """
            Parses a list of events coming from the Slack RTM API to find bot commands.
            If a bot command is found, this function returns a tuple of command and channel.
            If its not found, then this function returns None, None.
        """
        for event in slack_events:
            if event["type"] == "message" and not "subtype" in event:
                user_id, message = self.parse_direct_mention(event["text"])
                if user_id == self.starterbot_id:
                    return message, event["channel"]
        return None, None

    def parse_direct_mention(self, message_text):
        """
            Finds a direct mention (a mention that is at the beginning) in message text
            and returns the user ID which was mentioned. If there is no direct mention, returns None
        """
        matches = re.search(self.MENTION_REGEX, message_text)
        # the first group contains the username, the second group contains the remaining message
        return (matches.group(1), matches.group(2).strip()) if matches else (None, None)

    def handle_command(self, command, channel):
        """
            Executes bot command if the command is known
        """
        # Default response is help text for the user
        default_response = "Not sure what you mean. Try *{}*.".format(self.EXAMPLE_COMMAND)

        # Finds and executes the given command, filling in response
        response = None

        # NOTE: This is where you start to implement more commands!
        if command.lower().startswith("roll"):
            response = 'Rock and Roll!"

        # Sends the response back to the channel
        self.slack_client.api_call("chat.postMessage", channel=channel, text=response or default_response)

'''START THE BOT!'''
#Initialize the token (when installing the app)
bot = Bot('xxx-xxx')
bot.startup()
Code Monkey
  • 800
  • 1
  • 9
  • 27

1 Answers1

0

Slash commands do not work properly in threads. Its a known issue which has so far not been fixed.

See also this answer: Can a Slack bot get the thread id that a slash command was sent from?

Erik Kalkoken
  • 30,467
  • 8
  • 79
  • 114
  • The article says something about ‘thread_ts’ attribute to the message. Any ideas on how message attributes can be accessed? – Code Monkey Jun 27 '19 at 02:30
  • 2 main ways are: send a chat.postMessage or listen to message events. You will receive message objects that may contain that property – Erik Kalkoken Jun 27 '19 at 02:52
  • Check out the official page on threads for all details: https://api.slack.com/docs/message-threading – Erik Kalkoken Jun 27 '19 at 02:55