Ask Your Question
2

How can Jakarta.servlet.http.HttpServlet be utilized as the Bot Endpoint for the Bot Framework SDK in Java?

asked 2023-06-07 05:55:19 +0000

ladyg gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2023-06-07 05:56:02 +0000

plato gravatar image

To utilize Jakarta.servlet.http.HttpServlet as the Bot Endpoint for the Bot Framework SDK in Java, follow these steps:

  1. Create a new empty web project in your IDE of choice.
  2. 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>
    
  3. 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);
       }
    }
    
  4. 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.

  5. In the doPost() method, use the BotFrameworkAdapter to process the incoming activity and call your implementation of the onTurn() method.
  6. 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);
    }
    
  7. Deploy the servlet to a web server, such as Apache Tomcat or Jetty.

  8. Register your bot with the Bot Framework Registrar service.
  9. Configure your bot's endpoint URL to point to your deployed servlet URL.
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-06-07 05:55:19 +0000

Seen: 16 times

Last updated: Jun 07 '23