AI Zone Admin Forum Add your forum

NEWS: Chatbots.org survey on 3000 US and UK consumers shows it is time for chatbot integration in customer service!read more..

Java programs integration with ChatScript
 
 

Suppose I wrote a Java program to accept a birthdate and return the age of a person. So how do I integrate it with ChatScript so that CS sends the data to the program. The program executes it and it returns the value to CS which it will print.

How to do it? I want it in details. I saw a thread in which it used popen but the syntax was unclear. So I couldn’t use it. Also the code couldn’t pass data to the program.

 

 
  [ # 1 ]

Hello Abhishek!


I think that you need create a socket connection in java and put into it the address and port that you set up in the chatscript. I think that in the manual “Chatscript ClientServer Manual.PDF” has more details.

 

 
  [ # 2 ]

I see the ^popen command but can’t understand it’s syntax.

Can somebody include a sample code with step by step procedure? Sorry for the trouble. I’m still an amateur.


This is my java server code to accept a number from the client program and return it’s value multiplied by 2.
I will make modifications on this program to perform various functions such as accept a date from user in ChatScript and return his current age.

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class Server
{

private static Socket socket;

public static void main(String[] args)
{
try
{

int port = 25000;
ServerSocket serverSocket = new ServerSocket(port);
System.out.println("Server Started and listening to the port 25000");

//Server is running always. This is done using this while(true) loop
while(true)
{
//Reading the message from the client
socket = serverSocket.accept();
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String number = br.readLine();
System.out.println("Message received from client is "+number);

//Multiplying the number by 2 and forming the return message
String returnMessage;
try
{
int numberInIntFormat = Integer.parseInt(number);
int returnValue = numberInIntFormat*2;
returnMessage = String.valueOf(returnValue) + "\n";
}
          catch(NumberFormatException e)
          {
//Input was not a number. Sending proper message back to client.
returnMessage = "Please send a proper number\n";
}

          //Sending the response back to the client.
          OutputStream os = socket.getOutputStream();
          OutputStreamWriter osw = new OutputStreamWriter(os);
          BufferedWriter bw = new BufferedWriter(osw);
          bw.write(returnMessage);
          System.out.println(“Message sent to the client is “+returnMessage);
          bw.flush();
        }
      }
      catch (Exception e)
      {
e.printStackTrace();
}
      finally
      {
try
{
socket.close();
}
        catch(Exception e){}
}
  }
}

Now how do I integrate it with CS. Can somebody give me the code to a .top file to run the server code and send the values to it and print the values returned by the server.

Thanks in advance.

 

 
  [ # 3 ]

Ok, I am a newbie too. Look, what is the program (java, web, etc) that you will use to interface with the user?

As far as I know the chatscript just can communicate through of the functions: popen, tcpopen jsonopen. That is, I think (I’m not sure) that chatscript cannot communicate with java via socket, but the opposite is possible, that is, java can communicate with chatscript via socket.

In the directory /WEBINTERFACE/BETTER on the chatscript there are two files with a example of how to communicate with chatscript via web using socket.

I hope I have helped.

Best regards,
Oberdan Alves.

 

 
  [ # 4 ]

The program I will use is CS.
Sample Conversation:

User:> What is my age?
Bot:> When were you born?
User:> 3rd January 2001
Bot:> Wow your are 15 years old. I am younger than you.

Here the user will input to CS which will pass the data to my java program to calculate the age. then the java program will return the age and CS will print it as a reply.

 

 
  [ # 5 ]

So, Chatscript can do computation.

But if you want pass this information to the java from Chatscript, then you have to use the function ^popen and pass the command to execute through the console. I think is not possible to use socket from chatscript to java, but the opposite is possible.

example:

In the chatscript:

t: ( ) when were you born?  <<- Here the bot ask to the user

u: (i was born _* ) $$date = ‘_0   <<- Here the bot save the date

$$commad =  ^join(“java computeDate.java ” ^eval( $$date ) ) <<- Here we build the string that we will use to call the java program, passing the date as an argument
popen($$commad ‘^myfunc) <<-Here we execute the command in the console and capture the java program output.


outputmacro: ^myfunc(^data) <<- Here we process the java program output.

_0 = ^data

In the Java:

You need catch the information through the argument list.

P.S: Please, see the GERMAN bot that come with Chatscript for more details about this example that I wrote.

 

 

 
  [ # 6 ]

Okay. Still not working. I tried many times. Can somebody please post a complete code for both CS as well as Java(or C++) which interact with each other.

Any help is really and greatly appreciated. Please. I need it fast.
Please post the complete code for both CS as well as Java ( or C++)

 

 
  [ # 7 ]

You don’t need the ^eval() in the line that builds $$command

$$commad =  ^join(“java computeDate.java ” $$date )


So ^log($$command) and paste that into a console window - does it work?
If it does, is your ^myfunc being called?

You don’t give any indication of what is not working, so please give us more details.
Note that the :trace command can help in showing what is going on under the covers.

 

 
  [ # 8 ]

I compile the code. It compiles. But afterwards when I ask a question the topic is invoked because it asks a question from the topic beginning. But it doesn’t give the output. It just goes to a random topic.

Should I paste the Java code inside my Bot’s folder in RAWDATA?

And what should be the format of the Java code. Should I use socket or should I use just simple methods with basic arguments?

 

 
  [ # 9 ]

The command passed to ^popen() is one that can be run from a Windows console, and it just captures standard output.

So your Java program just needs to read standard command line arguments and write out the answer. You should be able to verify that independently from a command prompt. The location of the Java code should be reachable from whatever yoru current directory is.

But from your comments, it might appear that the appropriate rule is CS is not being invoked in the first place.

Therefore I would suggest debugging these independently. The Java program as stated above, and I would replace the ^popen() call with straight text to see of your CS rules are executed as you expect.

If you are having trouble with determining which CS rules are being executed then I would recommend doing a
:trace pattern output
first.

 

 
  [ # 10 ]

I used JNI and swig to make chatscript available as a library to java directly.  It is faster and easier to maintain, as chatscript code runs inside of your java process.  However, it’s probably more of an advanced technique.  Check out my post from a few years ago that includes working java code:

https://www.chatbots.org/ai_zone/viewthread/1615/

Good Luck!

 

 
  login or register to react