Is it possible to post to chat.postMessage as any user in a Slack team?

穿精又带淫゛_ 提交于 2019-11-29 11:28:50

There are 2 ways to achieve this:

A. Overwriting username and icon

When you send a message with chat.postMessage it is possible to set a user name with the property username. The message will then appear as being send by that user (same for icon with icon_url).

However, this is not meant to impersonate real users, so even if you use the same username and icon as the real user the message will have the app tag, so that they can be distinguished from a real user.

Here is an example how it looks like (from a gamer Slack about flying and killing space ships):

But depending on what your requirements are that might work for you.

If you want to use it make sure to also set the as_user property to false (yes, really) and it will not work with a bot token, only with a user token.

See here for more details on how it works.

This also works for the legacy version of Incoming Webhooks, not with the current version of incoming webhooks though. (You can still get the legacy version, see this answer)

B. Having the user's token

Another approach is to always use the token from the respective user for sending the message. In combination with as_user = true messages sent by your app will look exactly as if they would come from the respective user (no APP tag).

To make that happen your app would need to collect tokens from all users on your workspace and store them for later use. This can be done by asking every user to install your app (called adding a "configuration") through the Oauth process (same you use to install your app to a workspace), which allows your app to collect and store those tokens for later use.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!