feat(propulsion): Also handle propulsion
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Matthieu 'JP' DERASSE 2023-06-19 21:04:59 +00:00
parent 1a013939f5
commit 82f0e71ccc
Signed by: mderasse
GPG Key ID: 55141C777B16A705
4 changed files with 60 additions and 16 deletions

View File

@ -59,16 +59,24 @@ func checkPrice(alertChan chan api.Availability) {
availabilities, err := apiClient.GetAvailabilities(context.Background(), &api.AvailabilityParams{ availabilities, err := apiClient.GetAvailabilities(context.Background(), &api.AvailabilityParams{
Query: carFilter, Query: carFilter,
Count: 1, Count: 100,
}) })
if err != nil { if err != nil {
log.Warnf("Fail to contact API. Error: %s", err.Error()) log.Warnf("Fail to contact API. Error: %s", err.Error())
return return
} }
if availabilities.Results[0].Price < PriceAlert { for _, availability := range availabilities.Results {
log.Info("Launching an alert !") if availability.Price < 39000 && availability.Trim[0] == "SRRWD" {
alertChan <- availabilities.Results[0] log.Info("Launching an alert !")
alert++ alertChan <- availability
alert++
}
if availability.Price < 47000 && availability.Trim[0] == "LRAWD" {
log.Info("Launching an alert !")
alertChan <- availability
alert++
}
} }
} }

View File

@ -11,7 +11,7 @@ var carFilter api.AvailabilityQueryParams = api.AvailabilityQueryParams{
Model: "my", Model: "my",
Condition: "new", Condition: "new",
Options: api.OptionsParams{ Options: api.OptionsParams{
Trim: []string{"LRAWD"}, Trim: []string{"SRRWD", "LRAWD"},
}, },
Arrangeby: "Price", Arrangeby: "Price",
Order: "asc", Order: "asc",

View File

@ -56,14 +56,15 @@ func Init(alertChan chan api.Availability) {
func handleAlert(bot *tele.Bot, config *botConfig, alertChan chan api.Availability) { func handleAlert(bot *tele.Bot, config *botConfig, alertChan chan api.Availability) {
for availability := range alertChan { for availability := range alertChan {
log.Warn("ALLERLRTEFDF")
for _, chatId := range config.AlertChatIds { for _, chatId := range config.AlertChatIds {
log.Infof("Sending alert to chat %d", chatId) log.Infof("Sending alert to chat %d", chatId)
_, err := bot.Send( _, err := bot.Send(
tele.ChatID(chatId), tele.ChatID(chatId),
fmt.Sprintf( 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 "), strings.Join(availability.Paint, " and "),
availability.Price, availability.Price,
availability.GetOrderLink(), availability.GetOrderLink(),
@ -147,19 +148,53 @@ func help(c tele.Context) error {
func price(c tele.Context) error { func price(c tele.Context) error {
availabilities, err := apiClient.GetAvailabilities(context.Background(), &api.AvailabilityParams{ availabilities, err := apiClient.GetAvailabilities(context.Background(), &api.AvailabilityParams{
Query: carFilter, Query: carFilter,
Count: 1, Count: 100,
}) })
if err != nil { if err != nil {
log.Warnf("Fail to retrieve availability from tesla website. Error: %s", err.Error()) log.Warnf("Fail to retrieve availability from tesla website. Error: %s", err.Error())
return c.Send("Fail to retrieve availability from tesla website :(") 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( return c.Reply(
fmt.Sprintf("The lowest price currently found for a *Tesla Long Range* in *%s* color is: *%d*€ [View](%s)", msg, &homeMenu,
strings.Join(availabilities.Results[0].Paint, " and "),
availabilities.Results[0].Price,
availabilities.Results[0].GetOrderLink(),
), &homeMenu,
) )
} }
@ -176,8 +211,9 @@ func list(c tele.Context) error {
availabilitiesStr := fmt.Sprintf("Found *%s* cars\n", availabilities.TotalMatchesFound) availabilitiesStr := fmt.Sprintf("Found *%s* cars\n", availabilities.TotalMatchesFound)
for _, availability := range availabilities.Results { for _, availability := range availabilities.Results {
availabilitiesStr = fmt.Sprintf( 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, availabilitiesStr,
availability.TrimName,
strings.Join(availability.Paint, " and "), strings.Join(availability.Paint, " and "),
strings.ReplaceAll(availability.City, "-", "\\-"), strings.ReplaceAll(availability.City, "-", "\\-"),
availability.Price, availability.Price,

View File

@ -13,7 +13,7 @@ var carFilter api.AvailabilityQueryParams = api.AvailabilityQueryParams{
Model: "my", Model: "my",
Condition: "new", Condition: "new",
Options: api.OptionsParams{ Options: api.OptionsParams{
Trim: []string{"LRAWD"}, Trim: []string{"SRRWD", "LRAWD"},
}, },
Arrangeby: "Price", Arrangeby: "Price",
Order: "asc", Order: "asc",