-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweatherBot.go
125 lines (110 loc) · 3.52 KB
/
weatherBot.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"math"
"net/http"
"net/url"
"os"
"time"
"github.com/frclager/golangWeatherTelegramBot/models"
"github.com/joho/godotenv"
)
const MAX = 3
var cities = map[string]string{
"Madrid": "3117735",
"MexicoCity": "3530597",
"NewYork": "5128581",
"Toronto": "6167865",
}
func main() {
err := godotenv.Load(".env")
if err != nil {
log.Fatal("Error loading .env file")
}
KEY_WEATHER := os.Getenv("KEY_WEATHER")
TELEGRAM_BOT_TOKEN := os.Getenv("TELEGRAM_BOT_TOKEN")
TELEGRAM_CHAT_ID := os.Getenv("TELEGRAM_CHAT_ID")
cityName := *flag.String("cityName", "MexicoCity", "City")
option := *flag.String("request", "WF", "Type")
flag.Parse()
var idCity = cities[cityName]
if idCity != "" {
now := time.Now()
message := "WeatherBot " + now.Format(time.Kitchen)
switch option {
case "W":
message += callAPI(KEY_WEATHER, "weather", idCity)
case "F":
message += callAPI(KEY_WEATHER, "forecast", idCity)
case "WF":
message += callAPI(KEY_WEATHER, "weather", idCity) + callAPI(KEY_WEATHER, "forecast", idCity)
default:
message = ""
}
if message != "" {
sendMessage(TELEGRAM_BOT_TOKEN, TELEGRAM_CHAT_ID, message)
}
}
}
func callAPI(key string, endPoint string, idCity string) string {
url := "https://api.openweathermap.org/data/2.5/" + endPoint + "?id=" + idCity + "&APPID=" + key + "&lang=en"
resp, err := http.Get(url)
if err != nil {
panic(err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
panic("Weather request was not OK: " + resp.Status)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
var result = ""
if endPoint == "weather" {
result = parseResponseWeather(body)
} else if endPoint == "forecast" {
result = parseResponseForecast(body)
}
return result
}
func sendMessage(telBotToken string, telChat string, message string) {
url := "https://api.telegram.org/bot" + telBotToken + "/sendMessage?chat_id=" + telChat + "&text=" + url.QueryEscape(message)
resp, err := http.Get(url)
if err != nil {
panic(err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
panic("Telegram request was not OK: " + resp.Status)
}
}
func parseResponseWeather(body []byte) string {
var s = new(models.WeatherAPIResponse)
err := json.Unmarshal(body, &s)
if err != nil {
panic(err)
}
var message = "\n" + s.Name + " (" + s.Sys.Country + ") " + s.Weather[0].Description + ". 🌡 Temperature (ºC) " + fmt.Sprintf("%.0f", toCelsius(s.Main.Temp)) + ". Feels like " + fmt.Sprintf("%.0f", toCelsius(s.Main.FeelsLike)) + " (L " + fmt.Sprintf("%.0f", toCelsius(s.Main.TempMin)) + " - H " + fmt.Sprintf("%.0f", toCelsius(s.Main.TempMax)) + "), " + fmt.Sprintf("%2d", s.Main.Humidity) + " humidity. "
return message
}
func parseResponseForecast(body []byte) string {
var s = new(models.ForecastAPIResponse)
err := json.Unmarshal(body, &s)
if err != nil {
panic(err)
}
var message = ""
for _, value := range s.List[0:MAX] {
var partialMessage = " ⌚️ " + value.DtTxt + " " + value.Weather[0].Description + ". 🌡 Temperature (ºC) " + fmt.Sprintf("%.0f", toCelsius(value.Main.Temp)) + ". Feels like " + fmt.Sprintf("%.0f", toCelsius(value.Main.FeelsLike)) + " (L " + fmt.Sprintf("%.0f", toCelsius(value.Main.TempMin)) + " - H " + fmt.Sprintf("%.0f", toCelsius(value.Main.TempMax)) + "), " + fmt.Sprintf("%2d", value.Main.Humidity) + " humidity."
message += "\n" + partialMessage
}
return message
}
func toCelsius(kelvin float64) float64 {
return math.Round(kelvin - 273.15)
}