Chatbot

TagsProgrammingJava
Date12-2018

Description

On this project which was created for my Computer Science II class, I created an IRC chatbot using Java and PircBot. The bot is able to do several basic tasks, including responding to various greetings, getting the current time, and echoing whatever the user says.

Once I finished those basic tasks I gave the bot some more complex capabilities, all of which involve interfacing with APIs. The bot is able to...

Validate zip codes and return the weather forecast anywhere in the world

//Get weather data when the user types "weather" and a zip code
if(message.contains("weather") || message.contains("Weather")){
    String[] list = message.split(" ");
	@SuppressWarnings("unused")
	int z = 0;
	String zip = "";
	if(list.length == 1)
		sendMessage(channel, "Please enter a zip code.");
	else if(list.length > 1) {
		for(int i = 0; i < list.length; i++) {
			try { 
				z = Integer.parseInt(list[i]);
				zip = list[i];
			} catch (NumberFormatException e) {}
		}
		try {
			sendMessage(channel, getWeather(zip));
		} catch (Exception e) {
			if(e instanceof FileNotFoundException) {
				sendMessage(channel, "Please enter a valid zip code.");
			}
			else{e.printStackTrace();}
		}
	}

See what the current Twitter trends are

//Gets top 10 trending tags in the US from Twitter
if(message.contains("trending") || message.contains("Trending")) {
	String[] list = null;
	try {
		list = getTrending("23424977");
	} catch (Exception e) {
		e.printStackTrace();
	}
	String trending = "The current trends on twitter are: ";
	for(int i = 0; i < list.length; i++) {
		trending += list[i] + ", ";
	}
	sendMessage(channel, trending);
}

See who all is in space at this very moment

//Gets the names and titles of everybody in space right now
if(message.contains("space") || message.contains("Space")) {
	try {
		sendMessage(channel, "There are " + getSpaceNum() + " people in space right now.");
	} catch (Exception e1) {e1.printStackTrace();}
	String[] space = null;
	try {
		space = getSpace();
	} catch (Exception e) {
		e.printStackTrace();
	}
	for(int i = 0; i < space.length; i++) {
		sendMessage(channel, space[i]);
	}
}

And do username/UUID conversions for Minecraft accounts

if(message.contains("UUID") || message.contains("uuid")) {
	String list[] = message.split(" ");
	String uuid = list[1];
	try {
		sendMessage(channel, getName(uuid));
	} catch (Exception e1) {e1.printStackTrace();}
}

if(message.contains("Name") || message.contains("name")) {
	String list[] = message.split(" ");
	String name = list[1];
	try {
		sendMessage(channel, getUUID(name));
	} catch (Exception e1) {e1.printStackTrace();}
}