Code Monkey home page Code Monkey logo

Comments (7)

gawi avatar gawi commented on July 17, 2024

Rivr has no knowledge of APIs whatsoever. You have to provide the API client and invoke it from your Java code with the input provided by the user.

Rivr is all about centralising everything on the Java server side. Thus, you should do the integration of the API from the Java code.

from rivr.

D41138761 avatar D41138761 commented on July 17, 2024

Thanks for the response.
we want to construct data tag as below which is calling any URL.
-- data name="return_data" srcexpr="some_url" method="post" namelist="var1 var2 var3"
Looks like we dont have any class in rivr to construct it directly .

from rivr.

gawi avatar gawi commented on July 17, 2024

Once again, you shouldn't use the <data> element to do API calls. Instead, you should prefer make the call from the Java code, which allows you to do many things not possible with the <data> element:

  • handle timeout
  • add HTTP headers (for authentication, for instance)
  • use a method different from POSTor GET (which are the only 2 values allowed by the VoiceXML 2.1 recommendation).

from rivr.

D41138761 avatar D41138761 commented on July 17, 2024

I am confused. my understanding is data tag is part of vxml , system which is executing vxml and the data tag will call the api directly and get response ? or how can vxml call our java code to make api call ? , any sample code we can refer?

from rivr.

gawi avatar gawi commented on July 17, 2024

Yes, you are right when you say that <data> is part of VoiceXML 2.1. However, it has not been exposed in Rivr because there is a much better way to do it on the server side.

With Rivr, when you do an application, you do not generate a large VoiceXML document. Instead, Rivr generates a series of small VoiceXML document and posts the result of the document execution back to the server every time.

Here's a very basic code sample. I'm using HttpURLConnection but you should really use something better (there are many options when it comes to API clients in Java):

package com.nuecho.rivr.cookbook.dialogue;

import java.io.*;
import java.net.*;
import java.nio.charset.*;

import javax.json.*;

import com.nuecho.rivr.core.channel.*;
import com.nuecho.rivr.core.dialogue.*;
import com.nuecho.rivr.core.util.*;
import com.nuecho.rivr.voicexml.dialogue.*;
import com.nuecho.rivr.voicexml.turn.first.*;
import com.nuecho.rivr.voicexml.turn.input.*;
import com.nuecho.rivr.voicexml.turn.last.*;
import com.nuecho.rivr.voicexml.turn.output.*;
import com.nuecho.rivr.voicexml.turn.output.audio.*;
import com.nuecho.rivr.voicexml.turn.output.grammar.*;

public class Menu implements VoiceXmlDialogue {

    @Override
    public VoiceXmlLastTurn run(VoiceXmlFirstTurn firstTurn, VoiceXmlDialogueContext context) throws Timeout,
            InterruptedException {

        String number = getNumber(context);

        try {
            String processedNumber = invokeApi(number);
            Message resultMessage = new Message("resultMessage",
                                                new SpeechSynthesis("The result is: " + processedNumber));
            DialogueUtils.doTurn(resultMessage, context);
        } catch (IOException exception) {
            Message errorMessage = new Message("errorMessage",
                                               new SpeechSynthesis("An error occured: "
                                                                   + exception.getMessage()));
            DialogueUtils.doTurn(errorMessage, context);
        }

        return new Exit("exit");
    }

    private String getNumber(VoiceXmlDialogueContext context) throws Timeout, InterruptedException {
        String number = null;

        do {
            DtmfRecognition dtmfRecognition = new DtmfRecognition(new GrammarReference("builtin:dtmf/digits"));
            SpeechSynthesis promptText = new SpeechSynthesis("Enter a number.");

            Interaction interaction = OutputTurns.interaction("askNumber")
                                                 .addPrompt(dtmfRecognition, promptText)
                                                 .build(dtmfRecognition, Duration.milliseconds(5));

            VoiceXmlInputTurn inputTurn = DialogueUtils.doTurn(interaction, context);

            if (inputTurn.getRecognitionInfo() != null) {
                JsonArray recognitionResult = inputTurn.getRecognitionInfo().getRecognitionResult();
                number = recognitionResult.getJsonObject(0).getString("interpretation");
            }
        } while (number != null); //loop until valid value is provided

        return number;
    }

    public static String invokeApi(String number) throws IOException {
        URL apiUrl = new URL("http://localhost:8080/api/x/v1/numbers/" + number);
        HttpURLConnection connection = (HttpURLConnection) apiUrl.openConnection();
        connection.setRequestMethod("GET");

        int responseCode = connection.getResponseCode();
        if (responseCode != 200) throw new IOException("Non-200 reponse from API: " + responseCode);

        return readAllStream(connection.getInputStream());
    }

    public static String readAllStream(InputStream inputStream) throws IOException {
        int bufferSize = 1024;
        char[] buffer = new char[bufferSize];
        StringBuilder out = new StringBuilder();
        Reader in = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
        for (int numRead; (numRead = in.read(buffer, 0, buffer.length)) > 0;) {
            out.append(buffer, 0, numRead);
        }
        return out.toString();
    }
}

This will generate a first document where user will be prompted for a number. Once the number is provided, the result will be sent back to Rivr and the Dialogue will continue where we left off (in the doTurn method). Then the call to the API can be done in the Java code. Following that, we create another VoiceXML document that reads the result back to the user. Then we terminate the dialogue (with an <exit> element).

from rivr.

D41138761 avatar D41138761 commented on July 17, 2024

Thanks, this helps to understand the rivr better.

what is the equivalent vxml to this , to run the vxml from any system ?

from rivr.

gawi avatar gawi commented on July 17, 2024

"to run the vxml from any system"

Not sure what you mean by that.

from rivr.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.