func runPrayerCall(ctx context.Context, b *tlbot.Bot, msg *tlbot.Message) { loc, _ := time.LoadLocation(timezone) now := time.Now().In(loc) nowstr := now.Format(timeFormat) timepair, ok := callTime[nowstr] var txt string switch { case !ok: txt = "galiba oruç bitti" case now.After(timepair.iftar): txt = "okundu" case now.After(timepair.sahur) && now.Hour() < 6: txt = "sahur okundu ama iftara daha çok var" case now.Before(timepair.sahur): txt = "sahur henüz okunmadı" default: // after sahur and before iftar, hence NO txt = randChoice(noes) } _, err := b.SendMessage(msg.Chat.ID, txt, nil) if err != nil { log.Printf("Error while sending message. Err: %v\n", err) return } }
func runRayRay(ctx context.Context, b *tlbot.Bot, msg *tlbot.Message) { _, err := b.SendMessage(msg.Chat.ID, "malifalitiko!", nil) if err != nil { log.Printf("Error while sending message. Err: %v\n", err) return } }
func runToday(ctx context.Context, b *tlbot.Bot, msg *tlbot.Message) { txt := fmt.Sprintf("bugün %v", weekday(time.Now().Weekday()).String()) _, err := b.SendMessage(msg.Chat.ID, txt, nil) if err != nil { log.Printf("Error while sending message. Err: %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 runMovies(ctx context.Context, b *tlbot.Bot, msg *tlbot.Message) { movies, err := fetchOrCache() if err != nil { log.Printf("Error while fetching movies: %v\n", err) return } var buf bytes.Buffer buf.WriteString(" 🎬 İstanbul'da vizyon filmleri\n") for _, movie := range movies { buf.WriteString(fmt.Sprintf("🔸 %v\n", movie)) } _, err = b.SendMessage(msg.Chat.ID, buf.String(), nil) if err != nil { log.Printf("Error while sending message: %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 runCandle(ctx context.Context, b *tlbot.Bot, msg *tlbot.Message) { const timeformat = "2 Jan 2006" loc, _ := time.LoadLocation("Europe/Istanbul") now := time.Now().In(loc).Format(timeformat) var txt string v, ok := dasCandles[now] if !ok { txt = "hayır" } else { txt = fmt.Sprintf("Evet, bugün *%v*\n", v) } 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 runFoodDawn(ctx context.Context, b *tlbot.Bot, msg *tlbot.Message) { loc, _ := time.LoadLocation(timezone) now := time.Now().In(loc) nowstr := now.Format(timeFormat) timepair, ok := callTime[nowstr] var txt string if ok { txt = timepair.sahur.Format("15:04") } else { txt = "galiba oruç bitti" } _, err := b.SendMessage(msg.Chat.ID, txt, nil) if err != nil { log.Printf("Error while sending message. Err: %v\n", err) return } }
func runHoliday(ctx context.Context, b *tlbot.Bot, msg *tlbot.Message) { now := time.Now().UTC() opts := &tlbot.SendOptions{ParseMode: tlbot.ModeMarkdown} for _, t := range holidays { if in(now, t.date, t.date.Add(t.duration)) { _, err := b.SendMessage(msg.Chat.ID, fmt.Sprintf("Bugün %v", t.name), opts) if err != nil { log.Printf("Error while sending message. Err: %v\n", err) } return } if now.Before(t.date) { txt := fmt.Sprintf("En yakın tatil *%v* - %v (*%v* gün)", t.date.Format("_2/01/2006"), t.name, t.duration.Hours()/24) _, err := b.SendMessage(msg.Chat.ID, txt, opts) if err != nil { log.Printf("Error while sending message. Err: %v\n", err) } return } } _, err := b.SendMessage(msg.Chat.ID, "yakın zamanda tatil görünmüyor :(", 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 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 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 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 runHelp(ctx context.Context, b *tlbot.Bot, msg *tlbot.Message) { _, _ = b.SendMessage(msg.Chat.ID, help(), nil) }
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) } }