【问题标题】:Slack is printing raw JSON instead of styled messageSlack 正在打印原始 JSON 而不是样式化消息
【发布时间】:2016-06-14 16:54:10
【问题描述】:

我正在用 PHP 编写一个 Slack 斜杠命令,该命令采用 Twitter 用户名并返回该人的最新推文。

我不知道如何显示用户的个人资料图片以使其看起来像 Slack 的 Twitter 集成一样漂亮。

在我看来,问题是我必须在发送回 Slack 的 JSON 中包含“unfurl_media:true”。这意味着我不能只使用echo 来打印出我想要的 Twitter 数据,这些数据位于关联数组中。

我尝试从关联数组中获取我想要的 Twitter 数据,将其再次编码为 JSON,然后打印出来。但所做的只是将 JSON 打印为纯文本。我检查了一个 JSON 验证器,它说打印到 Slack 的内容是有效的 JSON,所以我不明白为什么 slack 没有将其转换为样式消息。

有什么建议吗?

这是代码。它是从一堆不同的地方提取的 frankencode,我正试图按照自己的意愿屈服。

<?php

require_once('TwitterAPIExchange.php');

$command = $_POST['command'];
$text = $_POST['text'];
$token = $_POST['token'];

if($token != '[insert your Slack slash command token here]'){ 
$msg = ":squirrel: The token for the slash command doesn't match. We're done    here until IT fixes it. Don't worry, Squirrelock is on the case.";
die($msg);
echo $msg;
}


$settings = array(
'oauth_access_token' => "[insert access token here]",
'oauth_access_token_secret' => "[insert access token secret here]",
'consumer_key' => "[insert consumer key here]",
'consumer_secret' => "[insert consumer secret here]"
);

$url = "https://api.twitter.com/1.1/statuses/user_timeline.json";

$requestMethod = "GET";

$getfield = '?screen_name='.$text.'&count=1';

$twitter = new TwitterAPIExchange($settings);

$string = json_decode($twitter->setGetfield($getfield)
                          ->buildOauth($url, $requestMethod)
                          ->performRequest(), $assoc = TRUE);

foreach($string as $items)
{
$reply = "".$items['user']['profile_image_url']." *".$items['user'] ['name']."* ".$items['user']['screen_name']. "\n ".$items['text']."\n  ".$items['created_at']."";
}

$data = json_encode(array(
"response_type" => "in_channel",
"text" => $reply,
"unfurl_media" => true,
"unfurl_links" => true

));

echo $data;

?>

【问题讨论】:

  • “我检查了一个 JSON 验证器,它说打印到 Slack 的内容是有效的 JSON,所以我不明白为什么 slack 没有将其转换为样式消息。”您能否举例说明您发送到 Slack 的 JSON 以及您希望 Slack 使用该 JSON 做什么?
  • 这是一个例子。我希望 Slack 将此 JSON 转换为格式化消息。如果您将其放入Slack's Message Builder,它将呈现(在我得到它呈现后仍需要处理样式)。但由于某种原因,这不适用于 Slack 本身。 {"response_type":"in_channel","text": "http:\/\/pbs.twimg.com\/profile_images\/462698461792456704\/IkB137SH_normal.png ​*Ian Paul*​ ianpaul\n Cortana 非常棒它没有崩溃,这并不常见。\n Tue Jun 14 06:01:06 +0000 2016","unfurl_media":"true","unfurl_links":"true"}
  • JSON 再一次以更清晰的格式 @smarx 。 {"response_type":"in_channel","text": "http:\/\/pbs.twimg.com\/profile_images\/462698461792456704\/IkB137SH_normal.png ​*Ian Paul*​ ianpaul\n Cortana is so great when it's not crashing, which isn't often.\n Tue Jun 14 06:01:06 +0000 2016","unfurl_media":"true","unfurl_links":"true"}
  • echo $data; 之前尝试header('Content-Type: application/json');
  • 是的!添加标题效果很好!谢谢@smarx!实际上,我不明白这一点,因为我之前尝试添加标题但它没有做任何事情。我在这个问题上花了几个小时,所以现在有点模糊,但我可能已经更改了 $data 变量中的一些内容以更正一些 JSON 输出,并且没有尝试再次添加标题。无论如何,现在它可以工作了!非常感谢!

标签: php slack-api


【解决方案1】:

将我的评论移至答案,因为它似乎已经解决了问题:

echo $data; 之前添加header('Content-Type: application/json');。问题是 Slack 将您的回复解释为文本。仅当Content-Type 正确时,它才会将其解释为 JSON。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-19
    • 1970-01-01
    相关资源
    最近更新 更多