使用Java的Slack SDK上传文件到Slack。

huangapple go评论91阅读模式
英文:

Upload file to Slack using the slack sdk for java

问题

我试图使用 slack sdk for javafile.upload method 将文件上传到 Slack。

我尝试过使用 .file() 和 .fileData(),但在控制台中没有看到任何错误信息,事实上,在调用这些方法后似乎没有任何打印输出。只有在使用 content("something") 方法时,我才会收到 "All ok" 消息以及 Slack 中的消息。

以下是我的代码:

File report = new File(System.getProperty("user.dir") + "\\report.zip");

if (!report.exists()) {
    System.out.println("Report Zip Doesnt Exist");
    return false;
}

FilesUploadRequest request = FilesUploadRequest.builder()
        .channels(channels)
        .initialComment(comment)
        .filename(report.getName())
        //.file(report) 不起作用
        //.fileData(FileUtils.readFileToByteArray(report)) 不起作用
        .content("something") // 有效
        .title("Report")
        .filetype("zip")
        .build();

try {
    FilesUploadResponse response = methods.filesUpload(request);
    if (response.isOk()) {
        System.out.println("All ok");
        return true;
    } else {
        System.out.println(response.getError());
        return false;
    }
} catch (SlackApiException requestFailure) {
    System.out.println("Request failed");
    return false;
} catch (IOException connectivityIssue) {
    System.out.println("Connectivity issue");
    return false;
}
英文:

I'm trying to upload a file to slack using the slack sdk for java and the file.upload method

I've tried using both .file() and .fileData() and I'm getting no errors in the console in fact nothing seems to print after either of these methods are called. I only get "All ok" and the message in slack when using the content("something") method.

Here is my code:

	File report = new File(System.getProperty("user.dir") + "\\report.zip");
	
	if(!report.exists()) {
		System.out.println("Report Zip Doesnt Exist");
		return false;
	}
	
	FilesUploadRequest request = FilesUploadRequest.builder()
			.channels(channels)
			.initialComment(comment)
			.filename(report.getName())
			//.file(report) doesnt work
			//.fileData(FileUtils.readFileToByteArray(report)) doesnt work
			.content("something") // works
			.title("Report")
			.filetype("zip")
			.build();

	try {
		FilesUploadResponse response = methods.filesUpload(request);
		if (response.isOk()) {
			System.out.println("All ok");
			return true;
		} else {
			System.out.println(response.getError());
			return false;
		}
	} catch (SlackApiException requestFailure) {
		System.out.println("Request failed");
		return false;
	} catch (IOException connectivityIssue) {
		System.out.println("Connectivity issue");
		return false;
	}

答案1

得分: 2

以下是翻译好的部分:

有一种方法
在 Slack 中创建您的应用程序 ->
转到添加功能和功能 -> 转到权限 ->
转到范围 -> 机器人令牌范围 -> 添加具有 files.write 的 OAuth 范围 -> 生成令牌

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
headers.setBearerAuth(token); // 在此处传递生成的令牌
MultiValueMap<String, Object> bodyMap = new LinkedMultiValueMap<>();
bodyMap.add("file", bos.toByteArray(), fileName)); // 将文件转换为 ByteArrayOutputStream 并使用 toByteArray() 传递,或者使用 new File() 传递
bodyMap.add("initial_comment", message); // 与文件一起传递评论
bodyMap.add("channels", "XXXXXX"); // 传递频道代码ID
HttpEntity<MultiValueMap<String, Object>> entity = new HttpEntity<>(bodyMap, headers);
ResponseEntity<Object> responseEntity = restTemplate.postForEntity("https://slack.com/api/files.upload", entity,
            Object.class);
System.out.println("sendEmailWithAttachment() 响应状态:"+ responseEntity.getStatusCode().toString());
英文:

There is a way

Create your app in slack ->
Go to Add features and functionality -> go to permission ->
Go to scopes -> Bot Token Scopes -> Add Oauth scope with files.write -> token generated

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
headers.setBearerAuth(token); // pass generated token here
MultiValueMap<String, Object> bodyMap = new LinkedMultiValueMap<>();
bodyMap.add("file", bos.toByteArray(),fileName )); // convert file to ByteArrayOutputStream and pass with toByteArray() or pass with new File() 
bodyMap.add("initial_comment", message); // pass comments with file 
bodyMap.add("channels", "XXXXXX"); // pass channel codeID
HttpEntity<MultiValueMap<String, Object>> entity = new HttpEntity<>(bodyMap, headers);
ResponseEntity<Object> responseEntity = restTemplate.postForEntity("https://slack.com/api/files.upload", entity,
            Object.class);
System.out.println("sendEmailWithAttachment() response status: "+ responseEntity.getStatusCode().toString());

huangapple
  • 本文由 发表于 2020年7月28日 10:21:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/63126117.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定