feat(init): Start a bot for tesla
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Matthieu 'JP' DERASSE
2023-06-05 13:41:48 +00:00
commit 444d9b1563
20 changed files with 2105 additions and 0 deletions

60
alert/alert.go Normal file
View File

@@ -0,0 +1,60 @@
package alert
import (
"context"
"os"
"strconv"
"time"
"git.dev.m-and-m.ovh/mderasse/tesla/api"
log "github.com/sirupsen/logrus"
)
var apiClient *api.Client
// Init will initialize telegram bot.
func Init(alertChan chan api.Availability) {
log.Info("Starting alert ticker")
var err error
apiClient, err = api.NewClient()
if err != nil {
log.Fatalf("Fail to instantiate the HTTP Client. Error: %s", err.Error())
}
priceAlertStr := os.Getenv("PRICE_ALERT")
if priceAlertStr != "" {
PriceAlert, err = strconv.ParseInt(priceAlertStr, 10, 64)
if err != nil {
log.Fatalf("invalid configuration. PRICE_ALERT environement variable should be an integer.")
}
}
// define an interval and the ticker for this interval
interval := time.Duration(1) * time.Minute
// create a new Ticker
tk := time.NewTicker(interval)
for range tk.C {
checkPrice(alertChan)
}
}
func checkPrice(alertChan chan api.Availability) {
log.Info("Ticker - Checking price")
availabilities, err := apiClient.GetAvailabilities(context.Background(), &api.AvailabilityParams{
Query: carFilter,
Count: 1,
})
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]
}
}

26
alert/config.go Normal file
View File

@@ -0,0 +1,26 @@
package alert
import (
"git.dev.m-and-m.ovh/mderasse/tesla/api"
)
// PriceAlert contain the minimum price that will trigger an alert.
var PriceAlert int64 = 47000
var carFilter api.AvailabilityQueryParams = api.AvailabilityQueryParams{
Model: "my",
Condition: "new",
Options: api.OptionsParams{
Trim: []string{"LRAWD"},
},
Arrangeby: "Price",
Order: "asc",
Market: "FR",
Language: "fr",
SuperRegion: "north america",
Lng: 3.0511,
Lat: 50.624,
Zip: "59100",
Range: 0,
Region: "FR",
}