Search code examples
slackslack-api

Slack bot post mesage to usergroup


I was able to create usergroups and update usergroups.

But there was no method in slack document to post a message to usergroup

https://api.slack.com/methods

I found this function chat.postMessage but from the arguments it takes I couldn't find usergroups as an option to post the message to.

https://api.slack.com/methods/chat.postMessage

Any help figuring out the appropriate slack api function is appreciated.


Solution

  • chat.postMessage is a correct way to post message into various conversations, i.e. public channel, private channel, DM or multi-person DM and in the channel argument you should provide the correct conversation ID, usually starting from C**, G**, D** followed with alphanumeric symbols. Slack usergroups have S** ID and are not considered as conversations. If you'll try to call chat.postMessage with channel=S** argument you'll get a channel_not_found error.

    Slack user groups are usually created to make easier users separation for mentioning or mass-inviting them into channels. Most typical, for mentioning. However, their mentioning is kind specific and described in the Slack documentation here: https://api.slack.com/reference/surfaces/formatting#mentioning-groups, i.e. you should include <!subteam^GROUP_ID> in your text argument or in your text "type": "mrkdwn" block.

    For example, this code:

    import logging
    
    from slack_sdk import WebClient
    from slack_sdk.errors import SlackApiError
    
    log = logging.getLogger(__name__)
    
    if __name__ == "__main__":
        client = WebClient(token="xox**")
        try:
            pt_mention = client.chat_postMessage(channel="C**",
                                                 text="Mentioning the <!subteam^S**> user group in plain text.")
            print(pt_mention)
    
            block_mention = client.chat_postMessage(channel="C**",
                                                    # text="Mentioning the <!subteam^S**> user group in the block.",
                                                    blocks=[
                                                        {
                                                            "type": "section",
                                                            "text": {
                                                                "type": "mrkdwn",
                                                                "text": "Mentioning the <!subteam^S**> user group in the block."
                                                            }
                                                        }
                                                    ])
            print(block_mention)
        except SlackApiError as e:
            logging.exception(f"Error occurred: {e.response['error']}")
    

    produces two sequential messages into the C** Slack channel where it mentions @test user group with ID S**: on this screenshot.

    However, it is not possible to invite a user group into Slack channel with conversations.invite call, you should use another method, described here.