/
utils.go
73 lines (67 loc) · 1.54 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
owm "github.com/briandowns/openweathermap"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
var (
weatherIcons = map[int]string{
2: "⚡️",
3: "☔️",
5: "🌧",
6: "❄️",
8: "🌤",
}
)
func getLogs() string {
resp, err := http.Get(os.Getenv(gistNewsURL))
if err != nil {
log.Printf("couldn't retrieve news %v", err)
return "не могу получить новости"
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Printf("couldn't retrieve news %v", err)
return "не могу получить новости"
}
return string(body)
}
func findKeyPhrase(message *tgbotapi.Message) string {
for k, v := range phrases {
if strings.Contains(message.Text, k) && !strings.Contains(message.Text, "gasoid") {
return v
}
}
return ""
}
func getWeather(cityName string) (*string, error) {
var (
icon string
ok bool
)
apiKey := os.Getenv(owmApiKey)
w, err := owm.NewCurrent("C", "ru", apiKey)
if err != nil {
log.Println("couldn't load weather", err)
return nil, fmt.Errorf("couldn't load weather %w", err)
}
w.CurrentByName(cityName)
wDescr := ""
for _, wW := range w.Weather {
if wW.Description == "" {
continue
}
if icon, ok = weatherIcons[wW.ID/100]; !ok {
icon = defaultWeatherIcon
}
wDescr = fmt.Sprintf("%s%s%s ", wDescr, icon, wW.Description)
}
description := fmt.Sprintf(weatherTmpl, w.Name, wDescr, w.Main.Temp, w.Wind.Speed)
return &description, nil
}