Ok, so I ripped this out of a chatter-bot-api and made it easier to use and static...
/*
chatter-bot-api
Copyright (C) 2011 pierredavidbelanger@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package me.linus.chat;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.StringWriter;
import java.math.BigInteger;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.util.LinkedHashMap;
import java.util.Map;
public class Cleverbot {
private static final String url = "http://www.cleverbot.com/webservicemin";
private static final Map<String, String> vars;
static {
vars = new LinkedHashMap<String, String>();
vars.put("start", "y");
vars.put("icognoid", "wsf");
vars.put("fno", "0");
vars.put("sub", "Say");
vars.put("islearning", "1");
vars.put("cleanslate", "false");
}
public static String parametersToWWWFormURLEncoded(
Map<String, String> parameters) throws Exception {
StringBuilder s = new StringBuilder();
for (Map.Entry<String, String> parameter : parameters.entrySet()) {
if (s.length() > 0) {
s.append("&");
}
s.append(URLEncoder.encode(parameter.getKey(), "UTF-8"));
s.append("=");
s.append(URLEncoder.encode(parameter.getValue(), "UTF-8"));
}
return s.toString();
}
public static String post(String url, Map<String, String> parameters)
throws Exception {
URLConnection connection = new URL(url).openConnection();
connection
.setRequestProperty("User-Agent",
"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20100101 Firefox/5.0");
connection.setDoOutput(true);
connection.setDoInput(true);
OutputStreamWriter osw = new OutputStreamWriter(
connection.getOutputStream());
osw.write(parametersToWWWFormURLEncoded(parameters));
osw.flush();
osw.close();
Reader r = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
StringWriter w = new StringWriter();
char[] buffer = new char[1024];
int n = 0;
while ((n = r.read(buffer)) != -1) {
w.write(buffer, 0, n);
}
r.close();
return w.toString();
}
public static String md5(String input) throws Exception {
MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.update(input.getBytes("UTF-8"));
BigInteger hash = new BigInteger(1, md5.digest());
return String.format("%1$032X", hash);
}
public static String stringAtIndex(String[] strings, int index) {
if (index >= strings.length)
return "";
return strings[index];
}
public static String think(String message) throws Exception {
vars.put("stimulus", message);
String formData = parametersToWWWFormURLEncoded(vars);
String formDataToDigest = formData.substring(9, 29);
String formDataDigest = md5(formDataToDigest);
vars.put("icognocheck", formDataDigest);
String response = post(url, vars);
String[] responseValues = response.split("\r");
vars.put("sessionid", stringAtIndex(responseValues, 1));
vars.put("logurl", stringAtIndex(responseValues, 2));
vars.put("vText8", stringAtIndex(responseValues, 3));
vars.put("vText7", stringAtIndex(responseValues, 4));
vars.put("vText6", stringAtIndex(responseValues, 5));
vars.put("vText5", stringAtIndex(responseValues, 6));
vars.put("vText4", stringAtIndex(responseValues, 7));
vars.put("vText3", stringAtIndex(responseValues, 8));
vars.put("vText2", stringAtIndex(responseValues, 9));
vars.put("prevref", stringAtIndex(responseValues, 10));
vars.put("emotionalhistory", stringAtIndex(responseValues, 12));
vars.put("ttsLocMP3", stringAtIndex(responseValues, 13));
vars.put("ttsLocTXT", stringAtIndex(responseValues, 14));
vars.put("ttsLocTXT3", stringAtIndex(responseValues, 15));
vars.put("ttsText", stringAtIndex(responseValues, 16));
vars.put("lineRef", stringAtIndex(responseValues, 17));
vars.put("lineURL", stringAtIndex(responseValues, 18));
vars.put("linePOST", stringAtIndex(responseValues, 19));
vars.put("lineChoices", stringAtIndex(responseValues, 20));
vars.put("lineChoicesAbbrev", stringAtIndex(responseValues, 21));
vars.put("typingData", stringAtIndex(responseValues, 22));
vars.put("divert", stringAtIndex(responseValues, 23));
return stringAtIndex(responseValues, 16);
}
}
Example usage:
package me.linus;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import me.linus.chat.Cleverbot;
public class Chat {
public static void main(String... a) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Me: ");
while(true) {
String console = br.readLine();
console = Cleverbot.think(console);
System.out.println("CB: " + console);
System.out.print("Me: ");
}
}
}
Example output:
Me: Hello
CB: How are you?
Me: Good, you?
CB: I'm doing alright.
Me: That's good to hear!
CB: How are you doing?
Me: Like I just said, I'm doing good
CB: How am I confusing?
Me: I didn't say you were confusing
CB: Yes you did.
Me: No, no I didn't
CB: Yes, you did.
Me: Fuck you
CB: Yes, please.