Search code examples
slackslack-api

How do I change the slack bot display name dynamically via slack API?


I am trying to setup a slack bot which sends reminders out for varying reasons. I've already setup the API calls to send messages via our slack bot. The issue is that each reminder should have a different display name as opposed to the static display name "reminder".

My question is, is there any way via the slack API to dynamically change the name of your bot every time a reminder is sent through API post request.

For example : I have a reminder sent out for office members to put their time sheet in so the bot should have the name "Timesheet Reminder" when sending said reminder. But then I also have a reminder in slack to reach out to a client, so I'd like the bot display name to dynamically change to "Client Reach Out" and not stay as "Timesheet Reminder"..

Let me know if anyone can help, I would really appreciate it.

I tried to do this via the users.profile.set call and nothing changed..

https://slack.com/api/users.profile.set?name=BOTNAME&user=BOTUSERID&display_name=CHANGENAMETOTHIS

The display_name value stayed the same:

 "display_name": "",

Solution

  • Great news, I figured it out myself so here is the solution in case anyone needs it:

    Make a postrequest to this api endpoint:

    https://slack.com/api/users.profile.set
    

    Next you need set the parameters as follows :

     $payload=[
                'user' => 'YOUR_BOT_USER_ID',
                'profile' => [
                  'display_name' => 'INSERT_YOUR_DYNAMIC_NAME_HERE',
                ],
            ];  
    //This example is in PHP
    //The trick here is to make sure to nest 'display_name' in 'profile'
    

    If you don't know how to get the user ID for your bot, here's how:

    go to the slack api page: https://api.slack.com/methods/auth.test/test

    Make sure Select A Token is set to 'No Token' and then under Or, provide your own token:, enter your bot token.

    voilà, your slack bot can now change names as per request!