2016-07-25 17:56:11 -07:00
|
|
|
extern crate irc;
|
2016-07-26 22:58:06 -07:00
|
|
|
extern crate hyper;
|
2016-07-25 17:56:11 -07:00
|
|
|
|
2016-07-26 22:58:06 -07:00
|
|
|
use std::io::Read;
|
2016-07-25 17:56:11 -07:00
|
|
|
use irc::client::prelude::*;
|
2016-07-26 22:58:06 -07:00
|
|
|
use hyper::{Client};
|
|
|
|
|
|
|
|
static WEATHER_API_BASE: &'static str = "http://api.wunderground.com/api/";
|
2016-07-25 17:56:11 -07:00
|
|
|
|
2016-07-25 15:59:46 -07:00
|
|
|
fn main() {
|
|
|
|
println!("Hello, world!");
|
2016-07-25 17:56:11 -07:00
|
|
|
|
|
|
|
let server = IrcServer::new("config.json").unwrap();
|
|
|
|
server.identify().unwrap();
|
|
|
|
for message in server.iter() {
|
|
|
|
let message = message.unwrap();
|
|
|
|
println!("Received message: {}", message);
|
|
|
|
match message.command {
|
2016-07-26 22:58:06 -07:00
|
|
|
Command::PRIVMSG(ref target, ref msg) => handle_privmsg(target, msg, &server),
|
2016-07-25 17:56:11 -07:00
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
2016-07-25 15:59:46 -07:00
|
|
|
}
|
2016-07-26 22:58:06 -07:00
|
|
|
|
|
|
|
fn handle_privmsg(target: &String, msg: &String, server: &IrcServer) {
|
|
|
|
if msg.contains("rust-bot") {
|
|
|
|
server.send_privmsg(target, "Hello!").unwrap();
|
|
|
|
} else if msg.contains("test_get_weather") {
|
|
|
|
match server.config().options {
|
|
|
|
None => println!("Options not configured!"),
|
|
|
|
Some(ref options) => match options.get("wunderground_api_key") {
|
|
|
|
None => println!("wunderground_api_key not configured!"),
|
|
|
|
Some(api_key) => test_get_weather(api_key),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
server.send_privmsg(target, "Check console for output!").unwrap();
|
|
|
|
} else if msg.contains("bot-quit") {
|
|
|
|
std::process::exit(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_get_weather(api_key: &String) {
|
|
|
|
let client = Client::new();
|
|
|
|
let data_type = "conditions";
|
|
|
|
let location = "WA/Maple_Valley";
|
|
|
|
let url = &format!("{}{}/{}/q/{}.json", WEATHER_API_BASE, api_key, data_type, location);
|
|
|
|
println!("Attempting to fetch {}", url);
|
|
|
|
let mut response = match client.get(url).send() {
|
|
|
|
Ok(response) => response,
|
|
|
|
Err(_) => panic!("Error!"),
|
|
|
|
};
|
|
|
|
let mut buf = String::new();
|
|
|
|
match response.read_to_string(&mut buf) {
|
|
|
|
Ok(_) => (),
|
|
|
|
Err(_) => panic!("Error reading to buffer"),
|
|
|
|
};
|
|
|
|
println!("buf: {}", buf);
|
|
|
|
}
|