Search code examples
python-3.xdjangobotsslack

send message from bot to direct message after typing /slash command


I'm trying to make SlackBot and if I call him in some public channel it works fine but when I call him (type slash-command) in any direct channel I receive "The server responded with: {'ok': False, 'error': 'channel_not_found'}". In public channels where I've invited my bot it works fine, but if I type "/my-command" in any DM-channel I receive response in separate DM-channel with my bot. I expect to receive these responses in that DM-channel where I type the command. Here is some part of my code:

if slack_command("/command"):
    self.open_quick_actions_message(user_id, channel_id)
    return Response(status=status.HTTP_200_OK)

def open_quick_actions_message(self, user, channel):
    """
        Opens message with quick actions.
    """
    slack_template = ActionsMessage()
    message = slack_template.get_quick_actions_payload(user=user)
    client.chat_postEphemeral(channel=channel, user=user, **message)

Here are my Event Eubscriptions

Here are my Bot Token Scopes

Can anybody help me to solve this?


Solution

  • I've already solved my problem. Maybe it will help someone in the future. I've sent my payload as the immediate response as it was shown in the docs and the response_type by default is set to ephemeral.

    The part of my code looks like this now:

    if slack_command("/command"):
        res = self.slack_template.get_quick_actions_payload(user_id)
        return Response(data=res, status=status.HTTP_200_OK)
    else:
        res = {"text": "Sorry, slash command didn't match. Please try again."}
        return Response(data=res, status=status.HTTP_200_OK)
    

    Also I have an action-button and there I need to receive some response too. For this I used the response_url, here are the docs, and the requests library.

    Part of this code is here:

    if action.get("action_id", None) == "personal_settings_action":
        self.open_personal_settings_message(response_url)
    
    def open_personal_settings_message(self, response_url):
        """
            Opens message with personal settings.
        """
        message = self.slack_template.get_personal_settings_payload()
        response = requests.post(f"{response_url}", data=json.dumps(message))
        try:
            response.raise_for_status()
        except Exception as e:
            log.error(f"personal settings message error: {e}")
    

    P. S. It was my first question and first answer on StackOverflow, so don't judge me harshly.