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..

Chatting from java
 
 

Hello,
I have been working on an application built in java that creates a chat interface and connects to a ChatScript server(local).
I am still learning Java and ChatScript so I know im error prone.

the program should connect to the local server, send whatever the user types and recieve the output to display.

The following code I have written is suppose to push a message to the server and recieve the volley in return.

if(ae.getActionCommand().equals("Send")) {
   Chat c 
= new Chat();
   
   
// Push Message
   
c.setFrom(login);
   
c.setTo(bot);
   
c.setMessage(chatInput.getText());
   
chatDisplay.setText(chatDisplay.getText() + c.getFrom() + ": " c.getMessage() + "n");
   
   
   
// Open Chat Script and send the message
   
try {    
    Process p
Runtime.getRuntime().exec(path args c.get());
    
p.waitFor();
    
InputStream in p.getInputStream();
    
ByteArrayOutputStream baos = new ByteArrayOutputStream();

    
baos.write(in.read())
    
c.set(botlogin,new String(baos.toByteArray()) );
    
    
     
   
catch (Exception e{
    e
.printStackTrace();
   
}
   
   
//Display output
   //c.set(bot, login, "Hello"); //dummy chat   
   
chatDisplay.setText(chatDisplay.getText() + c.getFrom() + ":" c.getMessage() + "n");
   
   
//clear the input {write code}
  
}
 } 

any suggestions or advice?

 

 
  [ # 1 ]

Process p= Runtime.getRuntime().exec(path + args + c.get());
 
Not being a java expert, it looks to me like you are merely executing ChatScript and trying to put the chat on a command line to it. Doesn’t work that way.  If you were trying to communicate with a local server, you would be opening a socket, connecting to localhost:1024 and sending and receiving the socket data then closing it

 

 
  [ # 2 ]
Eric Virgil - Jan 26, 2014:

any suggestions or advice?

What Bruce said is essentially correct - here is code to connect to the chatbot server from java and send/receive a text…

/** class is instantiated, then initialized, call processInput(message) **/

public class ChatScriptBot implements Bot {

 char nullChar 
= (char0;
        
String userName "Jerry"// or take as input
 
 
@Override
 
public String processInput(String input{
  
return doMessage(default+nullChar+nullChar+input+nullChar);
 
}

 
private String doMessage(String mess)
 
{
  Socket echoSocket
;
  
String resp "";
  
  try 
{
   echoSocket 
= new Socket("localhost"3333);
   
PrintWriter out = new PrintWriter(echoSocket.getOutputStream(),
     
true);
   
BufferedReader in = new BufferedReader(new InputStreamReader(
     
echoSocket.getInputStream()));
   
out.println(mess);
   
resp in.readLine();
   
echoSocket.close();
  
catch (IOException e{
   e
.printStackTrace();
   
System.out.println("Error: " e.getMessage());
  
}

  
return resp;
  
 
}
 
@Override
 
public void init(HashMap<StringStringargs{
  System
.out.println("ChatScript Bot init()...");
  
System.out.println("got back:" doMessage(nullChar+"1"+nullChar+nullChar));
  
System.out.println("now starting conversation..." doMessage(userName+nullChar+nullChar+nullChar));
  
System.out.println("conversation started with default bot as " userName);
 
}

 
@Override
 
public String getBotType() {
  
// TODO Auto-generated method stub
  
return "ChatSCript";
 
}

Run the server as described in the server manual, substitute the right port number.  Hope this helps.  The tricky part is the null termination - java strings are not null terminated so you have to do it manually.

 

 
  [ # 3 ]

i get error at this line:
  return doMessage(default+nullChar+nullChar+input+nullChar);
does default needs to be removed?

 

 
  [ # 4 ]
Ash Far - Mar 23, 2017:

i get error at this line:
  return doMessage(default+nullChar+nullChar+input+nullChar);
does default needs to be removed?

no, you need to define default as the user you are connecting as.  I think the sample I posted inherited that variable from it’s parent, so it isn’t defined in that snippet?

 

 
  [ # 5 ]

Correct! Works perfectly now. Thanks alot Todd.
Did you had any luck with using Chatscript as library with JNI or Swig?

 

 
  [ # 6 ]
Ash Far - Mar 24, 2017:

Correct! Works perfectly now. Thanks alot Todd.
Did you had any luck with using Chatscript as library with JNI or Swig?

I have, yes.  I’m assuming you are referring to - http://vinterstitial.blogspot.com/2014/05/a-swig-of-chatbot-making-unity-chatter.html?

I haven’t used the java JNI versions extensively, but it passed my tests when I was checking that alternative.  You can find the latest swig stuff for java on my GitHub - https://github.com/tkuebler/ChatScriptBrains

 

 
  [ # 7 ]

I have written a Apache Camel component to interact with the BOTS. Initial version can be checked out from git hub.
https://github.com/varunblaze/camel-chatscript/

 

 
  [ # 8 ]

Have you bumped up an old thread to say this for a reason?

 

 
  [ # 9 ]

grin Actually, I a CS newbie and was googling to get a java client for CS.  Only from this thread I got a proper code. So thought of updating the same thread

 

 
  login or register to react