php - 避免对 in_channel 响应回显斜杠命令

标签 php slack slack-api

我现在正在为我的 channel 创建一个非常小的斜杠命令。我使用 HTTPS 200 将命令发送到 PHP 脚本,对其进行操作并将其作为 JSON 有效负载发回,基本上是这样的:

<?php
header('Content-Type: application/json');

$text = $_POST['text'];

if ($text === "keyword") $answer = "Hello World";
else $answer = "Nothing in here";

$response = array(
  'response_type' => 'in_channel',
  'text' => $answer,
);

echo json_encode($response);

现在我不想像标题中提到的那样在 channel 中发布初始斜杠推荐,但 channel 中的每个人都应该只看到服务器的答案。我找不到合适的文章,所以我问自己:这可能吗?

有人有想法吗?

编辑: 我不希望 slack 也将我的命令作为消息发布。这样只有第二条消息在 channel 中发布: see a screenshot

更新: 我现在已经实现了 Erik Kalkoken 的建议,但是我没有从 curl 函数中得到信号。我还成功地检查了我的网络服务器是否支持 curl 功能。我的代码现在看起来像这样(代码已减少到必要的部分):

<?php
header('Content-Type: application/json');

$response = array(
  'response_type' => 'in_channel',
  'text' => "> \\M2\Kunden\_Tutorial"
);

echo json_encode(array('text' => 'One moment...'));

$ch = curl_init($_POST['response_url']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $response);
$exec = curl_exec($ch);
curl_close($ch);

工作解决方案: 再次考虑了 Erik Kalkoken 的建议后,还是不行。这并不意味着他说错了什么。相反,我忘记了什么。我发现我的 $response 数组需要编码为 json 字符串。您可以在下面看到一个工作版本。感谢 Erik Kalkoken!

    <?php
    $response = array(
      'response_type' => 'in_channel',
      'text' => '> \\M2\Kunden\_Tutorial'
    );

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $_POST['response_url']);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($response));
    curl_setopt($ch, CURLOPT_POST, 1);
    $headers = array();
    $headers[] = "Content-Type: application/json";
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    $result = curl_exec($ch);
    curl_close ($ch);

最佳答案

当您使用 response_type 设置为 in_channel 时,斜线命令的标准行为会响应您的斜线命令。这通常是有道理的,因此该 channel 中的人们可以在看到结果之前看到斜线命令有问题。

没有直接的方法可以关闭此回显功能,但有一个简单的解决方法。

不要直接回复 Slack 请求,而是将您的响应作为新请求发送到 response_url,这是您在初始请求中从 Slack 获得的。然后你只会在 channel 中得到你的回应,斜杠命令本身不会被回显。

顺便说一句。 documentation 中也解释了此行为在“发送延迟响应”部分:

As with immediate response messages, you can include the response_type field. However, when you use a value of in_channel with this delayed method, the original text that invoked the command is not included.

我建议使用 curl将请求发送到 Slack。

在使用 curl 调用更新问题后进行编辑

您仍在返回对 Slack 请求的直接响应,这导致 Slack 回显用户命令。除了 200 OK(您的 PHP 脚本会自动执行)之外,您不得返回任何内容。所以请删除这一行:

echo json_encode(array('text' => 'One moment...'));

我还建议通过添加以下行在 curl 调用中包含正确的内容 header :

curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);

开头的 header() php 命令不影响 curl 调用,也可以顺便删除。

关于php - 避免对 in_channel 响应回显斜杠命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52151188/

相关文章:

slack-api - 松弛斜杠命令仅显示对 channel 的响应

python-3.x - Slack 上未显示交互式响应

slack - 如何修复在 SLACK 中获取 users.profile.get 的错误

松弛 API : is it ever possible to update other user's messages?

php - 一篇很长的文章如何分片存入数据库方便检索和分页?

php - 如何将项目数组转换为其对应的 id 数组

php - 使用 php 读取 xml 响应

php - 表单提交期间在链配置的命名空间中找不到类 'Symfony\Component\Form\Form'

prometheus - 设置 PromQL 值的格式

node.js - Slackbot 发出 HTTP 请求