Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.jibble.pircbot.*;
- import java.net.*;
- import java.util.Scanner;
- import java.io.*;
- import com.google.gson.*;
- public class MyBot {
- public static void main(String[] args) throws Exception{
- // Now start our bot up.
- MyBotClass bot = new MyBotClass();
- // Enable debugging output.
- bot.setVerbose(true);
- // Connect to the IRC server.
- bot.connect("chat.freenode.net");
- // Join the #pircbot channel.
- bot.joinChannel("#pircbot");
- }
- }
- class MyBotClass extends PircBot{
- public MyBotClass() {
- this.setName("Bot1");
- }
- public void onMessage(String channel, String sender,
- String login, String hostname, String message) {
- String[] arrOfString = message.split("@");
- if (arrOfString[0].equalsIgnoreCase("weather")) {
- try {
- sendMessage(channel, sender + ": " + WeatherAPI.getWeatherInfo(arrOfString[1]));
- }
- catch(IOException ie) {
- ie.printStackTrace();
- }
- }
- if (arrOfString[0].equalsIgnoreCase("stock")) {
- try {
- sendMessage(channel, sender + ": " + StockAPI.getStockInfo(arrOfString[1]));
- }
- catch(IOException ie) {
- ie.printStackTrace();
- }
- }
- if ((arrOfString[0] == getName()) && (arrOfString[1] == "quit")) {
- this.quitServer();
- }
- }
- }
- class WeatherAPI {
- public static String getWeatherInfo(String cityName) throws IOException{
- String myAPIurl = "https://api.openweathermap.org/data/2.5/weather?q=";
- String myApiToken = "&APPID=26aa1d90a24c98fad4beaac70ddbf274";
- String weatherURL = myAPIurl + cityName + myApiToken;
- URL url = new URL(weatherURL);
- HttpURLConnection conn = (HttpURLConnection) url.openConnection();
- conn.setRequestMethod("GET");
- BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
- String response = new String();
- for (String line; (line = rd.readLine()) != null; response += line) ;
- String output = parseJsonFunction(response);
- return output;
- }
- //Function that will parse your JSON
- public static String parseJsonFunction (String json)
- {
- JsonObject object = new JsonParser().parse(json).getAsJsonObject();
- //System.out.print(object.toString());
- String cityName = object.get("name").getAsString();
- JsonObject main = object.getAsJsonObject("main");
- double temp = main.get("temp").getAsDouble();
- double fahr = ((9 / 5) * (temp - 273) + 32);
- String output = "In the city of " + cityName + " the temperature is " + fahr + " degrees fahrenheit.";
- return output;
- }
- }
- class StockAPI {
- public static String getStockInfo(String tickerSymbl) throws IOException{
- String myAPIurl = "https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol=";
- String myApiToken = "&apikey=UELU6AL4I27DSMI4";
- String stockURL = myAPIurl + tickerSymbl + myApiToken;
- URL url = new URL(stockURL);
- HttpURLConnection conn = (HttpURLConnection) url.openConnection();
- conn.setRequestMethod("GET");
- BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
- String response = new String();
- for (String line; (line = rd.readLine()) != null; response += line) ;
- String output = parseJsonFunction(response);
- return output;
- }
- //Function that will parse your JSON
- public static String parseJsonFunction (String json)
- {
- JsonObject object = new JsonParser().parse(json).getAsJsonObject();
- JsonObject main = object.getAsJsonObject("Global Quote");
- double open = main.get("02. open").getAsDouble();
- double high = main.get("03. high").getAsDouble();
- double low = main.get("04. low").getAsDouble();
- String day = main.get("07. latest trading day").getAsString();
- String ticker = main.get("01. symbol").getAsString();
- String output = "On the day of " + day + " " + ticker +
- " opened with a stock price of $" + open + " USD. " + ticker +
- " reached a high of $" + high + " USD, and a low of $" + low + " USD.";
- return output;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement