Skip to content

Commit

Permalink
worldclock: Use geocoding service because Microsoft broke the API
Browse files Browse the repository at this point in the history
  • Loading branch information
Brawl345 committed Jan 2, 2024
1 parent 884011c commit 46bf44b
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 30 deletions.
2 changes: 1 addition & 1 deletion bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 5 additions & 8 deletions plugin/worldclock/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
48 changes: 30 additions & 18 deletions plugin/worldclock/worldclock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand Down Expand Up @@ -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)
Expand All @@ -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(
Expand Down
5 changes: 2 additions & 3 deletions plugin/worldclock/worldclock.http
Original file line number Diff line number Diff line change
@@ -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}}

0 comments on commit 46bf44b

Please sign in to comment.