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.");
}
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;
}
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);
}
}
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());
}
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);
}
Aggregations