Search in sources :

Example 1 with Argument

use of com.google.api.services.actions_fulfillment.v2.model.Argument in project dialogflow-transactions-java by actions-on-google.

the class TransactionsApp method transactionCheckComplete.

// Check result of transaction requirements
@ForIntent("Transaction Check Complete")
public ActionResponse transactionCheckComplete(ActionRequest request) {
    LOGGER.info("Checking Transaction Requirements Result.");
    ResourceBundle rb = ResourceBundle.getBundle("resources", request.getLocale());
    // Check result of transaction requirements check
    Argument transactionCheckResult = request.getArgument("TRANSACTION_REQUIREMENTS_CHECK_RESULT");
    boolean result = false;
    if (transactionCheckResult != null) {
        Map<String, Object> map = transactionCheckResult.getExtension();
        if (map != null) {
            String resultType = (String) map.get("resultType");
            result = resultType != null && resultType.equals("CAN_TRANSACT");
        }
    }
    ResponseBuilder responseBuilder = getResponseBuilder(request);
    if (result) {
        responseBuilder.add(rb.getString("get_delivery_address")).addSuggestions(new String[] { "get delivery address" });
    } else {
        responseBuilder.add(rb.getString("transaction_failed"));
    }
    return responseBuilder.build();
}
Also used : Argument(com.google.api.services.actions_fulfillment.v2.model.Argument) ResourceBundle(java.util.ResourceBundle) JsonObject(com.google.gson.JsonObject) JSONObject(org.json.simple.JSONObject) ResponseBuilder(com.google.actions.api.response.ResponseBuilder) ForIntent(com.google.actions.api.ForIntent)

Example 2 with Argument

use of com.google.api.services.actions_fulfillment.v2.model.Argument in project dialogflow-transactions-java by actions-on-google.

the class TransactionsApp method deliveryAddressComplete.

// Verifies delivery address and caches it for later use
@ForIntent("Delivery Address Complete")
public ActionResponse deliveryAddressComplete(ActionRequest request) {
    ResourceBundle rb = ResourceBundle.getBundle("resources", request.getLocale());
    // Check delivery address value
    Argument deliveryAddressValue = request.getArgument("DELIVERY_ADDRESS_VALUE");
    Location deliveryAddress = null;
    if (deliveryAddressValue != null) {
        Map<String, Object> map = deliveryAddressValue.getExtension();
        if (map != null) {
            String userDecision = (String) map.get("userDecision");
            Location location = (Location) map.get("location");
            deliveryAddress = userDecision != null && userDecision.equals("ACCEPTED") ? location : null;
        }
    }
    ResponseBuilder responseBuilder = getResponseBuilder(request);
    if (deliveryAddress != null) {
        // Cache delivery address in conversation data for later use
        Map<String, Object> conversationData = request.getConversationData();
        conversationData.put("location", GSON_BUILDER.create().toJson(deliveryAddress, Location.class));
        responseBuilder.add(rb.getString("confirm_transaction")).addSuggestions(new String[] { "confirm transaction" });
    } else {
        responseBuilder.add(rb.getString("delivery_address_failed"));
    }
    return responseBuilder.build();
}
Also used : Argument(com.google.api.services.actions_fulfillment.v2.model.Argument) ResourceBundle(java.util.ResourceBundle) JsonObject(com.google.gson.JsonObject) JSONObject(org.json.simple.JSONObject) ResponseBuilder(com.google.actions.api.response.ResponseBuilder) Location(com.google.api.services.actions_fulfillment.v2.model.Location) ForIntent(com.google.actions.api.ForIntent)

Example 3 with Argument

use of com.google.api.services.actions_fulfillment.v2.model.Argument in project dialogflow-transactions-java by actions-on-google.

the class TransactionsApp method transactionDecisionComplete.

// Check result of asking to perform transaction / place order
@ForIntent("Transaction Decision Complete")
public ActionResponse transactionDecisionComplete(ActionRequest request) {
    ResourceBundle rb = ResourceBundle.getBundle("resources", request.getLocale());
    // Check transaction decision value
    Argument transactionDecisionValue = request.getArgument("TRANSACTION_DECISION_VALUE");
    Map<String, Object> extension = null;
    if (transactionDecisionValue != null) {
        extension = transactionDecisionValue.getExtension();
    }
    String transactionDecision = null;
    if (extension != null) {
        transactionDecision = (String) extension.get("transactionDecision");
    }
    ResponseBuilder responseBuilder = getResponseBuilder(request);
    if ((transactionDecision != null && transactionDecision.equals("ORDER_ACCEPTED"))) {
        OrderV3 order = ((OrderV3) extension.get("order"));
        order.setLastUpdateTime(Instant.now().toString());
        // Update order status
        PurchaseOrderExtension purchaseOrderExtension = order.getPurchase();
        purchaseOrderExtension.setStatus("CONFIRMED");
        purchaseOrderExtension.setUserVisibleStatusLabel("Order confirmed");
        order.setPurchase(purchaseOrderExtension);
        // Order update
        OrderUpdateV3 orderUpdate = new OrderUpdateV3().setType("SNAPSHOT").setReason("Reason string").setOrder(order);
        Map<String, Object> conversationData = request.getConversationData();
        String orderId = (String) conversationData.get("UNIQUE_ORDER_ID");
        String response = MessageFormat.format(rb.getString("transaction_decision_result_success"), orderId);
        responseBuilder.add(response).add(new StructuredResponse().setOrderUpdateV3(orderUpdate));
    } else {
        responseBuilder.add(rb.getString("transaction_failed"));
    }
    return responseBuilder.endConversation().build();
}
Also used : StructuredResponse(com.google.api.services.actions_fulfillment.v2.model.StructuredResponse) Argument(com.google.api.services.actions_fulfillment.v2.model.Argument) OrderUpdateV3(com.google.api.services.actions_fulfillment.v2.model.OrderUpdateV3) ResourceBundle(java.util.ResourceBundle) JsonObject(com.google.gson.JsonObject) JSONObject(org.json.simple.JSONObject) ResponseBuilder(com.google.actions.api.response.ResponseBuilder) OrderV3(com.google.api.services.actions_fulfillment.v2.model.OrderV3) PurchaseOrderExtension(com.google.api.services.actions_fulfillment.v2.model.PurchaseOrderExtension) ForIntent(com.google.actions.api.ForIntent)

Aggregations

ForIntent (com.google.actions.api.ForIntent)3 ResponseBuilder (com.google.actions.api.response.ResponseBuilder)3 Argument (com.google.api.services.actions_fulfillment.v2.model.Argument)3 JsonObject (com.google.gson.JsonObject)3 ResourceBundle (java.util.ResourceBundle)3 JSONObject (org.json.simple.JSONObject)3 Location (com.google.api.services.actions_fulfillment.v2.model.Location)1 OrderUpdateV3 (com.google.api.services.actions_fulfillment.v2.model.OrderUpdateV3)1 OrderV3 (com.google.api.services.actions_fulfillment.v2.model.OrderV3)1 PurchaseOrderExtension (com.google.api.services.actions_fulfillment.v2.model.PurchaseOrderExtension)1 StructuredResponse (com.google.api.services.actions_fulfillment.v2.model.StructuredResponse)1