Slack以用户身份发布而不是机器人身份

huangapple go评论60阅读模式
英文:

Slack post as user instead as bot

问题

根据您提供的信息,您正在尝试代表用户在Slack上发布消息。您创建了一个Slack应用并将用户重定向到以下链接:

https://slack.com/oauth/v2/authorize?scope=chat:write,incoming-webhook,channels:read,groups:read,im:read,mpim:read&client_id=16615312099.5562656968566W

一切都进行得很顺利。但是,当我获取一个Bearer令牌并使用它来发布到特定的Slack频道时,消息不是以用户的身份发布,而是以我的Slack应用的身份发布。

$res = Http::asForm()->withToken('xoxe.xoxb-1-MS0yLTE2NjE1MzEyMDk5LTU1ODIyODc2ODU4NTctNTU5MzgwNjgxNjU5Mi01NTY5OTg1ODg2NjQzLTIzYzg2MzM1YTVmNGQwMDMyMjNkY2ExOTA5M2QzOIwZmU5YTk1ZjY4M2FjNjg4ZDA1NzBjNTI3MmI2Y2I')
    ->withHeaders([
        'Content-type' => 'application/x-www-form-urlencoded;charset=utf-8'
    ])->post('https://slack.com/api/chat.postMessage', [
        'channel' => 'C5GS2B67Q',
        'text' => 'Hello world',
    ]);

为什么会出现这种情况,以及如何确保消息是由用户发布的呢?

根据chatGPT的建议:

根据您提供的代码片段,似乎您正在使用与应用相关联的访问令牌('xoxe.xoxb-1-...'),而不是用户令牌。当您使用应用令牌时,消息将以应用本身的身份发布,而不是作为用户。

要以用户的身份发布消息,您需要获取用户令牌,而不是应用令牌。用户令牌是特定于个人用户的,允许您的应用代表他们执行操作。

要获取用户令牌,通常需要遵循Slack提供的OAuth流程等身份验证过程。一旦您获得了用户令牌,您可以在代码片段中使用它以用户的身份发布消息。

以下是使用OAuth流程获取用户令牌的示例:

  1. 将用户重定向到Slack的OAuth授权页面,指定必要的范围(例如chat:write.user),以便代表用户发布消息。
  2. 用户授权您的应用后,Slack将重定向回您指定的重定向URL,同时提供一个临时授权代码。
  3. 通过向Slack的令牌API(https://slack.com/api/oauth.v2.access)发送请求,提供代码、客户端ID、客户端秘钥和重定向URL来交换授权代码以获得用户令牌。一旦获得了用户令牌,您就可以在代码片段中使用它以用户的身份在Slack中发布消息。

但是需要注意的是,Slack似乎没有提供特定的'chat:write.user'范围,这可能需要与Slack的最新文档或API版本进行确认。您可能需要查看Slack API文档以获取有关如何以用户的身份发布消息的最新信息。

英文:

I'm trying to post on behalf of a user to slack. Created a slack app and are redirecting to:

https://slack.com/oauth/v2/authorize?scope=chat:write,incoming-webhook,channels:read,groups:read,im:read,mpim:read&client_id=16615312099.5562656968566W

That all goes well. When I fetch a bearer token and use that for posting to a specific slack channel it's not posted as the user but as my slack app.

$res = Http::asForm()->withToken('xoxe.xoxb-1-MS0yLTE2NjE1MzEyMDk5LTU1ODIyODc2ODU4NTctNTU5MzgwNjgxNjU5Mi01NTY5OTg1ODg2NjQzLTIzYzg2MzM1YTVmNGQwMDMyMjNkY2ExOTA5M2QzOIwZmU5YTk1ZjY4M2FjNjg4ZDA1NzBjNTI3MmI2Y2I')
    ->withHeaders([
        'Content-type' => 'application/x-www-form-urlencoded;charset=utf-8'
    ])->post('https://slack.com/api/chat.postMessage', [
        'channel' => 'C5GS2B67Q',
        'text' => 'Hello world',
    ]);

Why is this the case and how can I make sure it's posted by the user.

According to chatGPT:

> Based on the code snippet you provided, it appears that you are using
> an access token ('xoxe.xoxb-1-...') associated with an app instead of
> a user token. When you use an app token, the message will be posted as
> the app itself rather than as a user.
>
> To post messages as a user, you need to obtain a user token instead of
> an app token. User tokens are specific to individual users and allow
> your application to act on their behalf.
>
> To obtain a user token, you typically need to follow an authentication
> process such as the OAuth flow provided by Slack. Once you have the
> user token, you can use it in your code snippet to post messages as
> that user.
>
> Here's an example of how you can obtain a user token using the OAuth
> flow:
>
> Redirect the user to Slack's OAuth authorization page, specifying the
> necessary scopes (chat:write.user) for posting messages as a user.
> After the user authorizes your application, Slack will redirect back
> to your specified redirect URL with a temporary authorization code.
> Exchange the authorization code for a user token by making a request
> to Slack's token API (https://slack.com/api/oauth.v2.access),
> providing the code, client ID, client secret, and redirect URL. Once
> you have the user token, you can use it in your code snippet to post
> messages as a user in Slack.

But there is no chat:write.user scope?

答案1

得分: 1

有两种方式可以代表用户在Slack平台上发布消息,其中一种更像是一个“模拟用户”而不是用户本身。

  1. 不要使用您的机器人令牌,对于每个您想代表的用户,通过OAuth安装流程将用户传递并请求chat:write:user作为user_scope。将这些令牌与您的机器人/应用程序令牌分开存储。在代表用户发布消息时,使用此令牌而不是您的机器人令牌。

  2. 在安装流程中,请求将chat:write.customizescope添加到您的机器人令牌中。然后,您可以使用额外的参数来chat.postMessage以设置要发布的消息的显示名称和头像。您的机器人用户仍然是发布消息的用户,只是自定义成另一个用户的样式。

英文:

There are two ways to post on behalf of a user on the Slack platform, and one of them is more of a "mock user" than the user itself.

  1. Instead of using your bot token, for each user you want to post on behalf of, send the user through the OAuth installation flow and ask for chat:write:user as a user_scope. Store these tokens separate from your bot/app tokens. Use this token when posting on behalf of the user instead of your bot token.

  2. During the installation flow, ask for the scope of chat:write.customize to be added to your bot token. You can then use additional arguments to chat.postMessage to set the display name and avatar of the message being posted. Your bot user is still the user posting the message, it's just customized to look like another user.

huangapple
  • 本文由 发表于 2023年7月13日 21:46:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76680107.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定