你必须分两部分完成
- 先将图片上传到https://filesng.yammer.com/v4/uploadSmallFile,并获取图片的id。
- 像往常一样将您的消息发送到https://www.yammer.com/api/v1/messages.json,并附上新获得的图片ID。
注意:我将在此处使用 Guzzle 库进行 REST 调用。
1。将图片发送到 Azure 云
protected function yammerFileUpload(string $file, string $filename): int
{
$multipart = [
[
'name' => 'network_id',
'contents' => $this->networkId,
],
[
'name' => 'group_id',
'contents' => $this->groupId,
],
[
'name' => 'target_type',
'contents' => 'GROUP',
],
[
'name' => 'filename',
'contents' => $filename,
],
[
'name' => 'file',
'contents' => $file,
'filename' => $filename,
'headers' => ['Content-Type' => 'image/jpeg']
],
];
$client = new Client();
$options = [
'headers' => [
'Accept' => 'application/json',
'Authorization' => "Bearer $this->yammerToken",
],
'multipart' => $multipart,
];
$response = $client->request('POST', 'https://filesng.yammer.com/v4/uploadSmallFile', $options);
return \json_decode((string)$response->getBody(), true)['id'];
}
当然,你必须用你自己的替换类变量。以及您文件的内容类型。
2。发送您的信息
public function postMessage(string $message, string $file): array
{
$fileId = $this->yammerFileUpload($file, 'my-file.jpg');
$client = new Client();
$options = [
'headers' => [
'Accept' => 'application/json',
'Authorization' => "Bearer $this->token",
],
'form_params' => [
'body' => $message,
'group_id' => $this->groupId,
'attached_objects[]' => "uploaded_file:$fileId",
],
];
$response = $client->request('POST', 'https://www.yammer.com/api/v1/messages.json', $options);
return \json_decode((string)$response->getBody(), true);
}