/
weather.go
67 lines (57 loc) · 1.83 KB
/
weather.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
package main
import (
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
"github.com/go-resty/resty/v2"
)
var _ larkContentBuilder = (*weather)(nil)
func newWeather(cfg *weatherConfig) *weather {
return &weather{cfg: cfg}
}
// weather 获取天气有关的内容
type weather struct {
cfg *weatherConfig
}
// getLocalWeather 获取天气数据
func (w *weather) getLocalWeather() (*weatherResp, error) {
const authScheme = "APPCODE"
const api = "https://freecityid.market.alicloudapi.com/whapi/json/alicityweather/briefforecast3days"
resp, err := resty.New().R().
EnableTrace().
SetAuthScheme(authScheme).
SetAuthToken(w.cfg.AppCode).
SetFormData(map[string]string{
"cityId": w.cfg.CityID,
"token": "677282c2f1b3d718152c4e25ed434bc4",
}).
Post(api)
if err != nil {
return nil, err
}
if code := resp.StatusCode(); code != http.StatusOK {
return nil, fmt.Errorf("ask weather failed, code: %d", code)
}
respWeather := new(weatherResp)
if errUnmarshal := json.Unmarshal(resp.Body(), respWeather); errUnmarshal != nil {
return nil, errUnmarshal
}
if respWeather.Code != 0 {
return nil, errors.New(respWeather.Msg)
}
return respWeather, nil
}
// build implement larkContentBuilder
func (w *weather) build(_ *lark) []*larkElement {
forecast, err := w.getLocalWeather()
if err != nil || forecast.Data == nil || forecast.Data.Forecast[0] == nil {
log.Printf("failed to get local weather info, err: %v, forcast: %#v", err, forecast)
return nil
}
cityName := forecast.Data.City.Name
todayForecast := forecast.Data.Forecast[0]
content := fmt.Sprintf("☁️ **%s - %s** \n☀️ 白天温度:%s - %s \n🌛 夜间温度:%s - %s", cityName, todayForecast.ConditionDay, todayForecast.TempDay, todayForecast.WindDirDay, todayForecast.TempNight, todayForecast.WindDirNight)
return []*larkElement{newLarkMarkdown(content)}
}