DuerOS custom skill development - single intention development

Every skill can't be processed without intention, that is to say, every skill has at least one intention and will be better demonstrated in the future. Let me show you a simple case first

Skill sharing address: https://dueros.baidu.com/dbp/main/console?shareCode=FYay

1, Skill design (for example, buying movie tickets)

  • 1. Basic information of skills
  • 2. Interaction model
  • (1) Intention + slot
  • (2) Dictionary


2, Development of skills (CFC Development) reference document address: https://dueros.baidu.com/didp/doc/dueros-bot-platform/dbp-deploy/cfc-java_markdown

(1) , code development

  • ยท,Bot.java
package com.ittzg.dueros;

import com.baidu.dueros.bot.BaseBot;
import com.baidu.dueros.certificate.Certificate;
import com.baidu.dueros.data.request.IntentRequest;
import com.baidu.dueros.data.request.LaunchRequest;
import com.baidu.dueros.data.request.SessionEndedRequest;
import com.baidu.dueros.data.response.OutputSpeech;
import com.baidu.dueros.data.response.card.TextCard;
import com.baidu.dueros.model.Request;
import com.baidu.dueros.model.Response;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @author: ittzg
 * @date: 2020/1/6 18:56
 * @Email: tazhigang095@163.com
 * @desc:
 */
public class Bot extends BaseBot {
    protected Bot(Request request) throws IOException {
        super(request);
    }

    protected Bot(String request) throws IOException {
        super(request);
    }

    protected Bot(Certificate certificate) throws IOException {
        super(certificate);
    }

    protected Bot(HttpServletRequest request) throws IOException {
        super(request);
    }

    @Override
    protected Response onLaunch(LaunchRequest launchRequest) {
        // New text card
        TextCard textCard = new TextCard("Film ticket center");
        // Set link address
        textCard.setUrl("www:....");
        // Set link content
        textCard.setAnchorText("setAnchorText");
        // Add guided script
        textCard.addCueWord("Cinema ticket");
        // New returned voice content
        OutputSpeech outputSpeech = new OutputSpeech(OutputSpeech.SpeechType.PlainText, "Welcome to the interface of test case 1. You can tell me to buy movie tickets");
        // Construct the returned Response
        Response response = new Response(outputSpeech, textCard);
        return response;
    }

    /**
     * Override the onInent method to handle onInent dialog events
     *
     * @param intentRequest
     *            IntentRequest Request body
     * @see BaseBot#onInent(IntentRequest)
     */
    @Override
    protected Response onInent(IntentRequest intentRequest) {
        String movieTicket = getSlot("movie.ticket");
        String confirmFlag = getSlot("movie.confirm");
        // Determine whether the intention name of NLU resolution matches movie ticket
        if ("movie-ticket".equals(intentRequest.getIntentName())) {
            // Determine whether this slot exists after NLU parsing
            if(movieTicket!=null){
                // Construct TextCard
                TextCard textCard = new TextCard("I would like to recommend juvenile you. Would you like to buy it;Say yes to me");
                OutputSpeech outputSpeech = new OutputSpeech(OutputSpeech.SpeechType.PlainText, "I would like to recommend juvenile you. Would you like to buy it;Tell me about it.");
                // Construct the returned Response
                this.setExpectSpeech(true);
                Response response = new Response(outputSpeech, textCard);
                return response;
            }
            if(confirmFlag!=null){
                // Construct TextCard
                TextCard textCard = new TextCard("You have successfully purchased tickets for the movie "young you". You can continue to tell me to buy tickets for the movie");
                OutputSpeech outputSpeech = new OutputSpeech(OutputSpeech.SpeechType.PlainText, "You have successfully purchased tickets for the movie "young you". You can continue to tell me to buy tickets for the movie");
                // Construct the returned Response
                this.setExpectSpeech(true);
                Response response = new Response(outputSpeech, textCard);
                return response;
            }
            
        }

        return null;
    }

    /**
     * Override the onSessionEnded event to handle the onSessionEnded dialog event
     *
     * @param sessionEndedRequest
     *            SessionEndedRequest Request body
     * @see BaseBot#onSessionEnded(SessionEndedRequest)
     */
    @Override
    protected Response onSessionEnded(SessionEndedRequest sessionEndedRequest) {

        // Construct outputspiech
        OutputSpeech outputSpeech = new OutputSpeech(OutputSpeech.SpeechType.PlainText, "Thank you for using this skill. See you next time");

        // Construct Response
        Response response = new Response(outputSpeech);

        return response;
    }

}
  • 2,App.java
package com.ittzg.dueros;

import com.baidubce.faas.core.FaasContext;
import com.baidubce.faas.core.InvokeHandler;
import org.apache.commons.io.IOUtils;

import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;

/**
 * @author: ittzg
 * @date: 2020/1/6 18:55
 * @Email: tazhigang095@163.com
 * @desc:
 */
public class App implements InvokeHandler {
    @Override
    public void invoke(InputStream inputStream, OutputStream outputStream, FaasContext faasContext) throws Exception {
        try {
            String result = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
            Bot bot = new Bot(result);
            bot.disableVerify();
            //Call the run method of bot
            String responseJson = bot.run();
            outputStream.write(responseJson.getBytes());
        } catch (Exception e) {
            e.printStackTrace();
            outputStream.write("{\"status\":1,\"msg\":\"function error\"}".getBytes());
        }
    }
}

(2) Deployment and configuration of CFC functions

(3) , test


Tags: Java Python Apache

Posted on Wed, 08 Jan 2020 08:53:41 -0800 by slicer123