use of model.db.Category in project amos-ss17-alexa by c-i-ber.
the class BudgetTrackerService method onIntent.
@Override
public SpeechletResponse onIntent(SpeechletRequestEnvelope<IntentRequest> requestEnvelope) throws SpeechletException {
Intent intent = requestEnvelope.getRequest().getIntent();
String intentName = intent.getName();
LOGGER.info("Intent name: " + intentName);
Session session = requestEnvelope.getSession();
String context = (String) session.getAttribute(DIALOG_CONTEXT);
String withinDialogContext = (String) session.getAttribute(WITHIN_DIALOG_CONTEXT);
if (withinDialogContext == null) {
withinDialogContext = "";
}
if (CATEGORY_LIMIT_INFO_INTENT.equals(intentName)) {
session.setAttribute(DIALOG_CONTEXT, CATEGORY_LIMIT_INFO_INTENT);
return getCategoryLimitInfo(intent);
} else if (PLAIN_CATEGORY_INTENT.equals(intentName)) {
return getCategoryLimitInfo(intent);
} else if (CATEGORY_LIMIT_SET_INTENT.equals(intentName)) {
session.setAttribute(DIALOG_CONTEXT, CATEGORY_LIMIT_SET_INTENT);
return saveSlotValuesAndAskForConfirmation(intent, session);
} else if ((PLAIN_NUMBER_INTENT.equals(intentName) || PLAIN_EURO_INTENT.equals(intentName)) && context != null && context.equals(CATEGORY_LIMIT_SET_INTENT)) {
Map<String, Slot> slots = intent.getSlots();
String newLimit = slots.get(NUMBER_SLOT_KEY) != null ? slots.get(NUMBER_SLOT_KEY).getValue() : null;
if (newLimit == null) {
return getAskResponse(BUDGET_TRACKER, "Das habe ich nicht ganz verstanden. Bitte wiederhole deine Eingabe.");
}
session.setAttribute(CATEGORY_LIMIT, newLimit);
String categoryName = (String) session.getAttribute(CATEGORY);
return askForConfirmation(categoryName, newLimit);
} else if (CATEGORY_SPENDING_INTENT.equals(intentName)) {
session.setAttribute(DIALOG_CONTEXT, intentName);
return saveSpendingAmountForCategory(intent, session, false);
} else if (YES_INTENT.equals(intentName) && context != null && context.equals(CATEGORY_SPENDING_INTENT) && !withinDialogContext.equals("createCategory")) {
return saveSpendingAmountForCategory(intent, session, true);
} else if (YES_INTENT.equals(intentName) && context != null && context.equals(CATEGORY_SPENDING_INTENT) && withinDialogContext.equals("createCategory")) {
String categoryName = (String) session.getAttribute("categoryName");
String amount = (String) session.getAttribute("categoryAmount");
Category category = new Category(categoryName);
DynamoDbMapper.getInstance().save(category);
return getResponse(BUDGET_TRACKER, "Ich habe die Kategorie " + categoryName + " erstellt und " + amount + " Euro hinzugefuegt.");
} else if (YES_INTENT.equals(intentName) && context != null && context.equals(CATEGORY_LIMIT_SET_INTENT)) {
return setCategoryLimit(session);
} else if (NO_INTENT.equals(intentName) && context != null && context.equals(CATEGORY_LIMIT_SET_INTENT)) {
return askForCorrection(session);
} else if (STOP_INTENT.equals(intentName) && context != null && context.equals(CATEGORY_LIMIT_SET_INTENT)) {
return getResponse("Stop", null);
} else if (CATEGORY_STATUS_INFO_INTENT.equals(intentName)) {
session.setAttribute(DIALOG_CONTEXT, intentName);
return getCategoryStatusInfo(intent);
} else {
throw new SpeechletException("Unhandled intent: " + intentName);
}
}
use of model.db.Category in project amos-ss17-alexa by c-i-ber.
the class BudgetTrackerService method getCategoryStatusInfo.
private SpeechletResponse getCategoryStatusInfo(Intent intent) {
Map<String, Slot> slots = intent.getSlots();
Slot categorySlot = slots.get(CATEGORY);
LOGGER.info("Category: " + categorySlot.getValue());
// DynamoDbClient.instance.getItems(Category.TABLE_NAME, Category::new);
List<Category> categories = DynamoDbMapper.getInstance().loadAll(Category.class);
LOGGER.info("All categories: " + categories);
Category category = null;
for (Category cat : categories) {
if (cat.getName().equals(categorySlot.getValue())) {
category = cat;
}
}
if (category != null) {
double spending = category.getSpending();
long percentage = Math.round(spending / category.getLimit() * 100);
double remaining = category.getLimit() - spending > 0 ? category.getLimit() - spending : 0;
String response = "Du hast bereits " + percentage + "% des Limits von " + category.getLimit() + " Euro fuer" + " die Kategorie " + category.getName() + " ausgegeben. " + "Du kannst noch " + remaining + " Euro für diese Kategorie ausgeben.";
return getResponse(BUDGET_TRACKER, response);
} else
return getAskResponse(BUDGET_TRACKER, "Es gibt keine Kategorie mit diesem Namen. Waehle eine andere Kategorie oder" + " erhalte eine Info zu den verfuegbaren Kategorien.");
}
use of model.db.Category in project amos-ss17-alexa by c-i-ber.
the class BudgetTrackerService method getCategoryLimitInfo.
private SpeechletResponse getCategoryLimitInfo(Intent intent) {
Map<String, Slot> slots = intent.getSlots();
Slot categorySlot = slots.get(CATEGORY);
LOGGER.info("Category: " + categorySlot.getValue());
// DynamoDbClient.instance.getItems(Category.TABLE_NAME, Category::new);
List<Category> categories = DynamoDbMapper.getInstance().loadAll(Category.class);
LOGGER.info("All categories: " + categories);
Category category = null;
for (Category cat : categories) {
if (cat.getName().equals(categorySlot.getValue())) {
category = cat;
}
}
if (category != null) {
return getResponse(BUDGET_TRACKER, "Das Limit fuer die Kategorie " + categorySlot.getValue() + " liegt bei " + Double.valueOf(category.getLimit()) + " Euro.");
} else {
return getAskResponse(BUDGET_TRACKER, "Es gibt keine Kategorie mit diesem Namen. Waehle eine andere Kategorie oder" + " erhalte eine Info zu den verfuegbaren Kategorien.");
}
}
use of model.db.Category in project amos-ss17-alexa by c-i-ber.
the class BudgetTrackerService method saveSpendingAmountForCategory.
private SpeechletResponse saveSpendingAmountForCategory(Intent intent, Session session, boolean isConfirmation) {
Map<String, Slot> slots = intent.getSlots();
Slot spendingSlot = slots.get(AMOUNT);
Slot categorySlot = slots.get(CATEGORY);
if (!isConfirmation) {
if (spendingSlot == null) {
return getAskResponse(BUDGET_TRACKER, "Das habe ich nicht ganz verstanden. Bitte wiederhole deine Eingabe");
}
if (categorySlot == null) {
return getAskResponse(BUDGET_TRACKER, "Fuer welche Kategorie soll ich diesen Betrag notieren?");
}
session.setAttribute(AMOUNT, spendingSlot.getValue());
session.setAttribute(CATEGORY, categorySlot.getValue());
}
String spendingAmount = isConfirmation ? (String) session.getAttribute(AMOUNT) : spendingSlot.getValue();
String categoryName = isConfirmation ? (String) session.getAttribute(CATEGORY) : categorySlot.getValue();
// DynamoDbClient.instance.getItems(Category.TABLE_NAME, Category::new);
List<Category> categories = DynamoDbMapper.getInstance().loadAll(Category.class);
Category category = null;
for (Category cat : categories) {
// TODO duplicate. write find and update category method
if (cat.getName().equals(categoryName)) {
category = cat;
}
}
if (category != null) {
// LOGGER.info("Category spending before: " + category.getSpending());
// LOGGER.info("Add spending for category " + spendingAmount);
double newSpending = category.getSpending() + Double.valueOf(spendingAmount);
if (!isConfirmation && newSpending > category.getLimit()) {
// Fallback if the user is about to exceed the limit
return getAskResponse(BUDGET_TRACKER, "Warnung! Durch diese Aktion wirst du das Limit von " + Math.round(category.getLimit()) + " fuer die Kategorie " + categoryName + " ueberschreiten. Soll ich den" + " Betrag trotzdem notieren?");
}
// Create new spending in DB
BudgetManager.instance.createSpending(AccountData.ACCOUNT_DEFAULT, category.getId(), Double.valueOf(spendingAmount));
// DynamoDbClient.instance.putItem(Category.TABLE_NAME, category);
// LOGGER.info("Category spending afterwards: " + category.getSpending());
} else {
// Ask for category creation
session.setAttribute("categoryAmount", spendingAmount);
session.setAttribute("categoryName", categoryName);
session.setAttribute(WITHIN_DIALOG_CONTEXT, "createCategory");
return getAskResponse(BUDGET_TRACKER, "Ich konnte diese Kategorie nicht finden. Soll ich eine Kategorie" + " mit dem Namen " + categoryName + " anlegen und den Betrag hinzufuegen?");
}
String speechResponse = "";
Long spendingPercentage = category.getSpendingPercentage();
LOGGER.info("SpendingPercentage: " + spendingPercentage);
if (spendingPercentage >= 90) {
// TODO SSML
speechResponse = speechResponse + " Warnung! Du hast in diesem Monat bereits " + Math.round(category.getSpending()) + " Euro fuer " + category.getName() + " ausgegeben. Das sind " + spendingPercentage + "% des Limits fuer diese Kategorie.";
}
speechResponse = "Okay. Ich habe " + spendingAmount + " Euro fuer " + categoryName + " notiert." + speechResponse;
return getResponse(BUDGET_TRACKER, speechResponse);
}
use of model.db.Category in project amos-ss17-alexa by c-i-ber.
the class EditCategoriesService method addNewCategory.
/**
* @param intent
* @param session
* @return
*/
private SpeechletResponse addNewCategory(Intent intent, Session session, boolean confirmed) {
String intentName = intent.getName();
LOGGER.info("Intent Name: " + intentName);
if (!confirmed) {
Slot categoryNameSlot = intent.getSlot("CategoryName");
if (contains(categoryNameSlot.getValue()) == true) {
return getResponse(SERVICE_CARD_TITLE, "Diese Kategorie existiert bereits.");
}
session.setAttribute(DIALOG_CONTEXT, ADD_CATEGORY_INTENT);
SessionStorage.getInstance().putObject(session.getSessionId(), SERVICE_CARD_TITLE + ".categoryName", categoryNameSlot.getValue());
return getAskResponse(SERVICE_CARD_TITLE, "Moechtest du die Kategorie " + categoryNameSlot.getValue() + " wirklich erstellen?");
} else {
String slotName = (String) SessionStorage.getInstance().getObject(session.getSessionId(), SERVICE_CARD_TITLE + ".categoryName");
Category item = new Category(slotName);
DynamoDbMapper.getInstance().save(item);
return getResponse(SERVICE_CARD_TITLE, "Verstanden. Die Kategorie " + slotName + " wurde erstellt.");
}
}
Aggregations