Skip to content

Commit 9d2c3fe

Browse files
committed
Add preview image replacement example
1 parent 5dceba1 commit 9d2c3fe

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

slack-api-client/src/test/java/test_with_remote_apis/methods/files_remote_Test.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package test_with_remote_apis.methods;
22

33
import com.slack.api.Slack;
4+
import com.slack.api.methods.MethodsClient;
45
import com.slack.api.methods.SlackApiException;
56
import com.slack.api.methods.response.chat.ChatDeleteResponse;
67
import com.slack.api.methods.response.chat.ChatPostMessageResponse;
8+
import com.slack.api.methods.response.chat.ChatUpdateResponse;
79
import com.slack.api.methods.response.files.remote.*;
810
import com.slack.api.methods.response.search.SearchFilesResponse;
911
import com.slack.api.model.File;
@@ -21,8 +23,12 @@
2123
import java.io.InputStream;
2224
import java.net.URL;
2325
import java.net.URLConnection;
26+
import java.nio.file.Files;
2427
import java.util.Arrays;
28+
import java.util.UUID;
2529

30+
import static com.slack.api.model.block.Blocks.*;
31+
import static com.slack.api.model.block.composition.BlockCompositions.plainText;
2632
import static org.hamcrest.CoreMatchers.is;
2733
import static org.hamcrest.CoreMatchers.nullValue;
2834
import static org.hamcrest.MatcherAssert.assertThat;
@@ -238,4 +244,57 @@ public void updateAndShare_searchable() throws Exception {
238244
log.info("Searchable file contents took {} milliseconds to get indexed", millis);
239245
}
240246

247+
@Test
248+
public void testPreviewImageReplacement() throws Exception {
249+
loadRandomChannel();
250+
251+
MethodsClient client = slack.methods(botToken);
252+
String externalUrl = "https://www.example.com/test-image";
253+
254+
String firstExternalId = "remote-file-slack-logo-" + UUID.randomUUID();
255+
byte[] firstPreviewImage = Files.readAllBytes(new java.io.File("src/test/resources/seratch.jpg").toPath());
256+
FilesRemoteAddResponse firstFileCreation = client.filesRemoteAdd(r -> r
257+
.externalId(firstExternalId)
258+
.externalUrl(externalUrl)
259+
.title("Some image")
260+
.previewImage(firstPreviewImage)
261+
);
262+
assertThat(firstFileCreation.getError(), is(nullValue()));
263+
264+
ChatPostMessageResponse newMessage = client.chatPostMessage(r -> r
265+
.channel(randomChannelId)
266+
.text("This is v1")
267+
.blocks(asBlocks(
268+
section(s -> s.text(plainText("This is v1"))),
269+
file(f -> f.externalId(firstExternalId).source("remote"))
270+
))
271+
);
272+
assertThat(newMessage.getError(), is(nullValue()));
273+
274+
// For humans
275+
Thread.sleep(2000);
276+
277+
String secondExternalId = "remote-file-slack-logo-" + UUID.randomUUID();
278+
byte[] secondPreviewImage = Files.readAllBytes(new java.io.File("src/test/resources/slack-logo.gif").toPath());
279+
FilesRemoteAddResponse secondFileCreation = client.filesRemoteAdd(r -> r
280+
.externalId(secondExternalId)
281+
.externalUrl(externalUrl)
282+
.title("Some image")
283+
.previewImage(secondPreviewImage)
284+
);
285+
assertThat(secondFileCreation.getError(), is(nullValue()));
286+
287+
ChatUpdateResponse messageModification = client.chatUpdate(r -> r
288+
.channel(randomChannelId)
289+
.ts(newMessage.getTs())
290+
.text("This is v2")
291+
.blocks(asBlocks(
292+
section(s -> s.text(plainText("This is v2"))),
293+
file(f -> f.externalId(secondExternalId).source("remote"))
294+
))
295+
);
296+
assertThat(messageModification.getError(), is(nullValue()));
297+
298+
}
299+
241300
}

0 commit comments

Comments
 (0)