diff --git a/bot/bot.go b/bot/bot.go index 04db887..7bb635c 100644 --- a/bot/bot.go +++ b/bot/bot.go @@ -156,7 +156,7 @@ func New(db *sqlx.DB) (*Gobot, error) { urbandictionary.New(), weather.New(geocodingService, homeService), wikipedia.New(), - worldclock.New(credentialService), + worldclock.New(credentialService, geocodingService), youtube.New(credentialService), } managerService.SetPlugins(plugins) diff --git a/plugin/worldclock/api.go b/plugin/worldclock/api.go index ba95600..36f540d 100644 --- a/plugin/worldclock/api.go +++ b/plugin/worldclock/api.go @@ -10,14 +10,11 @@ type ( ResourceSets []struct { EstimatedTotal int `json:"estimatedTotal"` Resources []struct { - TimeZoneAtLocation []struct { - PlaceName string `json:"placeName"` - TimeZone []struct { - Abbreviation string `json:"abbreviation"` - IanaTimeZoneId string `json:"ianaTimeZoneId"` - ConvertedTime ConvertedTime `json:"convertedTime"` - } `json:"timeZone"` - } `json:"timeZoneAtLocation"` + TimeZone struct { + Abbreviation string `json:"abbreviation"` + IanaTimeZoneId string `json:"ianaTimeZoneId"` + ConvertedTime ConvertedTime `json:"convertedTime"` + } `json:"timeZone"` } `json:"resources"` } `json:"resourceSets"` StatusCode int `json:"statusCode"` diff --git a/plugin/worldclock/worldclock.go b/plugin/worldclock/worldclock.go index 559c67f..d52c594 100644 --- a/plugin/worldclock/worldclock.go +++ b/plugin/worldclock/worldclock.go @@ -17,19 +17,21 @@ import ( ) type Plugin struct { - apiKey string // https://www.bingmapsportal.com/ + apiKey string // https://www.bingmapsportal.com/ + geocodingService model.GeocodingService } var log = logger.New("worldclock") -func New(credentialService model.CredentialService) *Plugin { +func New(credentialService model.CredentialService, geocodingService model.GeocodingService) *Plugin { apiKey, err := credentialService.GetKey("bing_maps_api_key") if err != nil { log.Warn().Msg("bing_maps_api_key not found") } return &Plugin{ - apiKey: apiKey, + apiKey: apiKey, + geocodingService: geocodingService, } } @@ -62,29 +64,42 @@ func (p *Plugin) Handlers(botInfo *telebot.User) []plugin.Handler { func (p *Plugin) onTime(c plugin.GobotContext) error { _ = c.Notify(telebot.Typing) + var location string + if len(c.Matches) > 1 { + location = c.Matches[1] + } else { + location = "Berlin, Deutschland" + } + venue, err := p.geocodingService.Geocode(location) + if err != nil { + if errors.Is(err, model.ErrAddressNotFound) { + return c.Reply("❌ Ort nicht gefunden.", utils.DefaultSendOptions) + } + + guid := xid.New().String() + log.Err(err). + Str("guid", guid). + Str("location", c.Matches[1]). + Msg("Failed to get coordinates for location") + return c.Reply(fmt.Sprintf("❌ Fehler beim Abrufen der Koordinaten.%s", utils.EmbedGUID(guid)), + utils.DefaultSendOptions) + } + requestUrl := url.URL{ Scheme: "https", Host: "dev.virtualearth.net", - Path: "/REST/v1/TimeZone/", + Path: fmt.Sprintf("/REST/v1/TimeZone/%f,%f", venue.Location.Lat, venue.Location.Lng), } q := requestUrl.Query() q.Set("key", p.apiKey) - - var location string - if len(c.Matches) > 1 { - location = c.Matches[1] - } else { - location = "Berlin" - } - q.Set("query", location) q.Set("culture", "de-de") requestUrl.RawQuery = q.Encode() var response Response var httpError *httpUtils.HttpError - err := httpUtils.GetRequest(requestUrl.String(), &response) + err = httpUtils.GetRequest(requestUrl.String(), &response) if err != nil { if errors.As(err, &httpError) && httpError.StatusCode == 404 { return c.Reply("❌ Ort nicht gefunden.", utils.DefaultSendOptions) @@ -100,15 +115,12 @@ func (p *Plugin) onTime(c plugin.GobotContext) error { utils.DefaultSendOptions) } - if len(response.ResourceSets) == 0 || - len(response.ResourceSets[0].Resources) == 0 || - len(response.ResourceSets[0].Resources[0].TimeZoneAtLocation) == 0 || - len(response.ResourceSets[0].Resources[0].TimeZoneAtLocation[0].TimeZone) == 0 { + if len(response.ResourceSets) == 0 || len(response.ResourceSets[0].Resources) == 0 { return c.Reply("❌ Ort nicht gefunden.", utils.DefaultSendOptions) } var sb strings.Builder - timezone := response.ResourceSets[0].Resources[0].TimeZoneAtLocation[0].TimeZone[0] + timezone := response.ResourceSets[0].Resources[0].TimeZone sb.WriteString( fmt.Sprintf( diff --git a/plugin/worldclock/worldclock.http b/plugin/worldclock/worldclock.http index 41ae0bc..cfe1d91 100644 --- a/plugin/worldclock/worldclock.http +++ b/plugin/worldclock/worldclock.http @@ -1,5 +1,4 @@ ### Current time in another country by city name -GET https://dev.virtualearth.net/REST/v1/TimeZone/ - ?query=Kyoto - &culture=de-de +GET https://dev.virtualearth.net/REST/v1/TimeZone/35.021042,135.755600 + ?culture=de-de &key={{bing_maps_api_key}}