From 82f0e71ccc3c24be2267de7de82a101386a3594e Mon Sep 17 00:00:00 2001 From: Matthieu 'JP' DERASSE Date: Mon, 19 Jun 2023 21:04:59 +0000 Subject: [PATCH] feat(propulsion): Also handle propulsion --- alert/alert.go | 18 ++++++++++++----- alert/config.go | 2 +- bot/bot.go | 54 ++++++++++++++++++++++++++++++++++++++++--------- bot/config.go | 2 +- 4 files changed, 60 insertions(+), 16 deletions(-) diff --git a/alert/alert.go b/alert/alert.go index 27e863d..d27519f 100644 --- a/alert/alert.go +++ b/alert/alert.go @@ -59,16 +59,24 @@ func checkPrice(alertChan chan api.Availability) { availabilities, err := apiClient.GetAvailabilities(context.Background(), &api.AvailabilityParams{ Query: carFilter, - Count: 1, + Count: 100, }) if err != nil { log.Warnf("Fail to contact API. Error: %s", err.Error()) return } - if availabilities.Results[0].Price < PriceAlert { - log.Info("Launching an alert !") - alertChan <- availabilities.Results[0] - alert++ + for _, availability := range availabilities.Results { + if availability.Price < 39000 && availability.Trim[0] == "SRRWD" { + log.Info("Launching an alert !") + alertChan <- availability + alert++ + } + + if availability.Price < 47000 && availability.Trim[0] == "LRAWD" { + log.Info("Launching an alert !") + alertChan <- availability + alert++ + } } } diff --git a/alert/config.go b/alert/config.go index b321413..532b2b5 100644 --- a/alert/config.go +++ b/alert/config.go @@ -11,7 +11,7 @@ var carFilter api.AvailabilityQueryParams = api.AvailabilityQueryParams{ Model: "my", Condition: "new", Options: api.OptionsParams{ - Trim: []string{"LRAWD"}, + Trim: []string{"SRRWD", "LRAWD"}, }, Arrangeby: "Price", Order: "asc", diff --git a/bot/bot.go b/bot/bot.go index 252b0de..b0c0cf2 100644 --- a/bot/bot.go +++ b/bot/bot.go @@ -56,14 +56,15 @@ func Init(alertChan chan api.Availability) { func handleAlert(bot *tele.Bot, config *botConfig, alertChan chan api.Availability) { for availability := range alertChan { - log.Warn("ALLERLRTEFDF") + for _, chatId := range config.AlertChatIds { log.Infof("Sending alert to chat %d", chatId) _, err := bot.Send( tele.ChatID(chatId), fmt.Sprintf( - "ALERT\\!\\! Found a Tesla Long Range in *%s* color is: *%d*€ [View](%s)", + "ALERT\\!\\! Found a %s in *%s* color is: *%d*€ [View](%s)", + availability.TrimName, strings.Join(availability.Paint, " and "), availability.Price, availability.GetOrderLink(), @@ -147,19 +148,53 @@ func help(c tele.Context) error { func price(c tele.Context) error { availabilities, err := apiClient.GetAvailabilities(context.Background(), &api.AvailabilityParams{ Query: carFilter, - Count: 1, + Count: 100, }) if err != nil { log.Warnf("Fail to retrieve availability from tesla website. Error: %s", err.Error()) return c.Send("Fail to retrieve availability from tesla website :(") } + var longRange api.Availability + var propulsion api.Availability + for _, availability := range availabilities.Results { + if longRange.TrimName == "" && availability.Trim[0] == "LRAWD" { + longRange = availability + } + if propulsion.TrimName == "" && availability.Trim[0] == "SRRWD" { + propulsion = availability + } + if propulsion.TrimName != "" && longRange.TrimName != "" { + break + } + } + + var msg string + if propulsion.TrimName == "" { + msg = "There is no Tesla Propulsion available\n" + } else { + msg = fmt.Sprintf("The lowest price currently found for a *%s* in *%s* color is: *%d*€ [View](%s)\n", + propulsion.TrimName, + strings.Join(propulsion.Paint, " and "), + propulsion.Price, + propulsion.GetOrderLink(), + ) + } + + if longRange.TrimName == "" { + msg = fmt.Sprintf("%sThere is no Tesla Long Range available", msg) + } else { + msg = fmt.Sprintf("%sThe lowest price currently found for a *%s* in *%s* color is: *%d*€ [View](%s)\n", + msg, + longRange.TrimName, + strings.Join(longRange.Paint, " and "), + longRange.Price, + longRange.GetOrderLink(), + ) + } + return c.Reply( - fmt.Sprintf("The lowest price currently found for a *Tesla Long Range* in *%s* color is: *%d*€ [View](%s)", - strings.Join(availabilities.Results[0].Paint, " and "), - availabilities.Results[0].Price, - availabilities.Results[0].GetOrderLink(), - ), &homeMenu, + msg, &homeMenu, ) } @@ -176,8 +211,9 @@ func list(c tele.Context) error { availabilitiesStr := fmt.Sprintf("Found *%s* cars\n", availabilities.TotalMatchesFound) for _, availability := range availabilities.Results { availabilitiesStr = fmt.Sprintf( - "%sTesla Long Range, color *%s* available in *%s* for *%d*€ [View](%s)\n", + "%s%s, color *%s* available in *%s* for *%d*€ [View](%s)\n", availabilitiesStr, + availability.TrimName, strings.Join(availability.Paint, " and "), strings.ReplaceAll(availability.City, "-", "\\-"), availability.Price, diff --git a/bot/config.go b/bot/config.go index f3ef45b..1373724 100644 --- a/bot/config.go +++ b/bot/config.go @@ -13,7 +13,7 @@ var carFilter api.AvailabilityQueryParams = api.AvailabilityQueryParams{ Model: "my", Condition: "new", Options: api.OptionsParams{ - Trim: []string{"LRAWD"}, + Trim: []string{"SRRWD", "LRAWD"}, }, Arrangeby: "Price", Order: "asc",