I am trying Slack API for the first time and I wanted to post a chat message from a Java client. For this purpose I am using the recommended HubSpot Java client and from its examples I am trying the following code excerpts:
import com.hubspot.slack.client.SlackClient;
import com.hubspot.slack.client.SlackClientFactory;
import com.hubspot.slack.client.SlackClientRuntimeConfig;
public class BasicRuntimeConfig {
public static SlackClient getClient() {
return SlackClientFactory.defaultFactory().build(get());
}
public static SlackClientRuntimeConfig get() {
return SlackClientRuntimeConfig.builder()
.setTokenSupplier(() -> "the-token-from-my-slack-app")
.build();
}
}
and:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.hubspot.algebra.Result;
import com.hubspot.slack.client.SlackClient;
import com.hubspot.slack.client.methods.params.chat.ChatPostMessageParams;
import com.hubspot.slack.client.models.response.SlackError;
import com.hubspot.slack.client.models.response.chat.ChatPostMessageResponse;
public class PostAMessage {
private static final Logger LOG = LoggerFactory.getLogger(PostAMessage.class);
public static ChatPostMessageResponse messageChannel(String channelToPostIn, SlackClient slackClient) {
Result<ChatPostMessageResponse, SlackError> postResult = slackClient.postMessage(
ChatPostMessageParams.builder()
.setText("Hello me! Here's a slack message!")
.setChannelId(channelToPostIn)
.build()
).join();
return postResult.unwrapOrElseThrow(); // release failure here as a RTE
}
}
and finally I run:
@Test
public void testSlackChatMessage() {
SlackClient client = BasicRuntimeConfig.getClient();
ChatPostMessageResponse response = messageChannel("general", client);
LOG.info("Got: {}", response);
}
Previously I created a Slack App and used its authentication token to run this example. I was able to have it working via postman, but not using the code above, due to the following error:
java.lang.IllegalStateException: SlackError{type=MISSING_SCOPE, error=missing_scope}
at com.hubspot.algebra.Result.lambda$unwrapOrElseThrow$1(Result.java:68)
at com.hubspot.algebra.Result.lambda$unwrapOrElseThrow$0(Result.java:64)
at java.base/java.util.Optional.orElseThrow(Optional.java:408)
at com.hubspot.algebra.Result.unwrapOrElseThrow(Result.java:60)
at com.hubspot.algebra.Result.unwrapOrElseThrow(Result.java:64)
at com.hubspot.algebra.Result.unwrapOrElseThrow(Result.java:68)
at io.vakt.messaging.poc.slack.PostAMessage.messageChannel(PostAMessage.java:30)
at io.vakt.messaging.poc.slack.PocSlackTest.testSlackChatMessage(PocSlackTest.java:22)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Does anyone know what I am missing? Thank you for your attention.
It took a long time for me to figure this out. The Postfix :user and :bot is represented in the below two sections of the UI.

If you add chat:write under Bot Token Scopes, then you will have chat:write:bot. And if you add chat:write under User Token Scopes, then you will have chat:write:user.
More information on this:
If you read about Perspectival scopes, you can see that you simply have to use chat:write from now onwards.
Perspectival scopes (for example, chat:write:user, chat:write:bot, files:write:user, files:write:bot) are disappearing. Just use chat:write and files:write.
If you get the following error, the given steps
{
"ok": false,
"error": "missing_scope",
"needed": "chat:write:bot"
}
chat:write permission under Bot Token ScopesIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With