Any legacy method through attachment1
etc. fields of the /messages.json
endpoint won't work anymore.
The new method is not so well documented: https://developer.yammer.com/docs/upload-files-into-yammer-groups
I'm giving here an example in PHP below, but you can do the same in any language.
Note: I will use here the Guzzle library for the REST calls.
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'];
}
Of course, you have to replace class variables with your owns. And the content type by your file's one.
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);
}