|
| 1 | +package net.b07z.sepia.sdk.services.uid1007; |
| 2 | + |
| 3 | +import java.util.TreeSet; |
| 4 | + |
| 5 | +import org.json.simple.JSONObject; |
| 6 | + |
| 7 | +import net.b07z.sepia.server.assist.data.Card; |
| 8 | +import net.b07z.sepia.server.assist.data.Card.ElementType; |
| 9 | +import net.b07z.sepia.server.assist.interpreters.NluResult; |
| 10 | +import net.b07z.sepia.server.assist.services.ServiceBuilder; |
| 11 | +import net.b07z.sepia.server.assist.services.ServiceInfo; |
| 12 | +import net.b07z.sepia.server.assist.services.ServiceInterface; |
| 13 | +import net.b07z.sepia.server.assist.services.ServiceResult; |
| 14 | +import net.b07z.sepia.server.assist.services.ServiceInfo.Content; |
| 15 | +import net.b07z.sepia.server.assist.services.ServiceInfo.Type; |
| 16 | +import net.b07z.sepia.server.core.assistant.ACTIONS; |
| 17 | +import net.b07z.sepia.server.core.data.Language; |
| 18 | +import net.b07z.sepia.server.core.tools.JSON; |
| 19 | +import net.b07z.sepia.server.core.tools.Sdk; |
| 20 | + |
| 21 | +/** |
| 22 | + * "Hello World" custom service that just returns a "Hello" answer, |
| 23 | + * a demo button that links to the SDK and a demo card that links to SEPIA website. |
| 24 | + * |
| 25 | + * @author Florian Quirin |
| 26 | + * |
| 27 | + */ |
| 28 | +public class HelloWorld implements ServiceInterface{ |
| 29 | + |
| 30 | + //Command name |
| 31 | + private static final String CMD_NAME = "hello_world"; //Name tag of your service (will be combined with userId to be unique) |
| 32 | + //Answers used: |
| 33 | + private static final String successAnswer = "chat_hello_0a"; //successful answer |
| 34 | + private static final String okAnswer = "default_not_possible_0a"; //service ran properly but can't generate desired result (e.g. a search term was not found) |
| 35 | + private static final String failAnswer = "error_0a"; //fallback if service or some part of it crashed |
| 36 | + |
| 37 | + //Define some sentences for testing: |
| 38 | + |
| 39 | + @Override |
| 40 | + public TreeSet<String> getSampleSentences(String lang){ |
| 41 | + TreeSet<String> samples = new TreeSet<>(); |
| 42 | + //GERMAN |
| 43 | + if (lang.equals(Language.DE.toValue())){ |
| 44 | + samples.add("Hallo Welt!"); |
| 45 | + |
| 46 | + //OTHER |
| 47 | + }else{ |
| 48 | + samples.add("Hello world!"); |
| 49 | + } |
| 50 | + return samples; |
| 51 | + } |
| 52 | + |
| 53 | + //Basic service setup: |
| 54 | + |
| 55 | + @Override |
| 56 | + public ServiceInfo getInfo(String language) { |
| 57 | + //Type of service (for descriptions, choose what you think fits best) |
| 58 | + ServiceInfo info = new ServiceInfo(Type.plain, Content.data, false); |
| 59 | + |
| 60 | + //Should be available publicly or only for the developer? Set this when you are done with testing and want to release |
| 61 | + //info.makePublic(); |
| 62 | + |
| 63 | + //Command |
| 64 | + info.setIntendedCommand(Sdk.getMyCommandName(this, CMD_NAME)); |
| 65 | + |
| 66 | + //Direct-match trigger sentences in different languages: |
| 67 | + String DE = Language.DE.toValue(); |
| 68 | + info.addCustomTriggerSentence("Hallo Welt!", DE) |
| 69 | + .addCustomTriggerSentence("Hello world!", DE) |
| 70 | + .addCustomTriggerSentence("Teste meinen hallo Welt service.", DE); |
| 71 | + String EN = Language.EN.toValue(); |
| 72 | + info.addCustomTriggerSentence("Hello world!", EN) |
| 73 | + .addCustomTriggerSentence("Test my hello world service.", EN); |
| 74 | + |
| 75 | + //Parameters: |
| 76 | + //This service has no parameters |
| 77 | + |
| 78 | + //Answers (these are the default answers, you can trigger a custom answer at any point in the module with api.setCustomAnswer(..)): |
| 79 | + info.addSuccessAnswer(successAnswer) |
| 80 | + .addFailAnswer(failAnswer) |
| 81 | + .addOkayAnswer(okAnswer); |
| 82 | + |
| 83 | + return info; |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public ServiceResult getResult(NluResult nluResult) { |
| 88 | + //initialize result |
| 89 | + ServiceBuilder api = new ServiceBuilder(nluResult, |
| 90 | + getInfoFreshOrCache(nluResult.input, this.getClass().getCanonicalName())); |
| 91 | + |
| 92 | + //get required parameters |
| 93 | + //NONE |
| 94 | + //get optional parameters |
| 95 | + //NONE |
| 96 | + |
| 97 | + //This service basically cannot fail ... ;-) |
| 98 | + |
| 99 | + //Just for demo purposes we add a button-action with a link to the SDK |
| 100 | + api.addAction(ACTIONS.BUTTON_IN_APP_BROWSER); |
| 101 | + api.putActionInfo("url", "https://github.com/SEPIA-Framework/sepia-sdk-java"); |
| 102 | + api.putActionInfo("title", "SDK info"); |
| 103 | + |
| 104 | + //... and we also add a demo card |
| 105 | + Card card = new Card(Card.TYPE_SINGLE); |
| 106 | + JSONObject linkCard = card.addElement( |
| 107 | + ElementType.link, |
| 108 | + JSON.make("title", "S.E.P.I.A." + ":", "desc", "Hello World!"), |
| 109 | + null, null, "", |
| 110 | + "https://sepia-framework.github.io/", |
| 111 | + "https://sepia-framework.github.io/img/icon.png", |
| 112 | + null, null |
| 113 | + ); |
| 114 | + JSON.put(linkCard, "imageBackground", "#000"); //more options like CSS background |
| 115 | + api.addCard(card.getJSON()); |
| 116 | + |
| 117 | + //all good |
| 118 | + api.setStatusSuccess(); |
| 119 | + |
| 120 | + //build the API_Result |
| 121 | + ServiceResult result = api.buildResult(); |
| 122 | + return result; |
| 123 | + } |
| 124 | + |
| 125 | +} |
0 commit comments