rust-bot/src/main.rs

59 lines
1.9 KiB
Rust
Raw Normal View History

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/";
2016-07-25 15:59:46 -07:00
fn main() {
println!("Hello, world!");
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 {
Command::PRIVMSG(ref target, ref msg) => handle_privmsg(target, msg, &server),
_ => (),
}
}
2016-07-25 15:59:46 -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);
}