... | ... | @@ -60,11 +60,6 @@ public class MyModule extends AIModule{ |
|
|
public MyModule() {
|
|
|
super("modulename","Maximvdw","Description here");
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public String getResponse(AIQuestionEvent event) {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
}
|
|
|
```
|
... | ... | @@ -73,8 +68,135 @@ If you are creating a module the class will automatically be initialized, if you |
|
|
The first argument of the constructor is the **action name**, this is the name of your action and has to be the same as the action you put in your intent response.
|
|
|
The second argument is the name of the author and the third argument is a simple description of the module.
|
|
|
|
|
|
#### Returning a dynamic response
|
|
|
The first thing to do is to overwrite the `getResponse` method of the AIModule.
|
|
|
|
|
|
The `getResponse` expects a string to be returned as the answer. It comes with an `AIQuestionEvent` containing everything you need ranging from the player who asked the question to the possible responses and parameters.
|
|
|
|
|
|
It is called after a question has been linked to your module.
|
|
|
|
|
|
```java
|
|
|
public class MyModule extends AIModule{
|
|
|
|
|
|
public MyModule() {
|
|
|
super("modulename","Maximvdw","Description here");
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public String getResponse(AIQuestionEvent event) {
|
|
|
// ... DO something here
|
|
|
// return string with the response
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
}
|
|
|
```
|
|
|
|
|
|
#### Sending a context
|
|
|
You have the possibility to send a context to accompany the question. The `getContext` method
|
|
|
will be called before the question is send to API.ai.
|
|
|
|
|
|
It is useful if you have 'generic' questions that need a contexts. An example would be a quest. If a player asks "how much time left?" when on a quest, you can create a context with this information.
|
|
|
|
|
|
```java
|
|
|
public class MyModule extends AIModule{
|
|
|
|
|
|
public MyModule() {
|
|
|
super("modulename","Maximvdw","Description here");
|
|
|
|
|
|
Intent questName = new Intent("proquests.quest.name")
|
|
|
// ONLY, AND ONLY match this question
|
|
|
// when we found a contexts called "proquests"
|
|
|
// Notice in "getContext(...) we only return
|
|
|
// this context when the user is on a quest
|
|
|
// So if the user asks "what is the name of the quest?"
|
|
|
// when not on a quest, the question won't be matched (accidently)
|
|
|
.addRequiredContext("proquests")
|
|
|
// This is a VERY simple question
|
|
|
// These are basic template questions without
|
|
|
// entities ,etc.. Note that the question of the user
|
|
|
// doesn't need to be exact like this
|
|
|
// The machine learning will learn variations
|
|
|
.addTemplate("what is the name of the quest?")
|
|
|
.addTemplate("what is the name of this quest?")
|
|
|
.addTemplate("what's the quest name?")
|
|
|
.addTemplate("what quest am I on?")
|
|
|
.addTemplate("what quest am I doing?")
|
|
|
.addResponse(new IntentResponse()
|
|
|
.addMessage(new IntentResponse.TextResponse()
|
|
|
.addSpeechText("The name is #proquests.name")));
|
|
|
|
|
|
Intent questTimeLeft = new Intent("proquests.quest.timeleft")
|
|
|
// ONLY, AND ONLY match this question
|
|
|
// when we found a contexts called "proquests"
|
|
|
// Notice in "getContext(...) we only return
|
|
|
// this context when the user is on a quest
|
|
|
// So if the user asks "what is the name of the quest?"
|
|
|
// when not on a quest, the question won't be matched (accidently)
|
|
|
.addRequiredContext("proquests")
|
|
|
// This is a VERY simple question
|
|
|
// These are basic template questions without
|
|
|
// entities ,etc.. Note that the question of the user
|
|
|
// doesn't need to be exact like this
|
|
|
// The machine learning will learn variations
|
|
|
.addTemplate("how much time do I have left?")
|
|
|
.addResponse(new IntentResponse()
|
|
|
.addMessage(new IntentResponse.TextResponse()
|
|
|
.addSpeechText("You have #proquests.time.left seconds left")));
|
|
|
|
|
|
try {
|
|
|
// Upload the intents
|
|
|
if (QAPluginAPI.findIntentByName(questTimeLeft.getName()) == null) {
|
|
|
if (!QAPluginAPI.uploadIntent(questTimeLeft)) {
|
|
|
warning("Unable to upload intent!");
|
|
|
}
|
|
|
}
|
|
|
if (QAPluginAPI.findIntentByName(questName.getName()) == null) {
|
|
|
if (!QAPluginAPI.uploadIntent(questName)) {
|
|
|
warning("Unable to upload intent!");
|
|
|
}
|
|
|
}
|
|
|
} catch (FeatureNotEnabled ex) {
|
|
|
severe("You do not have a developer access token in your QAPlugin config!");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* This is triggered BEFORE the question is asked.
|
|
|
* Here we will check if the player is on a quest.
|
|
|
* That way if he asks "how much time do I have left"
|
|
|
* we know he is talking about a quest and not about something else.
|
|
|
*
|
|
|
* @param event AIQuestionEvent event
|
|
|
* @return null if no quest, Context with quest info when he is
|
|
|
*/
|
|
|
@Override
|
|
|
public Context getContext(AIQuestionEvent event) {
|
|
|
Player player = event.getPlayer();
|
|
|
|
|
|
// ...
|
|
|
// DO CHECK IF PLAYER IS ON QUEST
|
|
|
// ...
|
|
|
boolean quest = true;
|
|
|
|
|
|
if (quest) {
|
|
|
// We gonna create a context containing all
|
|
|
// quest information
|
|
|
Context context = new Context("proquests", 1);
|
|
|
context.addParameter("name", "KillThePig");
|
|
|
context.addParameter("time.left", String.valueOf(150));
|
|
|
// ...
|
|
|
|
|
|
return context;
|
|
|
} else {
|
|
|
// Not on a quest, return null
|
|
|
return null;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
}
|
|
|
```
|
|
|
|
|
|
### Creating an entity
|
|
|
```java
|
|
|
// This is an entity
|
... | ... | |