1

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

attachment1通过端点的 etc. 字段的 任何遗留方法/messages.json将不再起作用。

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

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

4

1 回答 1

1

你必须分两部分做

  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'];
}

当然,你必须用你自己的替换类变量。以及您文件的内容类型

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);
}
于 2021-02-05T12:05:10.123 回答