mirror of
https://github.com/jlengrand/dialogflow-fun.git
synced 2026-03-10 08:11:22 +00:00
Add support for follow-up intents (#24)
This commit is contained in:
committed by
GitHub
parent
570017c344
commit
77530c13a5
@@ -1,8 +1,4 @@
|
||||
runtime: java11
|
||||
instance_class: F1
|
||||
automatic_scaling:
|
||||
min_instances: 1
|
||||
env_variables:
|
||||
OPENBANKPROJECT_USERNAME: 'jlengrand'
|
||||
OPENBANKPROJECT_PASSWORD: 'Bnj0VyuI127SN7yjJJYt'
|
||||
OPENBANKPROJECT_CONSUMERKEY: 'c4vkdziyo4xhry4nwir50oblsolo5f1g1k2tdyu2'
|
||||
min_instances: 1
|
||||
@@ -46,6 +46,9 @@ public class DialogflowFunApiApplication {
|
||||
case "send.money":
|
||||
response = dialogFlowService.createPaymentRequest(request);
|
||||
break;
|
||||
case "send.money - yes":
|
||||
response = dialogFlowService.createPaymentRequestWithFollowUp(request);
|
||||
break;
|
||||
default:
|
||||
response = new DialogFlowResponse("Sorry, I didn't get that. Can you try again? ");
|
||||
}
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
package fr.lengrand.dialogflowfunapi.dialogflow;
|
||||
|
||||
import fr.lengrand.dialogflowfunapi.dialogflow.data.DialogFlowResponse;
|
||||
import fr.lengrand.dialogflowfunapi.dialogflow.data.DialogFlowWebHookRequest;
|
||||
import fr.lengrand.dialogflowfunapi.dialogflow.data.PaymentRequestDetails;
|
||||
import fr.lengrand.dialogflowfunapi.dialogflow.data.*;
|
||||
import fr.lengrand.dialogflowfunapi.openbankproject.OpenBankClient;
|
||||
import fr.lengrand.dialogflowfunapi.openbankproject.data.BankAccount;
|
||||
import fr.lengrand.dialogflowfunapi.openbankproject.data.paymentrequest.PaymentRequest;
|
||||
@@ -18,28 +16,10 @@ import java.util.Optional;
|
||||
|
||||
@Service
|
||||
public class DialogFlowService {
|
||||
|
||||
@Autowired
|
||||
private OpenBankClient openBankClient;
|
||||
|
||||
// TODO : Add strong authentication
|
||||
public DialogFlowResponse createPaymentRequest(DialogFlowWebHookRequest request) throws IOException, InterruptedException {
|
||||
Optional<BankAccount> userAccount = UserAccountLookup.getBankAccountFromContact(request.getQueryResult().getParameters().getContact());
|
||||
|
||||
if (userAccount.isEmpty())
|
||||
return new DialogFlowResponse("Sorry, We have not found any bank account for " + request.getQueryResult().getParameters().getContact() + ". Cancelling.");
|
||||
|
||||
PaymentRequest paymentRequest = openBankClient.createPaymentRequest(UserAccountLookup.getCurrentUserAccount()
|
||||
, new PaymentRequestDetails(
|
||||
userAccount.get().toAccount(),
|
||||
request.getQueryResult().getParameters().getUnitCurrency(),
|
||||
request.getQueryResult().getParameters().getContact() + " at " + getCurrentTime())
|
||||
);
|
||||
|
||||
return paymentRequest.getStatus().equalsIgnoreCase("completed") ?
|
||||
new DialogFlowResponse("Created a payment for a value of " + request.getQueryResult().getParameters().getUnitCurrency().getAmount() + request.getQueryResult().getParameters().getUnitCurrency().getCurrency() + " to " + request.getQueryResult().getParameters().getContact())
|
||||
: new DialogFlowResponse("Sorry, the creation of the payment failed. Please try again later!");
|
||||
}
|
||||
|
||||
public DialogFlowResponse getLastTransactionRequest() throws IOException, InterruptedException {
|
||||
Optional<Transaction> transaction = this.getLastTransaction();
|
||||
return transaction.isPresent() ?
|
||||
@@ -47,6 +27,19 @@ public class DialogFlowService {
|
||||
: new DialogFlowResponse("You appear to have no transactions!");
|
||||
}
|
||||
|
||||
public DialogFlowResponse createPaymentRequest(DialogFlowWebHookRequest request) throws IOException, InterruptedException {
|
||||
DialogFlowParameters parameters = request.getQueryResult().getParameters();
|
||||
return this.createPaymentRequest(parameters.getContact(), parameters.getUnitCurrency());
|
||||
}
|
||||
|
||||
public DialogFlowResponse createPaymentRequestWithFollowUp(DialogFlowWebHookRequest request) throws IOException, InterruptedException {
|
||||
if(request.getQueryResult().getOutputContexts().size() < 1)
|
||||
return new DialogFlowResponse("Sorry, the creation of the payment failed. Please try again later!");
|
||||
|
||||
DialogFlowParameters parameters = request.getQueryResult().getOutputContexts().get(0).getParameters();
|
||||
return this.createPaymentRequest(parameters.getContact(), parameters.getUnitCurrency());
|
||||
}
|
||||
|
||||
private String createTransactionDialogResponse(Transaction transaction){
|
||||
return "Your last transaction was for " + transaction.getDetails().getDescription() + " with an amount of " + (-transaction.getDetails().getValue().getAmount()) + transaction.getDetails().getValue().getCurrency() + ". Your new balance is " + transaction.getDetails().getNewBalance().getAmount() + transaction.getDetails().getNewBalance().getCurrency();
|
||||
|
||||
@@ -62,6 +55,24 @@ public class DialogFlowService {
|
||||
.findFirst();
|
||||
}
|
||||
|
||||
private DialogFlowResponse createPaymentRequest(String contact, UnitCurrency unitCurrency) throws IOException, InterruptedException {
|
||||
Optional<BankAccount> userAccount = UserAccountLookup.getBankAccountFromContact(contact);
|
||||
|
||||
if (userAccount.isEmpty())
|
||||
return new DialogFlowResponse("Sorry, We have not found any bank account for " + contact + ". Cancelling.");
|
||||
|
||||
PaymentRequest paymentRequest = openBankClient.createPaymentRequest(UserAccountLookup.getCurrentUserAccount()
|
||||
, new PaymentRequestDetails(
|
||||
userAccount.get().toAccount(),
|
||||
unitCurrency,
|
||||
contact + " at " + getCurrentTime())
|
||||
);
|
||||
|
||||
return paymentRequest.getStatus().equalsIgnoreCase("completed") ?
|
||||
new DialogFlowResponse("Created a payment for a value of " + unitCurrency.getAmount() + unitCurrency.getCurrency() + " to " + contact)
|
||||
: new DialogFlowResponse("Sorry, the creation of the payment failed. Please try again later!");
|
||||
}
|
||||
|
||||
private String getCurrentTime() {
|
||||
return LocalDateTime.now().format(DateTimeFormatter.ofPattern("MM-dd HH:mm"));
|
||||
}
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
package fr.lengrand.dialogflowfunapi.dialogflow.data;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public class DialogFlowContext {
|
||||
|
||||
private String name;
|
||||
private DialogFlowParameters parameters;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public DialogFlowParameters getParameters() {
|
||||
return parameters;
|
||||
}
|
||||
|
||||
public void setParameters(DialogFlowParameters parameters) {
|
||||
this.parameters = parameters;
|
||||
}
|
||||
}
|
||||
@@ -2,12 +2,16 @@ package fr.lengrand.dialogflowfunapi.dialogflow.data;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public class DialogFlowQueryResult {
|
||||
|
||||
private DialogFlowIntent intent;
|
||||
private DialogFlowParameters parameters;
|
||||
|
||||
private List<DialogFlowContext> outputContexts;
|
||||
|
||||
public DialogFlowIntent getIntent() {
|
||||
return intent;
|
||||
}
|
||||
@@ -23,4 +27,12 @@ public class DialogFlowQueryResult {
|
||||
public void setParameters(DialogFlowParameters parameters) {
|
||||
this.parameters = parameters;
|
||||
}
|
||||
|
||||
public List<DialogFlowContext> getOutputContexts() {
|
||||
return outputContexts;
|
||||
}
|
||||
|
||||
public void setOutputContexts(List<DialogFlowContext> outputContexts) {
|
||||
this.outputContexts = outputContexts;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user