Attempt to get weather data, move handling messages to a new function

This commit is contained in:
Darren VanBuren 2016-07-26 22:58:06 -07:00
parent 5bbb554186
commit bd16ae1514
2 changed files with 199 additions and 3 deletions

View file

@ -1,6 +1,11 @@
extern crate irc;
extern crate hyper;
use std::io::Read;
use irc::client::prelude::*;
use hyper::{Client};
static WEATHER_API_BASE: &'static str = "http://api.wunderground.com/api/";
fn main() {
println!("Hello, world!");
@ -11,10 +16,43 @@ fn main() {
let message = message.unwrap();
println!("Received message: {}", message);
match message.command {
Command::PRIVMSG(ref target, ref msg) => if msg.contains("rust-bot") {
server.send_privmsg(target, "Hello!").unwrap();
},
Command::PRIVMSG(ref target, ref msg) => handle_privmsg(target, msg, &server),
_ => (),
}
}
}
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);
}