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

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!

  • 写回答

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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波