Search in sources :

Example 1 with Contact

use of model.db.Contact in project amos-ss17-alexa by c-i-ber.

the class AmosAlexaSimpleTestImpl method contactTransferTest.

@Test
public void contactTransferTest() throws Exception {
    ContactTransferService.contactTable = Contact.TABLE_NAME + "_test";
    List<Category> categories = DynamoDbMapper.getInstance().loadAll(Category.class);
    Category category = categories.get(0);
    System.out.println("name:" + category.getName());
    // Empty test contacts table
    DynamoDbMapper.getInstance().dropTable(Contact.class);
    DynamoDbMapper.getInstance().createTable(Contact.class);
    DynamoDbMapper.getInstance().save(new Contact("Bob Marley", "UK1"));
    DynamoDbMapper.getInstance().save(new Contact("Bob Ray Simmons", "UK2"));
    DynamoDbMapper.getInstance().save(new Contact("Lucas", "DE1"));
    DynamoDbMapper.getInstance().save(new Contact("Sandra", "DE2"));
    newSession();
    testIntentMatches("ContactTransferIntent", "Contact:sandra", "Amount:1", "Dein aktueller Kontostand beträgt ([0-9\\.]+) Euro\\. Möchtest du 1\\.0 Euro an Sandra überweisen\\?");
    testIntentMatches("AMAZON.YesIntent", "Erfolgreich\\. 1\\.0 Euro wurden an Sandra überwiesen\\. Dein neuer Kontostand beträgt ([0-9\\.]+) Euro\\. " + "Zu welcher Kategorie soll die Transaktion hinzugefügt werden. Sag zum Beispiel Kategorie " + Category.categoryListText());
    double before = category.getSpending();
    testIntentMatches("PlainCategoryIntent", "Category:" + category.getName(), "Verstanden. Die Transaktion wurde zur Kategorie " + category.getName() + " hinzugefügt");
    double after = category.getSpending();
    assertTrue("Category spending amount did not increase.", after > before);
    newSession();
    testIntentMatches("ContactTransferIntent", "Contact:bob", "Amount:1", "Ich habe 2 passende Kontakte gefunden\\. Bitte wähle einen aus: Kontakt Nummer 1: Bob ([A-Za-z ]+)\\. Kontakt Nummer 2: Bob ([A-Za-z ]+)\\. ");
    testIntentMatches("ContactChoiceIntent", "ContactIndex:1", "Dein aktueller Kontostand beträgt ([0-9\\.]+) Euro\\. Möchtest du 1\\.0 Euro an Bob ([A-Za-z ]+) überweisen\\?");
    testIntent("AMAZON.NoIntent", "Okay, verstanden. Dann bis zum nächsten Mal.");
}
Also used : Category(model.db.Category) Contact(model.db.Contact) Test(org.junit.Test)

Example 2 with Contact

use of model.db.Contact in project amos-ss17-alexa by c-i-ber.

the class ContactService method deleteContact.

private SpeechletResponse deleteContact(Intent intent, Session session, boolean confirmed) {
    if (!confirmed) {
        String contactIdStr = intent.getSlot("ContactID").getValue();
        if (contactIdStr == null || contactIdStr.equals("")) {
            return getResponse(CONTACTS, "Das habe ich nicht verstanden. Nuschel nicht so!");
        } else {
            int contactId = Integer.parseInt(contactIdStr);
            session.setAttribute(CONTACTS + ".delete", contactId);
            return getAskResponse(CONTACTS, "Moechtest du Kontakt Nummer " + contactId + " wirklich loeschen?");
        }
    } else {
        Integer contactId = (Integer) session.getAttribute(CONTACTS + ".delete");
        if (contactId != null) {
            Contact contact = new Contact(contactId);
            DynamoDbMapper.getInstance().delete(contact);
            return getResponse(CONTACTS, "Kontakt wurde geloescht.");
        }
    }
    return null;
}
Also used : Contact(model.db.Contact)

Example 3 with Contact

use of model.db.Contact in project amos-ss17-alexa by c-i-ber.

the class ContactService method readContacts.

