Slack 正在打印原始 JSON 而不是样式化消息
Slack is printing raw JSON instead of styled message
我正在 PHP 中使用 Slack 斜杠命令,该命令采用 Twitter 用户名和 returns 该人的最新推文。
我不知道如何显示用户的个人资料图片,使其看起来像 Slack 的 Twitter 集成一样漂亮。
在我看来,问题是我必须将 'unfurl_media: true' 包含在 JSON 中,然后发送回 Slack。这意味着我不能只使用 echo
打印出我想要的 Twitter 数据,它位于关联数组中。
我尝试从关联数组中获取我想要的 Twitter 数据,在 JSON 中再次对其进行编码,然后打印出来。但所做的只是将 JSON 打印为纯文本。我检查了一个 JSON 验证器,它说打印到 Slack 的内容是有效的 JSON,所以我不明白为什么 Slack 没有将它转换为样式化消息。
有什么建议吗?
这是代码。它是从我试图屈从于我的意志的一堆不同地方提取的科学代码。
<?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;
?>
将我的评论移至答案,因为它似乎已经解决了问题:
在 echo $data;
之前添加 header('Content-Type: application/json');
。问题是 Slack 将您的回复解释为文本。仅当 Content-Type
正确时,它才会将其解释为 JSON。
我正在 PHP 中使用 Slack 斜杠命令,该命令采用 Twitter 用户名和 returns 该人的最新推文。
我不知道如何显示用户的个人资料图片,使其看起来像 Slack 的 Twitter 集成一样漂亮。
在我看来,问题是我必须将 'unfurl_media: true' 包含在 JSON 中,然后发送回 Slack。这意味着我不能只使用 echo
打印出我想要的 Twitter 数据,它位于关联数组中。
我尝试从关联数组中获取我想要的 Twitter 数据,在 JSON 中再次对其进行编码,然后打印出来。但所做的只是将 JSON 打印为纯文本。我检查了一个 JSON 验证器,它说打印到 Slack 的内容是有效的 JSON,所以我不明白为什么 Slack 没有将它转换为样式化消息。
有什么建议吗?
这是代码。它是从我试图屈从于我的意志的一堆不同地方提取的科学代码。
<?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;
?>
将我的评论移至答案,因为它似乎已经解决了问题:
在 echo $data;
之前添加 header('Content-Type: application/json');
。问题是 Slack 将您的回复解释为文本。仅当 Content-Type
正确时,它才会将其解释为 JSON。