【发布时间】:2019-11-27 14:00:26
【问题描述】:
我是第一次尝试Slack API,我想从 Java 客户端发布一条聊天消息。为此,我使用推荐的HubSpot Java client 和its examples 我正在尝试以下代码摘录:
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();
}
}
和:
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
}
}
最后我跑了:
@Test
public void testSlackChatMessage() {
SlackClient client = BasicRuntimeConfig.getClient();
ChatPostMessageResponse response = messageChannel("general", client);
LOG.info("Got: {}", response);
}
之前我创建了一个 Slack 应用程序并使用它的身份验证令牌来运行这个示例。由于以下错误,我能够通过postman 让它工作,但没有使用上面的代码:
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)
有人知道我错过了什么吗?感谢您的关注。
【问题讨论】: