Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
691 views
in Technique[技术] by (71.8m points)

botframework - Synchronously get Teams channel list using Java Bot SDK

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...