private SpeechletResponse readContacts(Session session, int offset, int limit) {
    List<Contact> contactList = DynamoDbMapper.getInstance().loadAll(Contact.class);
    List<Contact> contacts = new ArrayList<>(contactList);
    LOGGER.info("Contacts: " + contacts);
    if (contacts.size() == 0) {
        return getResponse(CONTACTS, "Du hast keine Kontakte in deiner Kontaktliste.");
    }
    if (offset >= contacts.size()) {
        session.setAttribute(CONTACTS + ".offset", null);
        return getResponse(CONTACTS, "Keine weiteren Kontakte.");
    }
    if (offset + limit >= contacts.size()) {
        limit = contacts.size() - offset;
    }
    Collections.sort(contacts);
    StringBuilder response = new StringBuilder();
    response.append("<speak>");
    for (int i = offset; i < offset + limit; i++) {
        Contact contact = contacts.get(i);
        response.append("Kontakt ").append(contact.getId()).append(": ");
        response.append("Name: ").append(contact.getName()).append(", IBAN: ").append(DialogUtil.getIbanSsmlOutput(contact.getIban())).append(". ");
    }
    boolean isAskResponse = contacts.size() > offset + limit;
    if (isAskResponse) {
        response.append("Weitere Kontakte vorlesen?");
        response.append("</speak>");
        session.setAttribute(CONTACTS + ".offset", offset + limit);
        SsmlOutputSpeech speech = new SsmlOutputSpeech();
        speech.setSsml(response.toString());
        Reprompt reprompt = new Reprompt();
        reprompt.setOutputSpeech(speech);
        return SpeechletResponse.newAskResponse(speech, reprompt);
    } else {
        response.append("Keine weiteren Kontakte.");
        response.append("</speak>");
        session.setAttribute(CONTACTS + ".offset", null);
        SsmlOutputSpeech speech = new SsmlOutputSpeech();
        speech.setSsml(response.toString());
        return SpeechletResponse.newTellResponse(speech);
    }
}
Also used : Reprompt(com.amazon.speech.ui.Reprompt) SsmlOutputSpeech(com.amazon.speech.ui.SsmlOutputSpeech) Contact(model.db.Contact)

Example 4 with Contact

use of model.db.Contact in project amos-ss17-alexa by c-i-ber.

the class ContactTransferService method contactTransfer.

/**
 * Lists possible contact or directly asks for confirmation.
 */
private SpeechletResponse contactTransfer(Intent intent, Session session) {
    // Try to get the contact name
    String contactName = intent.getSlot("Contact").getValue();
    if (contactName == null || contactName.equals("")) {
        return getResponse(CONTACT_TRANSFER_CARD, "Ich konnte den Namen des Kontakts nicht verstehen. Versuche es noch einmal.");
    }
    log.info("ContactName: " + contactName);
    contactName = contactName.toLowerCase();
    // Try to get the amount
    double amount;
    try {
        amount = Double.parseDouble(intent.getSlot("Amount").getValue());
    } catch (NumberFormatException ignored) {
        return getResponse(CONTACT_TRANSFER_CARD, "Ich konnte den Betrag nicht verstehen. Versuche es noch einmal.");
    }
    session.setAttribute(SESSION_PREFIX + ".amount", amount);
    // Query database
    List<Contact> contacts = DynamoDbMapper.getInstance().loadAll(Contact.class);
    List<Contact> contactsFound = new LinkedList<>();
    for (Contact contact : contacts) {
        String thisContactName = contact.getName().toLowerCase();
        log.info("Contacts in DB: " + thisContactName);
        int dist = StringUtils.getLevenshteinDistance(thisContactName, contactName);
        if (dist < contactName.length() / 2) {
            contactsFound.add(contact);
        } else {
            if (thisContactName.startsWith(contactName)) {
                contactsFound.add(contact);
            } else if (thisContactName.contains(contactName)) {
                contactsFound.add(contact);
            } else {
                int middle = thisContactName.length() / 2;
                String firstHalf = thisContactName.substring(0, middle);
                String secondHalf = thisContactName.substring(middle);
                dist = Math.min(StringUtils.getLevenshteinDistance(firstHalf, contactName), StringUtils.getLevenshteinDistance(secondHalf, contactName));
                if (dist < middle / 1.5) {
                    contactsFound.add(contact);
                }
            }
        }
    }
    if (contactsFound.size() == 0) {
        return getResponse(CONTACT_TRANSFER_CARD, "Ich konnte keinen Kontakt finden mit dem Namen: " + contactName);
    }
    SESSION_STORAGE.putObject(session.getSessionId(), SESSION_PREFIX + ".contactsFound", contactsFound);
    if (contactsFound.size() == 1) {
        session.setAttribute(SESSION_PREFIX + ".choice", 1);
        return confirm(session);
    }
    StringBuilder contactListString = new StringBuilder();
    contactListString.append("Ich habe ").append(contactsFound.size()).append(" passende Kontakte gefunden. Bitte wähle einen aus: ");
    int i = 1;
    for (Contact contact : contactsFound) {
        contactListString.append("Kontakt Nummer ").append(i).append(": ").append(contact.getName()).append(". ");
        i++;
    }
    log.info("Several contacts found: " + contactListString.toString());
    session.setAttribute(DIALOG_CONTEXT, CONTACT_TRANSFER_INTENT);
    return getAskResponse(CONTACT_TRANSFER_CARD, contactListString.toString());
}
Also used : LinkedList(java.util.LinkedList) Contact(model.db.Contact)

