doudao9896
2017-07-06 19:56
浏览 36
已采纳

Slack Slash命令 - 不在私有通道或直接消息中工作

I have programmed several slash commands that show a response in public channels without any problems, but they don't show any response in private channels or direct messages.

As shown below, I am using the in_channel response type. Is there any other response type I can use or a workaround so that it works everywhere?

$data = array(
    "username" => "My_user",
    "channel" => $channel_id,
    "response_type" => "in_channel",
    "text" => $text,
    "mrkdwn" => true,
    "icon_url" => $icon_url

);

$json_string = json_encode($data);
$slack_call = curl_init($slack_webhook_url);
curl_setopt($slack_call, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($slack_call, CURLOPT_POSTFIELDS, $json_string);    
curl_setopt($slack_call, CURLOPT_CRLF, true);
curl_setopt($slack_call, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($slack_call, CURLOPT_HTTPHEADER, array(                                                                          
    "Content-Type: application/json",
    "Content-Length: " . strlen($json_string))
);                                                                                                                   
$result = curl_exec($slack_call);
curl_close($slack_call);

Thanks in advance!

图片转代码服务由CSDN问答提供 功能建议

我编写了几个斜杠命令,在公共频道中显示响应没有任何问题,但它们没有显示任何问题 私人频道或直接消息的回应。

如下所示,我使用的是in_channel响应类型。 我可以使用任何其他响应类型或解决方法,以便它可以在任何地方使用吗?

  $ data = array(
“username”=>“My_user”,
“channel”=> $ channel_id,
“response_type”=>  “in_channel”,
“text”=> $ text,
“mrkdwn”=> true,
“icon_url”=> $ icon_url 
 
); 
 
 $ json_string = json_encode(  $ data); 
 $ slack_call = curl_init($ slack_webhook_url); 
curl_setopt($ slack_call,CURLOPT_CUSTOMREQUEST,“POST”); 
curl_setopt($ slack_call,CURLOPT_POSTFIELDS,$ json_string);  
curl_setopt($ slack_call,CURLOPT_CRLF,true); 
curl_setopt($ slack_call,CURLOPT_RETURNTRANSFER,true);  
curl_setopt($ slack_call,CURLOPT_HTTPHEADER,array(
“Content-Type:application / json”,
“Content-Length:”。strlen($ json_string))
);  
 $ result = curl_exec($ slack_call); 
curl_close($ slack_call); 
   
 
 

提前致谢!

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duanji1482 2017-07-06 23:54
    已采纳

    That does not look like the right approach.

    Responding to slash commands

    For responding to a slash command you must not send a new message back (e.g. via webhook as in your code example). Instead just respond to the request from Slack with the content of your message.

    Example

    $message = array (
        'response_type' => 'in_channel',
        'text' => $text
        );
    
    header ('content-type: application/json');
    echo json_encode ($message);
    

    that is all you need.

    response_type defines if the response can be seen by all members of a channel "in_channel" or only by the issuer of the slash command "ephemeral"

    Please see the official documentation for more details and options.

    Sending additional messages

    You can of course also send a message from your script in response to the slash command. However, if you want to send a message to a private channel please note that the slash command request from Slack will not include the correct channel ID if it is used in a non-public channel. I don't think there is currently any solution or workaround for this.

    You can however always send a direct message to the user by using the ID of the user as channel ID.

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dongshi1869 2017-07-10 15:08

    I talked to the Slack team, which was extremely helpful, and we figured out what the problem was. I am sharing it here in case anybody else runs into the same problem.

    The problem was not about the commands being used in public or private channels. The person who created the webhook set it so that it would work on a private channel (our testing channel), so it would only work in that channel or in any channels that she was a part of (so, all the public channels). As soon as I added her to a private channel, it would work.

    The solution was for the creator of the webhook to edit it (not the code, just the webhook) and set it by default to a public channel (any) instead of a private channel. This made it work in every channel, even direct messages.

    This way, I was able to use my original code, which also allows me to change the user icon dynamically, instead of sending a message back.

    I hope that helps other people as well!

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题