func runLocation(ctx context.Context, b *tlbot.Bot, msg *tlbot.Message) { args := msg.Args() if len(args) == 0 { _, err := b.SendMessage(msg.Chat.ID, "nerenin konumunu arayayım?", nil) if err != nil { log.Printf("Error while sending message: %v\n", err) } return } u, err := url.Parse(mapBaseURL) if err != nil { log.Fatal(err) } googleAPIKey := ctx.Value("googleAPIKey").(string) place := strings.Join(args, " ") params := u.Query() params.Set("key", googleAPIKey) params.Set("query", place) u.RawQuery = params.Encode() resp, err := httpclient.Get(u.String()) if err != nil { log.Printf("Error searching place '%v'. Err: %v\n", place, err) return } defer resp.Body.Close() var places placesResponse if err := json.NewDecoder(resp.Body).Decode(&places); err != nil { log.Printf("Error decoding response. Err: %v\n", err) return } if resp.StatusCode != http.StatusOK { log.Printf("Error searching place '%v'. Status: %v\n", place, places.Status) return } if len(places.Results) == 0 { _, err = b.SendMessage(msg.Chat.ID, "bulamadim", nil) if err != nil { log.Printf("Error while sending message: %v\n", err) } return } firstPlace := places.Results[0] location := tlbot.Location{ Lat: firstPlace.Geometry.Location.Lat, Long: firstPlace.Geometry.Location.Long, } _, err = b.SendLocation(msg.Chat.ID, location, nil) if err != nil { log.Printf("Error sending location: %v\n", err) } }
func runEcho(ctx context.Context, b *tlbot.Bot, msg *tlbot.Message) { args := msg.Args() if len(args) == 0 { args = []string{"çok cahilsin"} } txt := fmt.Sprintf("*%v*", strings.Join(args, " ")) _, err := b.SendMessage(msg.Chat.ID, txt, nil) if err != nil { log.Printf("Error while sending message: %v\n", err) return } }
func runForecast(ctx context.Context, b *tlbot.Bot, msg *tlbot.Message) { args := msg.Args() var location string if len(args) == 0 { location = defaultCity } else { location = strings.Join(args, " ") } u, err := url.Parse(forecastURL) if err != nil { log.Printf("Error while parsing URL '%v'. Err: %v", forecastURL, err) return } forecastAPIKey := ctx.Value("openWeatherMapAppID").(string) params := u.Query() params.Set("units", "metric") params.Set("APPID", forecastAPIKey) params.Set("q", location) u.RawQuery = params.Encode() resp, err := http.Get(u.String()) if err != nil { log.Printf("Error while fetching forecast for location '%v'. Err: %v\n", location, err) return } defer resp.Body.Close() var forecast Forecast if err := json.NewDecoder(resp.Body).Decode(&forecast); err != nil { log.Printf("Error while decoding response: %v\n", err) return } txt := forecast.String() if txt == "" { txt = fmt.Sprintf("%v bulunamadı.", location) } opts := &tlbot.SendOptions{ParseMode: tlbot.ModeMarkdown} _, err = b.SendMessage(msg.Chat.ID, txt, opts) if err != nil { log.Printf("Error while sending message. Err: %v\n", err) return } }
func runMovie(ctx context.Context, b *tlbot.Bot, msg *tlbot.Message) { args := msg.Args() opts := &tlbot.SendOptions{} if len(args) == 0 { term := randChoice(movieExamples) txt := fmt.Sprintf("hangi filmi arıyorsun? örneğin: */imdb %s*", term) opts.ParseMode = tlbot.ModeMarkdown _, err := b.SendMessage(msg.Chat.ID, txt, opts) if err != nil { log.Printf("Error while sending message: %v\n", err) } return } googleAPIKey := ctx.Value("googleAPIKey").(string) searchEngineID := ctx.Value("googleSearchEngineID").(string) // the best search engine is still google. // i've tried imdb, themoviedb, rottentomatoes, omdbapi. // themoviedb search engine was the most accurate yet still can't find any // result if any release date is given in query terms. urls, err := search(googleAPIKey, searchEngineID, "", args...) if err != nil { log.Printf("Error searching %v: %v\n", args, err) if err == errSearchQuotaExceeded { _, _ = b.SendMessage(msg.Chat.ID, `¯\_(ツ)_/¯`, opts) } return } for _, url := range urls { if strings.Contains(url, "imdb.com/title/tt") { _, err := b.SendMessage(msg.Chat.ID, url, opts) if err != nil { log.Printf("Error while sending message. Err: %v\n", err) } return } } opts.ParseMode = tlbot.ModeMarkdown _, err = b.SendMessage(msg.Chat.ID, "aradığın filmi bulamadım 🙈", opts) if err != nil { log.Printf("Error while sending message. Err: %v\n", err) return } }
func runWiki(ctx context.Context, b *tlbot.Bot, msg *tlbot.Message) { args := msg.Args() opts := &tlbot.SendOptions{ParseMode: tlbot.ModeMarkdown} if len(args) == 0 { txt := "neye referans vereyim? mesela bana bakın: */bkz İlber Ortaylı*" _, err := b.SendMessage(msg.Chat.ID, txt, opts) if err != nil { log.Printf("Error while sending message. Err: %v\n", err) } return } googleAPIKey := ctx.Value("googleAPIKey").(string) searchEngineID := ctx.Value("googleSearchEngineID").(string) terms := []string{"wikipedia"} terms = append(terms, args...) urls, err := search(googleAPIKey, searchEngineID, "", terms...) if err != nil { log.Printf("Error while 'bkz' query. Err: %v\n", err) if err == errSearchQuotaExceeded { b.SendMessage(msg.Chat.ID, `¯\_(ツ)_/¯`, nil) } return } for _, articleURL := range urls { if strings.Contains(articleURL, "wikipedia.org/wiki/") { _, err = b.SendMessage(msg.Chat.ID, articleURL, nil) if err != nil { log.Printf("Error while sending message. Err: %v\n", err) return } return } } _, err = b.SendMessage(msg.Chat.ID, "aradığın referansı bulamadım 🙈", opts) if err != nil { log.Printf("Error while sending message. Err: %v\n", err) return } }
func runMe(ctx context.Context, b *tlbot.Bot, msg *tlbot.Message) { args := msg.Args() if len(args) == 0 { args = []string{"hmmmmm"} } user := msg.From.FirstName if user == "" { user = msg.From.Username } txt := fmt.Sprintf("`* %v %v`", user, strings.Join(args, " ")) opts := &tlbot.SendOptions{ParseMode: tlbot.ModeMarkdown} _, err := b.SendMessage(msg.Chat.ID, txt, opts) if err != nil { log.Printf("Error while sending message: %v\n", err) return } }
func runYo(ctx context.Context, b *tlbot.Bot, msg *tlbot.Message) { args := msg.Args() opts := &tlbot.SendOptions{ParseMode: tlbot.ModeMarkdown} if len(args) == 0 { term := randChoice(yoExamples) txt := fmt.Sprintf("hangi karikatürü arıyorsun? örneğin: */yo %s*", term) _, err := b.SendMessage(msg.Chat.ID, txt, opts) if err != nil { log.Printf("Error while sending message: %v\n", err) } return } googleAPIKey := ctx.Value("googleAPIKey").(string) searchEngineID := ctx.Value("googleSearchEngineID").(string) terms := []string{"Yiğit", "Özgür"} terms = append(terms, args...) u, err := search(googleAPIKey, searchEngineID, "image", terms...) if err != nil { log.Printf("Error while searching image with given criteria: %v. Err: %v\n", args, err) if err == errSearchQuotaExceeded { _, _ = b.SendMessage(msg.Chat.ID, `¯\_(ツ)_/¯`, nil) } return } photo := tlbot.Photo{ File: tlbot.File{ URL: u[0], }, } _, err = b.SendPhoto(msg.Chat.ID, photo, nil) if err != nil { log.Printf("Error while sending image: %v\n", err) return } }
func runImg(ctx context.Context, b *tlbot.Bot, msg *tlbot.Message) { args := msg.Args() opts := &tlbot.SendOptions{ParseMode: tlbot.ModeNone} if len(args) == 0 { term := randChoice(imgExamples) txt := fmt.Sprintf("ne resmi aramak istiyorsun? örneğin: */img %s*", term) _, err := b.SendMessage(msg.Chat.ID, txt, opts) if err != nil { log.Printf("Error while sending message: %v\n", err) } return } googleAPIKey := ctx.Value("googleAPIKey").(string) searchEngineID := ctx.Value("googleSearchEngineID").(string) urls, err := search(googleAPIKey, searchEngineID, "image", args...) if err != nil { log.Printf("Error while searching image. Err: %v\n", err) if err == errSearchQuotaExceeded { _, _ = b.SendMessage(msg.Chat.ID, `¯\_(ツ)_/¯`, nil) } return } photo := tlbot.Photo{ File: tlbot.File{ URL: urls[0], }, } _, err = b.SendPhoto(msg.Chat.ID, photo, nil) if err != nil { log.Printf("Error while sending photo: %v\n", err) return } }
func runArxiv(ctx context.Context, b *tlbot.Bot, msg *tlbot.Message) { args := msg.Args() opts := &tlbot.SendOptions{ParseMode: tlbot.ModeNone} if len(args) == 0 { opts := &tlbot.SendOptions{ParseMode: tlbot.ModeMarkdown} _, err := b.SendMessage(msg.Chat.ID, "boş geçmeyelim 💩", opts) if err != nil { log.Printf("Error while sending message: %v\n", err) } return } u, err := url.Parse(arxivURL) if err != nil { log.Printf("Error while parsing url '%v'. Err: %v", arxivURL, err) return } qs := strings.Join(args, "+") params := u.Query() params.Set("search_query", qs) params.Set("max_results", "1") // unescape the escaped querystring. arxiv api doesn't recognize an escaped // `+` character, resulting arbitrary documents to show up rawquery, _ := url.QueryUnescape(params.Encode()) u.RawQuery = rawquery resp, err := httpclient.Get(u.String()) if err != nil { log.Printf("Error while fetching arxiv document. Err: %v", err) return } defer resp.Body.Close() var result atom.Feed err = xml.NewDecoder(resp.Body).Decode(&result) if err != nil { log.Printf("Error while decoding the response: %v", err) return } if len(result.Entries) == 0 { _, err := b.SendMessage(msg.Chat.ID, "sonuç boş geldi 👐", opts) if err != nil { log.Printf("Error while sending message: %v\n", err) } return } entry := result.Entries[0] pdflink := "pdf linki yok" for _, link := range entry.Links { if link.Title == "pdf" { pdflink = link.Href break } } var categories []string for _, category := range entry.Categories { categories = append(categories, category.Term) } var buf bytes.Buffer buf.WriteString(fmt.Sprintf("*title:* %v\n", entry.Title)) buf.WriteString(fmt.Sprintf("*categories:* %v\n", strings.Join(categories, ", "))) buf.WriteString(fmt.Sprintf("*published:* %v\n", entry.Published.Format("2006-01-02"))) buf.WriteString(fmt.Sprint("*authors:*\n")) for _, author := range entry.Authors { buf.WriteString(fmt.Sprintf(" - %v\n", author.Name)) } buf.WriteString(fmt.Sprintf("*pdf:* %v", pdflink)) opts.ParseMode = tlbot.ModeMarkdown _, err = b.SendMessage(msg.Chat.ID, buf.String(), opts) if err != nil { log.Printf("Error while sending message: %v\n", err) } }