func GetWeather(result chan string, mes string) { // catch errors defer func() { if r := recover(); r != nil { log.Log.Warning("Weather Input: " + mes) log.Log.Warning("Weather Error: " + fmt.Sprint(r)) result <- "I'm sorry, I couldn't find that location." } }() res := WeaReg.FindAllStringSubmatch(mes, -1) input := strings.TrimSpace(res[0][2]) body := util.GetDataFromURL(weatherUrl, []string{ weatherBasePath, config.GetConfig().ApiInfo.Weather, weatherSecondPath, input, ".json", }, map[string]string{}) var jsonDat map[string]interface{} err := json.Unmarshal(body, &jsonDat) if err != nil { panic(err) } currentObs := jsonDat["current_observation"].(map[string]interface{}) displayLoc := currentObs["display_location"].(map[string]interface{}) city := displayLoc["city"].(string) state := displayLoc["state"].(string) temp := currentObs["temperature_string"].(string) humidity := currentObs["relative_humidity"].(string) wind := currentObs["wind_string"].(string) conditions := currentObs["weather"].(string) lastUpdate := currentObs["observation_time"].(string) str := "Current weather for " + city + ", " + state + ": " + conditions + " with a temperature of " + temp + " and winds " + wind + ". Humidity is " + humidity + ". " + lastUpdate log.Log.Info("Weather Input: " + mes) log.Log.Info("Weather Output: " + str) result <- str return }
func GetShortURL(longUrl string) string { fullUrl, err := url.Parse(shortenBaseUrl) if err != nil { panic(err) } fullUrl.Path += shortenPath parameters := url.Values{} parameters.Add("fields", "id") parameters.Add("key", config.GetConfig().ApiInfo.Google) fullUrl.RawQuery = parameters.Encode() jsonData := []byte(`{"longUrl":"` + longUrl + `"}`) req, err := http.NewRequest("POST", fullUrl.String(), bytes.NewBuffer(jsonData)) if err != nil { panic(err) } req.Header.Set("Content-Type", "application/json") client := &http.Client{} resp, err := client.Do(req) if err != nil { panic(err) } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { panic(err) } var jsonDat map[string]interface{} err = json.Unmarshal(body, &jsonDat) if err != nil { panic(err) } return jsonDat["id"].(string) }
func WolframQuery(result chan string, mes string) { // catch errors defer func() { if r := recover(); r != nil { log.Log.Warning("Wolfram Input: " + mes) log.Log.Warning("Wolfram Error: " + fmt.Sprint(r)) result <- "I'm sorry, I couldn't find the answer to that query." } }() res := WolfReg.FindAllStringSubmatch(mes, -1) input := strings.TrimSpace(res[0][1]) body := util.GetDataFromURL( wolframUrl, []string{wolframBasePath}, map[string]string{ "input": input, "appid": config.GetConfig().ApiInfo.Wolfram, }) var queryResult QueryResult err := xml.Unmarshal(body, &queryResult) if err != nil { panic(err) } var subpod *SubPod = nil var visualPod *Pod = nil var convPod *Pod = nil for _, pod := range queryResult.Pods { if pod.Title == "Result" || pod.Title == "Current result" || pod.Title == "Results" || pod.Title == "Average result" || pod.Title == "Value" || pod.Title == "Alternate forms" || pod.Title == "Solution" || pod.Title == "Alternate form" || pod.Title == "Plot" { subpod = &pod.SubPods[0] } if pod.Title == "Visual representation" || strings.HasPrefix(pod.Title, "Plots") { // for range copies values, so we can't use memory location here visualPod = new(Pod) visualPod.Title = pod.Title visualPod.SubPods = pod.SubPods } if pod.Title == "Unit conversions" { convPod = new(Pod) convPod.Title = pod.Title convPod.SubPods = pod.SubPods } } if subpod == nil { subpod = &queryResult.Pods[0].SubPods[0] } text := newLnReg.ReplaceAllString(html.UnescapeString(subpod.Text.Value), " ") img := util.GetShortURL(subpod.Img.Src) str := text + " | " + img append := func(pod *Pod) { for _, subpod := range pod.SubPods { subText := newLnReg.ReplaceAllString(html.UnescapeString(subpod.Text.Value), " ") subImg := util.GetShortURL(subpod.Img.Src) if strings.TrimSpace(subText) != "" { str += " | " + subText } if strings.TrimSpace(subImg) != "" { str += " | " + subImg } } } if visualPod != nil { append(visualPod) } if convPod != nil { append(convPod) } log.Log.Info("Wolfram Input: " + mes) log.Log.Info("Wolfram Output: " + str) result <- str return }
func main() { if !config.CheckConfig() { log.Log.Error("Base config file written, please fill it in.") return } jsonConfig := config.GetConfig() log.Log.Info("Connecting to: " + jsonConfig.BotInfo.ServerURL + " with nick: " + jsonConfig.BotInfo.Nick + " and user: "******"Failed connecting") log.Log.Error(fmt.Sprint(err)) return } log.Log.Info("Successfully connected.") // Identify and Join on connect con.AddCallback("001", func(e *irc.Event) { if jsonConfig.BotInfo.Ident != "" { log.Log.Info("Authenticating with password: "******"NickServ", "identify "+jsonConfig.BotInfo.User+" "+jsonConfig.BotInfo.Ident) time.Sleep(time.Second * 5) } for _, channel := range jsonConfig.BotInfo.Channels { log.Log.Info("Joining channel: " + channel) con.Join(channel) } }) // Channel message con.AddCallback("PRIVMSG", func(e *irc.Event) { result := make(chan string) go func() { defer func() { if r := recover(); r != nil { log.Log.Warning("Uncaught error: " + fmt.Sprint(r)) result <- "cancel" } }() res := parseMesReg.FindAllStringSubmatch(e.Message(), -1) mes := strings.TrimSpace(res[0][2]) // Calculator if commands.CalcReg.MatchString(mes) { commands.Calculate(result, mes) return } // Weather if commands.WeaReg.MatchString(mes) { commands.GetWeather(result, mes) return } // Wolfram Alpha if commands.WolfReg.MatchString(mes) { commands.WolframQuery(result, mes) return } // Google if commands.GoogReg.MatchString(mes) { commands.Google(result, mes) return } // Shorten URL if commands.ShortenReg.MatchString(mes) { commands.ShortenURL(result, mes) return } // Brainfuck if commands.BrainReg.MatchString(mes) { commands.RunBrainfuck(result, mes) return } result <- "cancel" }() final := <-result if final != "cancel" { channel := chanReg.FindAllStringSubmatch(e.Raw, -1)[0][1] con.Privmsg(channel, final) } }) // Join channel on invite con.AddCallback("INVITE", func(e *irc.Event) { log.Log.Info("Joining " + e.Message() + " and adding it to channel join list.") con.Join(e.Message()) jsonConfig := config.GetConfig() inList := false for _, channel := range jsonConfig.BotInfo.Channels { if channel == e.Message() { inList = true break } } if !inList { jsonConfig.BotInfo.Channels = append(jsonConfig.BotInfo.Channels, e.Message()) config.WriteConfig() } }) // Remove channel on kick con.AddCallback("KICK", func(e *irc.Event) { log.Log.Info("Kicked from " + e.Message() + ", removing it from channel join list.") jsonConfig := config.GetConfig() channel := chanReg.FindAllStringSubmatch(e.Raw, -1)[0][1] for i, bad := range jsonConfig.BotInfo.Channels { if bad == channel { jsonConfig.BotInfo.Channels = append(jsonConfig.BotInfo.Channels[:i], jsonConfig.BotInfo.Channels[i+1:]...) break } } config.WriteConfig() }) con.Loop() }