Example 5 with Contact

use of model.db.Contact in project amos-ss17-alexa by c-i-ber.

the class ContactTransferService method performTransfer.

/**
 * Performs the transfer.
 */
private SpeechletResponse performTransfer(Intent intent, Session session) {
    Object contactsFoundObj = SESSION_STORAGE.getObject(session.getSessionId(), SESSION_PREFIX + ".contactsFound");
    if (contactsFoundObj == null || !(contactsFoundObj instanceof List)) {
        return getResponse(CONTACT_TRANSFER_CARD, "Da ist etwas schiefgegangen. Tut mir Leid.");
    }
    Object choiceObj = session.getAttribute(SESSION_PREFIX + ".choice");
    if (choiceObj == null || !(choiceObj instanceof Integer)) {
        return getResponse(CONTACT_TRANSFER_CARD, "Da ist etwas schiefgegangen. Tut mir Leid.");
    }
    Object amountObj = session.getAttribute(SESSION_PREFIX + ".amount");
    if (amountObj == null || !(amountObj instanceof Double || amountObj instanceof Integer)) {
        return getResponse(CONTACT_TRANSFER_CARD, "Da ist etwas schiefgegangen. Tut mir Leid.");
    }
    Contact contact = ((List<Contact>) contactsFoundObj).get((int) choiceObj - 1);
    double amount;
    if (amountObj instanceof Double) {
        amount = (double) amountObj;
    } else {
        amount = (int) amountObj;
    }
    DateTimeFormatter apiTransactionFmt = DateTimeFormat.forPattern("yyyy-MM-dd");
    String valueDate = DateTime.now().toString(apiTransactionFmt);
    Transaction transaction = TransactionAPI.createTransaction((int) amount, /*"DE50100000000000000001"*/
    AmosAlexaSpeechlet.ACCOUNT_IBAN, contact.getIban(), valueDate, "Beschreibung", "Hans", null);
    Account account = AccountAPI.getAccount(ACCOUNT_NUMBER);
    String balanceAfterTransaction = String.valueOf(account.getBalance());
    // save transaction id to save in db
    session.setAttribute(TRANSACTION_ID_ATTRIBUTE, transaction.getTransactionId().toString());
    // add category ask to success response
    String categoryAsk = "Zu welcher Kategorie soll die Transaktion hinzugefügt werden. Sag zum Beispiel Kategorie " + Category.categoryListText();
    return getAskResponse(CONTACT_TRANSFER_CARD, "Erfolgreich. " + amount + " Euro wurden an " + contact.getName() + " überwiesen. Dein neuer Kontostand beträgt " + balanceAfterTransaction + " Euro. " + categoryAsk);
}
Also used : Account(model.banking.Account) Transaction(model.banking.Transaction) LinkedList(java.util.LinkedList) List(java.util.List) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) Contact(model.db.Contact)

Aggregations

Contact (model.db.Contact)10 LinkedList (java.util.LinkedList)3 Test (org.junit.Test)3 Reprompt (com.amazon.speech.ui.Reprompt)2 DateFormat (java.text.DateFormat)2 SimpleDateFormat (java.text.SimpleDateFormat)2 List (java.util.List)2 Account (model.banking.Account)2 Transaction (model.banking.Transaction)2 Slot (com.amazon.speech.slu.Slot)1 PlainTextOutputSpeech (com.amazon.speech.ui.PlainTextOutputSpeech)1 SimpleCard (com.amazon.speech.ui.SimpleCard)1 SsmlOutputSpeech (com.amazon.speech.ui.SsmlOutputSpeech)1 StandingOrder (model.banking.StandingOrder)1 Category (model.db.Category)1 DateTimeFormatter (org.joda.time.format.DateTimeFormatter)1