/
utils.go
196 lines (159 loc) · 5.21 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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package main
import (
"encoding/json"
"io/ioutil"
"net/http"
"net/url"
"reflect"
"strconv"
"time"
"github.com/bitly/go-simplejson"
"github.com/go-telegram-bot-api/telegram-bot-api"
)
// CityName is search city
type CityName struct {
Name string
}
// WeatherInfo is Bot API response information for user
type WeatherInfo struct {
City string
Time string
Tempture string
Humidity string
Status int
WindSpeed string
WindDirection float64
Sunrise string
Sunset string
Link string
}
// Forest :
type Forest struct {
code int
day string
low string
high string
date string
}
// ReadBotToken is read bot token(token.json)
func ReadBotToken(path string) (string, error) {
var data map[string]string
file, err := ioutil.ReadFile(path)
if err != nil {
return "", err
}
if err := json.Unmarshal(file, &data); err != nil {
return "", err
}
return data["token"], nil
}
// BuildURL is generate API URL
func BuildURL(param interface{}) (urlParsed string) {
URL, _ := url.Parse("https://query.yahooapis.com/v1/public/yql")
parameters := url.Values{}
switch t := param.(type) {
case tgbotapi.Location:
latitude := strconv.FormatFloat(t.Latitude, 'E', -1, 64)
longitude := strconv.FormatFloat(t.Longitude, 'E', -1, 64)
parameters.Add(
"q",
"select * from weather.forecast where u=\"u\" AND woeid in (select woeid from geo.places(1) where text=\"("+latitude+","+longitude+")\")",
)
case CityName:
parameters.Add(
"q",
"select * from weather.forecast where u=\"u\" AND woeid in (select woeid from geo.places(1) where text=\"("+t.Name+")\")",
)
}
parameters.Add("format", "json")
URL.RawQuery = parameters.Encode()
urlParsed = URL.String()
return urlParsed
}
// HTTPGet is
func HTTPGet(weatherURL string) ([]byte, error) {
response, err := http.Get(weatherURL)
if err != nil {
return nil, err
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, err
}
return body, nil
}
// HandleQueryResult is handle HTTPGet body
func (w *WeatherInfo) HandleQueryResult(body []byte) ([]interface{}, error) {
json, err := simplejson.NewJson(body)
if err != nil {
return nil, err
}
city, _ := json.Get("query").Get("results").Get("channel").Get("location").Get("city").String()
tempture, _ := json.Get("query").Get("results").Get("channel").Get("item").Get("condition").Get("temp").String()
humidity, _ := json.Get("query").Get("results").Get("channel").Get("atmosphere").Get("humidity").String()
status, _ := json.Get("query").Get("results").Get("channel").Get("item").Get("condition").Get("code").String()
windSpeed, _ := json.Get("query").Get("results").Get("channel").Get("wind").Get("speed").String()
direction, _ := json.Get("query").Get("results").Get("channel").Get("wind").Get("direction").String()
sunrise, _ := json.Get("query").Get("results").Get("channel").Get("astronomy").Get("sunrise").String()
sunset, _ := json.Get("query").Get("results").Get("channel").Get("astronomy").Get("sunset").String()
link, _ := json.Get("query").Get("results").Get("channel").Get("link").String()
forest, _ := json.Get("query").Get("results").Get("channel").Get("item").Get("forecast").Array()
if _, err := strconv.ParseFloat(direction, 64); err != nil {
return nil, err
}
if _, err := strconv.Atoi(status); err != nil {
return nil, err
}
windDirection, _ := strconv.ParseFloat(direction, 64)
emojiStatusCode, _ := strconv.Atoi(status)
w.City = city
w.Time = time.Now().Format("2006-01-02 15:04:05")
w.Tempture = tempture
w.Humidity = humidity
w.Status = emojiStatusCode
w.WindSpeed = windSpeed
w.WindDirection = windDirection
w.Sunrise = sunrise
w.Sunset = sunset
w.Link = link
return forest, nil
}
// ResponseWeatherText is response for user's template
func (w *WeatherInfo) ResponseWeatherText(weatherInfo *WeatherInfo) string {
emoji, _ := weatherEmoji(weatherInfo.Status)
weatherMessage := `🚩 *` + weatherInfo.City + `*
- - - - - - - - - - - - - - - - - - - - - -
🕘 目前時間 ➡️ ` + weatherInfo.Time + `
🔰 目前溫度 ➡️ ` + weatherInfo.Tempture + `°C
💧 目前濕度 ➡️ ` + weatherInfo.Humidity + `%
🌀 天氣狀態 ➡️ ` + emoji + `
💨 目前風速 ➡️ ` + weatherInfo.WindSpeed + ` km/h
🔃 風速風向 ➡️ ` + CheckWindDirection(weatherInfo.WindDirection) + `
- - - - - - - - - - - - - - - - - - - - - -
🌅 日出時間 ➡️ ` + weatherInfo.Sunrise + `
🌄 日落時間 ➡️ ` + weatherInfo.Sunset + `
詳細資訊 🔍 [Yahoo Weather](` + weatherInfo.Link + `)
`
return weatherMessage
}
// HandleQueryForest :
func (f *Forest) HandleQueryForest(forestArray []interface{}) string {
var forestResponse string
for _, v := range forestArray[0:8] {
s := reflect.ValueOf(v).Interface().(map[string]interface{})
f.code, _ = strconv.Atoi(s["code"].(string))
f.high = s["high"].(string)
f.low = s["low"].(string)
f.date = s["date"].(string)
f.day = s["day"].(string)
emoji, _ := weatherEmoji(f.code)
text := `
📅 *` + f.date + ` - ` + f.day + `
🔰 最低溫和最高溫 ➡️ ` + f.low + `°C - ` + f.high + `°C
🌀天氣狀態 ➡️ ` + emoji + `
` + "\n"
forestResponse = forestResponse + text
}
return forestResponse
}