如何通过 Azure 上传小文件 API 在 Yammer API 上上传文件

How to upload files on Yammer API via the Azure Upload Small File API

如何将附件与 Yammer 消息一起上传?

通过 attachment1/messages.json 端点字段的任何旧方法将不再有效。

新方法没有很好的记录:https://developer.yammer.com/docs/upload-files-into-yammer-groups

我在下面的 PHP 中给出了一个例子,但你可以用任何语言做同样的事情。

你必须分两部分完成

  1. 先上传图片到https://filesng.yammer.com/v4/uploadSmallFile,获取图片id
  2. 像往常一样将您的消息连同新获得的图片 ID 一起发送至 https://www.yammer.com/api/v1/messages.json

注意:我将在此处使用 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'];
}

当然,您必须将 class 个变量替换为您自己的变量。 内容类型 与您的文件相同。

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);
}