I used 1.26.1 slack api with filesUpload (version 1) and it worked perfectly, now it sends a warning that I should use filesUploadV2. When I upgraded to new 1.27.1 slack api, I face the problem using filesUploadV2.
Here's documentation to slack api: https://api.slack.com/methods/files.upload
THEY DON'T HAVE DOCUMENTATION FOR FILES UPLOAD V2 BUT THEY HAVE A WARNING TO USE FILES UPLOAD V2
Here's what I did:
fun postFileToSlack(file: File, text: String, fileName: String) {
val uploadFiles = UploadFile.builder()
.file(file)
.filename(fileName)
.title(fileName)
.build()
val request: FilesUploadV2Request = FilesUploadV2Request.builder()
.initialComment(text)
.uploadFiles(mutableListOf(uploadFiles))
.channel(slackBotProperties.channelName)
.build()
val result = methods.filesUploadV2(request)
}
I receive an error (completely undescriptive imo):
com.slack.api.methods.SlackFilesUploadV2Exception: null
at com.slack.api.methods.impl.FilesUploadV2Helper.<init>(FilesUploadV2Helper.java:33)
at com.slack.api.methods.impl.MethodsClientImpl.filesUploadV2(MethodsClientImpl.java:1969)
at project.SlackService.postFileToSlack(SlackService.kt:76)
at project.AnotherService.function(AnotherService.kt:64)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1540)
for example what was working in filesUpload (v1):
fun postFileToSlack(file: File, text: String) {
val request: FilesUploadRequest = FilesUploadRequest.builder()
.file(file)
.filename("file")
.initialComment(text)
.channels(mutableListOf(slackBotProperties.channelName))
.build()
methods.filesUpload(request)
}
Thanks in advance.
I was getting this error too, the problem was with the permissions the bot had for the v2 file upload. Having a look into the changelog, found this:
When migrating to the v2 method, please note that the new method requires both files:write and files:read scopes. If your existing apps have only files:write scope for uploading files, you need to add files:read to the scopes plus re-install the app to issue an updated token.
You just need to add the new scopes to the bot and install it again on the corresponding channels. Once done, you won't get that error. You can also check on the changelog an example of how to use the new methods.