I'm currently implementing a proactive messaging bot, and as part of the configuration, I need to synchronously list the channels available to the bot. The TeamsInfo.getTeamChannels()
method requires a TurnContext
, which we don't have since it's a proactive-only bot.
So far, I've been trying to get a TurnContext
using the continueConversation()
method. The problem with this method is that I can't get it to work synchronously. It takes a callback by implementing BotCallbackHandler
, but the method call itself only returns CompletableFuture<Void>
. So even if I wait on the CompletableFuture
using get()
, I don't actually get the TurnContext
when it completes.
What I've identified as a workaround is having my callback class set an instance field for the TurnContext
when the invoke()
method is called:
public class MyBotCallback implements BotCallbackHandler {
private TurnContext turnContext;
@Override
public CompletableFuture<Void> invoke(TurnContext turnContext) {
this.turnContext = turnContext;
CompletableFuture<Void> cf = new CompletableFuture<Void>();
cf.complete(null);
return cf;
}
public TurnContext getTurnContext() {
return turnContext;
}
}
Then, I can call get()
on the CompletableFuture<Void>
returned by continueConversation()
to synchronously wait until the callback has executed and the instance field has been set:
MyBotCallback callback = new MyBotCallback();
CompletableFuture<Void> cf = bot.continueConversation(appId, cr, callback);
cf.get();
TurnContext tc = callback.getTurnContext();
I don't have much familiarity with asynchronous processing in Java and the CompletableFuture
paradigm, so maybe there's a better way to do this. But this seems like a complex workaround, so I'm not sure why continueConversation
wouldn't return CompletableFuture<TurnContext>
instead. I'm also open to other ways of getting a TurnContext
aside from continueConversation()
.
question from:
https://stackoverflow.com/questions/66051656/synchronously-get-teams-channel-list-using-java-bot-sdk 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…