1 | initial version |
To utilize Jakarta.servlet.http.HttpServlet as the Bot Endpoint for the Bot Framework SDK in Java, follow these steps:
Add the required dependencies for the Bot Framework SDK and Servlet API to your project.
<dependencies>
<!-- Bot Framework SDK -->
<dependency>
<groupId>com.microsoft.bot</groupId>
<artifactId>bot-core</artifactId>
<version>4.12.2</version>
</dependency>
<!-- Servlet API -->
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>4.0.3</version>
<scope>provided</scope>
</dependency>
</dependencies>
Create a new class that extends HttpServlet and implements the Bot interface.
import com.microsoft.bot.builder.Bot;
import com.microsoft.bot.builder.BotFrameworkAdapter;
import com.microsoft.bot.builder.ConversationState;
import com.microsoft.bot.builder.MemoryStorage;
import com.microsoft.bot.builder.TurnContext;
import com.microsoft.bot.connector.authentication.MicrosoftAppCredentials;
import com.microsoft.bot.integration.Configuration;
import com.microsoft.bot.integration.RuntimeSettings;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.concurrent.CompletableFuture;
public class BotServlet extends HttpServlet implements Bot {
private BotFrameworkAdapter adapter;
private Configuration configuration;
private ConversationState conversationState;
private MicrosoftAppCredentials credentials;
@Override
public void init() throws ServletException {
super.init();
configuration = new Configuration();
adapter = new BotFrameworkAdapter(
configuration.getProperty(RuntimeSettings.APP_ID),
configuration.getProperty(RuntimeSettings.APP_PASSWORD)
);
conversationState = new ConversationState(new MemoryStorage());
credentials = new MicrosoftAppCredentials(
configuration.getProperty(RuntimeSettings.APP_ID),
configuration.getProperty(RuntimeSettings.APP_PASSWORD)
);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
CompletableFuture<Void> future = adapter.processActivity(req, resp, (TurnContext turnContext) -> {
return Bot.onTurn(turnContext, this::onTurn).thenApply(result -> null);
});
future.exceptionally(ex -> {
ex.printStackTrace();
return null;
});
}
private CompletableFuture<Void> onTurn(TurnContext turnContext) {
return CompletableFuture.completedFuture(null);
}
}
In the init() method, create a new instance of BotFrameworkAdapter, ConversationState, and MicrosoftAppCredentials. The values for APPID and APPPASSWORD should be retrieved from your Bot Framework registration.
Implement your bot logic in the onTurn() method.
private CompletableFuture<Void> onTurn(TurnContext turnContext) {
if (turnContext.getActivity().getType().equals(ActivityTypes.MESSAGE)) {
String message = turnContext.getActivity().getText();
Activity reply = MessageFactory.text("You said: " + message);
return turnContext.sendActivity(reply).thenApply(result -> null);
}
return CompletableFuture.completedFuture(null);
}
Deploy the servlet to a web server, such as Apache Tomcat or Jetty.