Tesla/bot/config.go

93 lines
2.4 KiB
Go
Raw Normal View History

2023-06-05 13:41:48 +00:00
package bot
import (
"fmt"
"os"
"strconv"
"strings"
"git.dev.m-and-m.ovh/mderasse/tesla/api"
)
var carFilter api.AvailabilityQueryParams = api.AvailabilityQueryParams{
Model: "my",
Condition: "new",
Options: api.OptionsParams{
Trim: []string{"SRRWD", "LRAWD"},
2023-06-05 13:41:48 +00:00
},
Arrangeby: "Price",
Order: "asc",
Market: "FR",
Language: "fr",
SuperRegion: "north america",
Lng: 3.0511,
Lat: 50.624,
Zip: "59100",
Range: 0,
Region: "FR",
}
// botConfig contain configuration for the telegram bot.
type botConfig struct {
AlertChatIds []int64
BotName string
WhiteListChatIds []int64
Token string
}
func initBotConfig() (*botConfig, error) {
alertChatIdsArrStr := strings.Split(os.Getenv("TELEGRAM_ALERT_CHAT_IDS"), ",")
whitelistChatIdsArrStr := strings.Split(os.Getenv("TELEGRAM_WHITELIST_CHAT_IDS"), ",")
botName := os.Getenv("TELEGRAM_BOT_NAME")
token := os.Getenv("TELEGRAM_TOKEN")
// FOR DEV
// botName = "M_AND_M_Test_bot"
// token = "6273217607:AAGvMIU6bpc7xFWKDhd4llPH6f9zhgUzirs"
// alertChatIdsArrStr = []string{"648385994", "-894368809", "-893895409"}
// whitelistChatIdsArrStr = []string{"648385994", "-894368809", "-893895409"}
if len(alertChatIdsArrStr) == 0 {
return nil, fmt.Errorf("missing TELEGRAM_ALERT_CHAT_IDS environment variable")
}
if len(whitelistChatIdsArrStr) == 0 {
return nil, fmt.Errorf("missing TELEGRAM_WHITELIST_CHAT_IDS environment variable")
}
if botName == "" {
return nil, fmt.Errorf("missing TELEGRAM_BOT_NAME environment variable")
}
if token == "" {
return nil, fmt.Errorf("missing TELEGRAM_TOKEN environment variable")
}
config := botConfig{
AlertChatIds: []int64{},
BotName: botName,
Token: token,
WhiteListChatIds: []int64{},
}
for _, chatIdStr := range alertChatIdsArrStr {
chatId, err := strconv.ParseInt(chatIdStr, 10, 64)
if err != nil {
return nil, fmt.Errorf("impossible to use TELEGRAM_ALERT_CHAT_IDS. format expected: int64,int64")
}
config.AlertChatIds = append(config.AlertChatIds, chatId)
}
for _, chatIdStr := range whitelistChatIdsArrStr {
chatId, err := strconv.ParseInt(chatIdStr, 10, 64)
if err != nil {
return nil, fmt.Errorf("impossible to use TELEGRAM_WHITELIST_CHAT_IDS. format expected: int64,int64")
}
config.WhiteListChatIds = append(config.WhiteListChatIds, chatId)
2023-06-05 13:41:48 +00:00
}
return &config, nil
}