|
| 1 | +// Package cronplugins |
| 2 | +// @Author Clover |
| 3 | +// @Data 2024/9/17 下午8:27:00 |
| 4 | +// @Desc 定时获取天气模块 |
| 5 | +package cronplugins |
| 6 | + |
| 7 | +import ( |
| 8 | + "encoding/json" |
| 9 | + "fmt" |
| 10 | + aisdk "github.com/Clov614/go-ai-sdk" |
| 11 | + "github.com/Clov614/go-ai-sdk/example_func_call/weather" |
| 12 | + "github.com/Clov614/go-ai-sdk/global" |
| 13 | + "github.com/Clov614/logging" |
| 14 | + "github.com/Clov614/rikka-bot-wechat/rikkabot/config" |
| 15 | + "github.com/Clov614/rikka-bot-wechat/rikkabot/plugins/ai/cron" |
| 16 | + "strings" |
| 17 | +) |
| 18 | + |
| 19 | +type weatherCfg struct { |
| 20 | + Key string `json:"key"` |
| 21 | +} |
| 22 | + |
| 23 | +var ( |
| 24 | + w *weather.Weather |
| 25 | +) |
| 26 | + |
| 27 | +type WeatherCronPlugin struct { |
| 28 | + *cron.CronJob |
| 29 | + *weather.Weather |
| 30 | + city string |
| 31 | +} |
| 32 | + |
| 33 | +func NewWeatherPlugin(city string, cronJob *cron.CronJob) *WeatherCronPlugin { |
| 34 | + return &WeatherCronPlugin{ |
| 35 | + Weather: w, |
| 36 | + CronJob: cronJob, |
| 37 | + city: city, |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +func (p *WeatherCronPlugin) Call(params string) (jsonStr string, err error) { |
| 42 | + type weatherScheduleTaskProperties struct { |
| 43 | + Uuid string `json:"uuid"` |
| 44 | + IsGroup bool `json:"is_group"` |
| 45 | + CronSpec string `json:"cron_spec"` |
| 46 | + CityAddr string `json:"city_addr"` |
| 47 | + } |
| 48 | + var properties weatherScheduleTaskProperties |
| 49 | + err = json.Unmarshal([]byte(params), &properties) |
| 50 | + if err != nil { |
| 51 | + return "", fmt.Errorf("call_err: json.Unmarshal([]byte(params)): %w", err) |
| 52 | + } |
| 53 | + p.Uuid = properties.Uuid |
| 54 | + p.IsGroup = properties.IsGroup |
| 55 | + p.Spec = properties.CronSpec |
| 56 | + p.JobName = "push_weather" |
| 57 | + |
| 58 | + p.Weather = w |
| 59 | + p.city = properties.CityAddr |
| 60 | + p.CreateSchedule(p) |
| 61 | + res := "[系统] 添加" + p.city + "天气推送成功 cron: " + p.Spec |
| 62 | + p.SendText(res) |
| 63 | + return res, nil |
| 64 | +} |
| 65 | + |
| 66 | +func (p *WeatherCronPlugin) Run() { |
| 67 | + weatherResp := p.Weather.GetWeatherByCityAddr(p.city, true) |
| 68 | + if len(weatherResp.Forecasts) > 0 { |
| 69 | + p.SendText(getMultiDayWeatherString(weatherResp.Forecasts)) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +// 美观输出多日天气信息 |
| 74 | +func getMultiDayWeatherString(forecasts []weather.Forecast) string { |
| 75 | + var result strings.Builder |
| 76 | + |
| 77 | + for _, forecast := range forecasts { |
| 78 | + result.WriteString(fmt.Sprintf("🏙️ 城市:%s\n", forecast.City)) |
| 79 | + result.WriteString(fmt.Sprintf("📅 报告时间:%s\n", forecast.ReportTime)) |
| 80 | + for _, cast := range forecast.Casts { |
| 81 | + result.WriteString(fmt.Sprintf("📅 日期:%s\n", cast.Date)) |
| 82 | + result.WriteString(fmt.Sprintf("🌞 白天天气:%s 🌙 夜晚天气:%s\n", cast.DayWeather, cast.NightWeather)) |
| 83 | + result.WriteString(fmt.Sprintf("🌡️ 白天温度:%s°C 🌙 夜晚温度:%s°C\n", cast.DayTemp, cast.NightTemp)) |
| 84 | + result.WriteString(fmt.Sprintf("🌬️ 白天风向:%s 💨 风力:%s\n", cast.DayWind, cast.DayPower)) |
| 85 | + result.WriteString(fmt.Sprintf("🌬️ 夜晚风向:%s 💨 风力:%s\n", cast.NightWind, cast.NightPower)) |
| 86 | + } |
| 87 | + result.WriteString("--------------------------------------------------\n") |
| 88 | + } |
| 89 | + |
| 90 | + return result.String() |
| 91 | +} |
| 92 | + |
| 93 | +func init() { |
| 94 | + // 获取天气相关配置 |
| 95 | + cfg := config.GetConfig() |
| 96 | + wCfgInterface, ok := cfg.GetCustomPluginCfg("weather_ai") |
| 97 | + if !ok { |
| 98 | + cfg.SetCustomPluginCfg("weather_ai", weatherCfg{Key: ""}) |
| 99 | + _ = cfg.Update() // 更新设置 |
| 100 | + logging.Fatal("weather_ai plugin config loaded empty please write the key about weather api in config.yaml", 12) |
| 101 | + } |
| 102 | + bytes, err := json.Marshal(wCfgInterface) |
| 103 | + if err != nil { |
| 104 | + logging.Fatal("weather_ai plugin config marshalling failed", 12) |
| 105 | + } |
| 106 | + var wcfg weatherCfg |
| 107 | + err = json.Unmarshal(bytes, &wcfg) |
| 108 | + if err != nil { |
| 109 | + logging.Fatal("weather_ai plugin config unmarshal fail", 12) |
| 110 | + } |
| 111 | + w = weather.NewWeather(wcfg.Key) |
| 112 | + |
| 113 | + // 注册定时任务触发的ai插件 |
| 114 | + funcCallInfo := aisdk.FuncCallInfo{ |
| 115 | + Function: aisdk.Function{ |
| 116 | + Name: "push_weather_schedule", |
| 117 | + Description: "定期推送天气信息", |
| 118 | + Parameters: aisdk.FunctionParameter{ |
| 119 | + Type: global.ObjType, |
| 120 | + Properties: aisdk.Properties{ |
| 121 | + "uuid": aisdk.Property{ |
| 122 | + Type: global.StringType, |
| 123 | + Description: "标记定时任务的标识", |
| 124 | + }, |
| 125 | + "is_group": aisdk.Property{ |
| 126 | + Type: global.BoolType, |
| 127 | + Description: "是否为群聊", |
| 128 | + }, |
| 129 | + "cron_spec": aisdk.Property{ |
| 130 | + Type: global.StringType, |
| 131 | + Description: "定时任务cron表达式,请根据我的描述生成6字段的cron表达式,只返回表达式字符串:(每天早上八点半执行: 0 30 8 1/1 * ?)", |
| 132 | + }, |
| 133 | + "city_addr": aisdk.Property{ |
| 134 | + Type: global.StringType, |
| 135 | + Description: "地址,如:国家,城市,县、区地址", |
| 136 | + }, |
| 137 | + }, |
| 138 | + Required: []string{"uuid", "is_group", "cron_spec", "city_addr"}, |
| 139 | + }, |
| 140 | + Strict: false, |
| 141 | + }, |
| 142 | + CallFunc: &WeatherCronPlugin{ |
| 143 | + Weather: w, |
| 144 | + CronJob: &cron.CronJob{}, |
| 145 | + }, |
| 146 | + } |
| 147 | + |
| 148 | + aisdk.FuncRegister.Register(&funcCallInfo, []string{"定时任务", "定时", "推送"}) |
| 149 | +} |
0 commit comments