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 ( "bytes" "encoding/json" "fmt" "io" "io/ioutil" "net/http" )
func main() { url := "https://devapi.qweather.com/v7/weather/3d?location=101010100&key=xxxxxxxxxxxxx" method := "GET"
client := &http.Client{} req, err := http.NewRequest(method, url, nil)
if err != nil { fmt.Println(err) return } res, err := client.Do(req) if err != nil { fmt.Println(err) return } defer res.Body.Close()
body, err := io.ReadAll(res.Body) var weather WeatherInfo json.Unmarshal(body, &weather) FeishuNotify(weather) if err != nil { fmt.Println(err) return } }
func BuildWeatherString(weather WeatherInfo, idx int) string { s1 := fmt.Sprintf("%v —— 最高温:%v,最低温:%v,白天:%v %v %v级,夜间:%v %v %v级", weather.Daily[idx].FxDate, weather.Daily[idx].TempMax, weather.Daily[idx].TempMin, weather.Daily[idx].TextDay, weather.Daily[idx].WindDirDay, weather.Daily[idx].WindScaleDay, weather.Daily[idx].TextNight, weather.Daily[idx].WindDirNight, weather.Daily[idx].WindScaleNight) return s1 }
type WeatherInfo struct { Code string `json:"code"` UpdateTime string `json:"updateTime"` FxLink string `json:"fxLink"` Daily []struct { FxDate string `json:"fxDate"` Sunrise string `json:"sunrise"` Sunset string `json:"sunset"` Moonrise string `json:"moonrise"` Moonset string `json:"moonset"` MoonPhase string `json:"moonPhase"` MoonPhaseIcon string `json:"moonPhaseIcon"` TempMax string `json:"tempMax"` TempMin string `json:"tempMin"` IconDay string `json:"iconDay"` TextDay string `json:"textDay"` IconNight string `json:"iconNight"` TextNight string `json:"textNight"` Wind360Day string `json:"wind360Day"` WindDirDay string `json:"windDirDay"` WindScaleDay string `json:"windScaleDay"` WindSpeedDay string `json:"windSpeedDay"` Wind360Night string `json:"wind360Night"` WindDirNight string `json:"windDirNight"` WindScaleNight string `json:"windScaleNight"` WindSpeedNight string `json:"windSpeedNight"` Humidity string `json:"humidity"` Precip string `json:"precip"` Pressure string `json:"pressure"` Vis string `json:"vis"` Cloud string `json:"cloud"` UvIndex string `json:"uvIndex"` } `json:"daily"` Refer struct { Sources []string `json:"sources"` License []string `json:"license"` } `json:"refer"` }
func FeishuNotify(weather WeatherInfo) { url := "https://open.feishu.cn/open-apis/bot/v2/hook/xxxxxxxxxxx" method := "POST"
var msg WeatherCard
msg.MsgType = "interactive" msg.Card.Config.WideScreenMode = true msg.Card.Header.Template = "blue" msg.Card.Header.Title.Tag = "plain_text" msg.Card.Header.Title.Content = "最近三天天气"
buildColumn := func(idx int) []InnerElement { return []InnerElement{ { Tag: "div", Text: Text{ Content: weather.Daily[idx].FxDate, Tag: "plain_text", }, }, { Tag: "div", Text: Text{ Content: fmt.Sprintf("最高温:%v,最低温:%v", weather.Daily[idx].TempMax, weather.Daily[idx].TempMin), Tag: "plain_text", }, }, { Tag: "div", Text: Text{ Content: fmt.Sprintf("白天:%v %v %v级", weather.Daily[idx].TextDay, weather.Daily[idx].WindDirDay, weather.Daily[idx].WindScaleDay), Tag: "plain_text", }, }, { Tag: "div", Text: Text{ Content: fmt.Sprintf("夜间:%v %v %v级", weather.Daily[idx].TextNight, weather.Daily[idx].WindDirNight, weather.Daily[idx].WindScaleNight), Tag: "plain_text", }, }, } } msg.Card.Elements = append(msg.Card.Elements, buildColumn(0)...) msg.Card.Elements = append(msg.Card.Elements, InnerElement{ Tag: "hr", }) msg.Card.Elements = append(msg.Card.Elements, buildColumn(1)...) msg.Card.Elements = append(msg.Card.Elements, InnerElement{ Tag: "hr", }) msg.Card.Elements = append(msg.Card.Elements, buildColumn(2)...) b, _ := json.Marshal(msg) payload := bytes.NewReader(b)
client := &http.Client{} req, err := http.NewRequest(method, url, payload)
if err != nil { fmt.Println(err) return } req.Header.Add("Content-Type", "application/json")
res, err := client.Do(req) if err != nil { fmt.Println(err) return } defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body) if err != nil { fmt.Println(err) return } fmt.Println(string(body)) }
type WeatherCard struct { MsgType string `json:"msg_type"` Card struct { Config struct { WideScreenMode bool `json:"wide_screen_mode"` } `json:"config"` Header struct { Template string `json:"template"` Title struct { Tag string `json:"tag"` Content string `json:"content"` } `json:"title"` } `json:"header"` Elements []InnerElement } `json:"card"` }
type InnerElement struct { Tag string `json:"tag"` Text Text `json:"text"` }
type Text struct { Content string `json:"content"` Tag string `json:"tag"` }
|