我为Slack创建了一个(相当简单的)实现,用PHP在一个通道中发布某些消息。一切都很好,除了附件。出于某种原因,他们确实出现在"一些"消息,但不是一切。
我使用的代码段几乎与此here
相同<?php
/**
* Slack Incoming Webhook - Single PHP File
*
* @author Chris - http://codepad.co/cheers | https://github.com/webremote
* @copyright Copyright (c) 2016
* @version 0.0.28
* @build 20160204
*/
// Settings
$token = 'xxx-xxx-xxx-xxx-xxx'; // Request your token via https://api.slack.com/tokens
$channel = '#mandrill-log';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (!empty($_POST['mandrill_events'])) {
foreach (json_decode($_POST['mandrill_events'], true) as $entry) {
// Default values
$attachments = [];
$type = 'Unknown';
$emoji = 'question';
$text = print_r($entry, true);
switch ($entry['event']) {
case 'send':
$text = 'Sent a mail to ' . $entry['msg']['email'] . ' with subject "' . $entry['msg']['subject'] . '" (from: ' . $entry['msg']['sender'] . ')';
$emoji = 'email';
$type = 'Sent';
break;
case 'click':
$text = 'Somebody from ' . $entry['location']['country'] . ' clicked the URL "' . $entry['url'] . '"';
$emoji = 'point_right';
$type = 'Click';
$attachments = [
'fallback' => $text,
'text' => 'URL clicked: ' . $entry['url'],
'fields' => [
[
'title' => 'Location',
'value' => $entry['location']['country'] . (!empty($entry['location']['region']) ? PHP_EOL . $entry['location']['region'] : '') . (!empty($entry['location']['city']) ? PHP_EOL . $entry['location']['city'] : ''),
'short' => true,
],
[
'title' => 'Browser',
'value' => $entry['user_agent_parsed']['type'],
'short' => true
],
[
'title' => 'Subject',
'value' => $entry['msg']['subject'],
'short' => true,
],
[
'title' => 'Sender',
'value' => $entry['msg']['sender'],
'short' => true
]
],
'thumb_url' => $entry['user_agent_parsed']['ua_icon']
];
break;
case 'open':
$text = 'Somebody from ' . $entry['location']['country'] . ' opened an email "' . $entry['msg']['subject'] . '"';
$emoji = 'open_book';
$type = 'Read';
$attachments = [[
'fallback' => $text,
'fields' => [
[
'title' => 'Location',
'value' => $entry['location']['country'] . (!empty($entry['location']['region']) ? PHP_EOL . $entry['location']['region'] : '') . (!empty($entry['location']['city']) ? PHP_EOL . $entry['location']['city'] : ''),
'short' => true,
],
[
'title' => 'Browser',
'value' => $entry['user_agent_parsed']['type'] . ($entry['user_agent_parsed']['mobile'] ? ' - Mobile' : ' - Desktop'),
'short' => true
],
[
'title' => 'Subject',
'value' => $entry['msg']['subject'],
'short' => true,
],
[
'title' => 'Sender',
'value' => $entry['msg']['sender'],
'short' => true
]
],
'thumb_url' => $entry['user_agent_parsed']['ua_icon']
]];
break;
case 'soft_bounce':
$text = 'Email to ' . $entry['msg']['email'] . ' with subject "' . $entry['msg']['subject'] . '" got (soft) bounced';
$emoji = 'space_invader';
$type = 'Bounce';
break;
case 'hard_bounce':
$text = 'Email to ' . $entry['msg']['email'] . ' with subject "' . $entry['msg']['subject'] . '" got (hard) bounced (' . $entry['msg']['diag'] .')';
$emoji = 'skull_and_crossbones';
$type = 'Bounce';
break;
}
if ($entry['type'] == 'blacklist') {
if ($entry['action'] == 'add') {
$type = 'Blacklist';
$emoji = 'new_moon_with_face';
$text = 'The e-mail ' . $entry['reject']['email'] . ' has been added to the blacklist (Reason: ' . $entry['reject']['reason'] . ', expires at ' . $entry['reject']['expires_at'] . ')';
}
}
if (empty($attachments)) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, 'Webremote.Mandrill.Webhook');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_URL, 'https://slack.com/api/chat.postMessage?');
curl_setopt($ch, CURLOPT_POSTFIELDS, [
'token' => $token,
'channel' => $channel,
'username' => 'Mandrill-' . $type,
'text' => $text,
'icon_emoji' => ':' . $emoji . ':'
]);
curl_exec($ch);
curl_close($ch);
}
elseif (!empty($attachments)) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, 'Webremote.Mandrill.Webhook');
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_URL, 'https://slack.com/api/chat.postMessage?' . http_build_query([
'token' => $token,
'channel' => $channel,
'username' => 'Mandrill-' . $type,
'text' => $text,
'attachments' => json_encode($attachments),
'icon_emoji' => ':' . $emoji . ':'
]));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'content-length: ' . strlen($dataSend)
]);
$response = curl_exec($ch);
curl_close($ch);
}
}
}
}
对于&#34;案例:发送&#34; (第23行)我也想显示附件,但由于某种原因,它不起作用。松弛的结果只是&#34; text&#34; -field,而附件完全被忽略......没有充分的理由?
我已尝试搜索API文档,常见问题解答甚至是SO,但没有出现任何主题。是否存在附件无法显示的具体原因?