Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to register a callback for my chatbot to perform time-scheduled tasks?
#1
The callback functionality has been introduced with BBP release R2018 FP1832 to allow the execution of time-scheduled tasks.

If your objective is a callback functionality for your chatbot which is running on the platform, you can easly do this using the existing API provided by the Business Bot Platform.

In your chatbot, you have to register a callback from the Business Bot platform using the following function:

Code:
    public boolean registerCallback(MobileUserAccount mobileUserAccount) {
        String cronSchedule = "0 0/1 * * * ? *";
        String callbackMessage = "reminder-xy";
        ChatbotCallback chatbotCallback = new ChatbotCallback(Integer.parseInt(pluginMap.get(ConstantsPlugin.BOTID)), Integer.parseInt(pluginMap.get(ConstantsPlugin.PLUGIN_ID)), mobileUserAccount.getId(), cronSchedule, callbackMessage);
        
        // Register chatbot callback to BBP webservices
        String receiptId = null;
        try {
            WebService ws = new WebService(pluginMap);
            receiptId = ws.registerChatbotCallback(chatbotCallback);
        } catch (Exception e) {
            logger.error("Unable to register chatbot callback ["+chatbotCallback.getCallbackMessage()+"] to BBP webservices: "+e.getMessage());
            return false;
        }
        if (receiptId != null && receiptId.length() != 0) {
            logger.info("Chatbot callback ["+chatbotCallback.getCallbackMessage()+"] has been registered to BBP webservices successfully. Receipt Id: ["+receiptId+"]");
        } else {
            logger.error("Failed to register chatbot callback ["+chatbotCallback.getCallbackMessage()+"] to BBP webservices");
            return false;
        }
        return true;
    }


If your have registered the callback via web services, then the Business Bot Platform will call your chatbot as defined. Following function in your chatbot is called in order to do further activities:


Code:
        public String handleIncomingCallback(String tenantId, String accountId, String message, String jsonMobileUserAccount) {
        logger.info("handleIncomingCallback() called");
        
        JSONObject jsonObjectMobileUserAccount = new JSONObject(jsonMobileUserAccount);
        MobileUserAccount mobileUserAccount = new MobileUserAccount(jsonObjectMobileUserAccount);
        
        String userId = Utilities.getMobileAppSettingValue(Utilities.getMobileAppSettingParameterNameUserId(tenantId, accountId, pluginMap), mobileUserAccount.getPrimaryMobileDevice().getPrimaryMobileApp().getMobileAppSettings());
        if (null != userId) {
            logger.info("User identification ["+Utilities.getMobileAppSettingParameterNameUserId(tenantId, accountId, pluginMap)+"] has been found: ["+userId+"]");            
        } else {
            logger.error("User identification ["+Utilities.getMobileAppSettingParameterNameUserId(tenantId, accountId, pluginMap)+"] not found for mobile user ["+mobileUserAccount.getUsername()+"]");
            return null;
        }
        
        PluginReturnMessage pluginReturnMessage = new PluginReturnMessage(null, "Callback message: ["+message+"]", null);
        return pluginReturnMessage.toJson().toString();
    }



If you want to stop the scheduled chatbot callback, then you have to unregister the callback from the Business Bot Platform via webservices:


Code:
    public boolean unregisterCallback(int id) {
        // Unregister chatbot callback from BBP webservices
        try {
            WebService ws = new WebService(pluginMap);
            if (ws.unregisterChatbotCallback(id)) {
                logger.info("Chatbot callback id ["+id+"] has been unregistered from BBP webservices successfully.");
            } else {
                logger.error("Failed to unregister chatbot callback id ["+id+"] from BBP webservices");
                return false;
            }
        } catch (Exception e) {
            logger.error("Unable to unregister chatbot callback id ["+id+"] from BBP webservices: "+e.getMessage());
            return false;
        }
        return true;
    }
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)