Browse Source

Standardize handleError() command to repeat timeouts or have better messages

master
Gregory Rudolph 3 years ago
parent
commit
fe89ce761f
Signed by: rudi
GPG Key ID: EF64F3CBD1A1EBDD
  1. 113
      commands.go

113
commands.go

@ -14,16 +14,12 @@ import ( @@ -14,16 +14,12 @@ import (
func reset(m chat1.MsgSummary) {
_, err := k.KVDelete(&m.Channel.Name, "teslabot", "authtok")
if err != nil {
tracker := uuid.NewString()
k.SendMessageByConvID(m.ConvID, "There was an error resetting your authentication. Contact @rudi9719 for more information with code %+v", tracker)
log.LogError("%+v: %+v", tracker, err)
handleError(err,m,"There was an error resetting your authentication. Contact @rudi9719 for more information with code %+v")
return
}
_, err = k.KVDelete(&m.Channel.Name, "teslabot", "startPass")
if err != nil {
tracker := uuid.NewString()
k.SendMessageByConvID(m.ConvID, "There was an error resetting your authentication. Contact @rudi9719 for more information with code %+v", tracker)
log.LogError("%+v: %+v", tracker, err)
handleError(err,m,"There was an error resetting your authentication. Contact @rudi9719 for more information with code %+v")
return
}
}
@ -46,17 +42,13 @@ func authenticate(m chat1.MsgSummary) { @@ -46,17 +42,13 @@ func authenticate(m chat1.MsgSummary) {
password := parts[2]
t, err := login(context.Background(), username, password)
if err != nil {
tracker := uuid.NewString()
k.SendMessageByConvID(m.ConvID, "There was an error logging in. Contact @rudi9719 for more information with code %+v", tracker)
log.LogError("%+v: %+v", tracker, err)
handleError(err,m,"There was an error logging in. Contact @rudi9719 for more information with code %+v")
return
}
log.LogDebug("Token created for %+v", m.Sender.Username)
_, err = k.KVPut(&m.Channel.Name, "teslabot", "authtok", t)
if err != nil {
tracker := uuid.NewString()
k.SendMessageByConvID(m.ConvID, "There was an error storing your auth token. Contact @rudi9719 for more information with code %+v", tracker)
log.LogError("%+v: %+v", tracker, err)
handleError(err,m,"There was an error storing your auth token. Contact @rudi9719 for more information with code %+v")
return
}
k.ReactByConvID(m.ConvID, m.Id, ":car:")
@ -71,9 +63,7 @@ func listVehicles(m chat1.MsgSummary) { @@ -71,9 +63,7 @@ func listVehicles(m chat1.MsgSummary) {
}
v, err := c.Vehicles()
if err != nil {
tracker := uuid.NewString()
k.SendMessageByConvID(m.ConvID, "There was an error listing vehicles. Contact @rudi9719 for more information with code %+v", tracker)
log.LogError("%+v: %+v", tracker, err)
handleError(err,m,"There was an error listing vehicles. Contact @rudi9719 for more information with code %+v")
return
}
ret := "Detected vehicles for account: ```"
@ -93,9 +83,7 @@ func deferTime(m chat1.MsgSummary) { @@ -93,9 +83,7 @@ func deferTime(m chat1.MsgSummary) {
m.Content.Text.Body = fmt.Sprintf("!%+v", command)
timer, err := time.ParseDuration(t)
if err != nil {
tracker := uuid.NewString()
k.SendMessageByConvID(m.ConvID, "There was an error parsing your time input. Contact @rudi9719 for more information with code %+v", tracker)
log.LogError("%+v: %+v", tracker, err)
handleError(err,m,"There was an error parsing your time input. Contact @rudi9719 for more information with code %+v")
return
}
k.SendMessageByConvID(m.ConvID, fmt.Sprintf("I will run `%+v` at %+v", command, time.Now().Add(timer).Format("Jan _2 15:04:05")))
@ -111,9 +99,7 @@ func honk(m chat1.MsgSummary) { @@ -111,9 +99,7 @@ func honk(m chat1.MsgSummary) {
}
err := v.HonkHorn()
if err != nil {
tracker := uuid.NewString()
k.SendMessageByConvID(m.ConvID, "There was an error honking your horn. Contact @rudi9719 for more information with code %+v", tracker)
log.LogError("%+v: %+v", tracker, err)
handleError(err, m,"There was an error honking your horn. Contact @rudi9719 for more information with code %+v")
return
}
k.SendMessageByConvID(m.ConvID, "I've honked your horn!")
@ -126,9 +112,7 @@ func chargeStatus(m chat1.MsgSummary) { @@ -126,9 +112,7 @@ func chargeStatus(m chat1.MsgSummary) {
}
state, err := v.ChargeState()
if err != nil {
tracker := uuid.NewString()
k.SendMessageByConvID(m.ConvID, "There was an error getting charge state. Contact @rudi9719 for more information with code %+v", tracker)
log.LogError("%+v: %+v", tracker, err)
handleError(err,m,"There was an error getting charge state. Contact @rudi9719 for more information with code %+v")
return
}
ret := fmt.Sprintf("Status for %+v: ```", v.DisplayName)
@ -160,9 +144,7 @@ func flashLights(m chat1.MsgSummary) { @@ -160,9 +144,7 @@ func flashLights(m chat1.MsgSummary) {
}
err := v.FlashLights()
if err != nil {
tracker := uuid.NewString()
k.SendMessageByConvID(m.ConvID, "There was an error flashing your lights. Contact @rudi9719 for more information with code %+v", tracker)
log.LogError("%+v: %+v", tracker, err)
handleError(err,m,"There was an error flashing your lights. Contact @rudi9719 for more information with code %+v")
return
}
@ -176,16 +158,12 @@ func currentTemp(m chat1.MsgSummary) { @@ -176,16 +158,12 @@ func currentTemp(m chat1.MsgSummary) {
}
guiSettings, err := v.GuiSettings()
if err != nil {
tracker := uuid.NewString()
k.SendMessageByConvID(m.ConvID, "There was an error getting your preferences. Contact @rudi9719 for more information with code %+v", tracker)
log.LogError("%+v: %+v", tracker, err)
handleError(err,m,"There was an error getting your preferences. Contact @rudi9719 for more information with code %+v")
return
}
climateState, err := v.ClimateState()
if err != nil {
tracker := uuid.NewString()
k.SendMessageByConvID(m.ConvID, "There was an error getting your Climate State. Contact @rudi9719 for more information with code %+v", tracker)
log.LogError("%+v: %+v", tracker, err)
handleError(err, m, "There was an error getting your Climate State. Contact @rudi9719 for more information with code %+v")
return
}
tempSetting := climateState.DriverTempSetting
@ -210,9 +188,7 @@ func setClimate(m chat1.MsgSummary) { @@ -210,9 +188,7 @@ func setClimate(m chat1.MsgSummary) {
}
guiSettings, err := v.GuiSettings()
if err != nil {
tracker := uuid.NewString()
k.SendMessageByConvID(m.ConvID, "There was an error getting your preferences. Contact @rudi9719 for more information with code %+v", tracker)
log.LogError("%+v: %+v", tracker, err)
handleError(err, m, "There was an error getting your preferences. Contact @rudi9719 for more information with code %+v")
return
}
parts := strings.Split(m.Content.Text.Body, " ")
@ -220,18 +196,14 @@ func setClimate(m chat1.MsgSummary) { @@ -220,18 +196,14 @@ func setClimate(m chat1.MsgSummary) {
if val == "on" {
err := v.StartAirConditioning()
if err != nil {
tracker := uuid.NewString()
k.SendMessageByConvID(m.ConvID, "There was an error starting your Climate. Contact @rudi9719 for more information with code %+v", tracker)
log.LogError("%+v: %+v", tracker, err)
handleError(err, m, "There was an error starting your Climate. Contact @rudi9719 for more information with code %+v")
return
}
}
if val == "off" {
err := v.StopAirConditioning()
if err != nil {
tracker := uuid.NewString()
k.SendMessageByConvID(m.ConvID, "There was an error turning off your Climate. Contact @rudi9719 for more information with code %+v", tracker)
log.LogError("%+v: %+v", tracker, err)
handleError(err, m, "There was an error turning off your Climate. Contact @rudi9719 for more information with code %+v")
return
}
}
@ -241,25 +213,19 @@ func setClimate(m chat1.MsgSummary) { @@ -241,25 +213,19 @@ func setClimate(m chat1.MsgSummary) {
}
err = v.SetTemperature(float64(temp), float64(temp))
if err != nil {
tracker := uuid.NewString()
k.SendMessageByConvID(m.ConvID, "There was an error setting your Climate. Contact @rudi9719 for more information with code %+v", tracker)
log.LogError("%+v: %+v", tracker, err)
handleError(err, m, "There was an error setting your Climate. Contact @rudi9719 for more information with code %+v")
return
}
err = v.StartAirConditioning()
if err != nil {
tracker := uuid.NewString()
k.SendMessageByConvID(m.ConvID, "There was an error starting your Climate. Contact @rudi9719 for more information with code %+v", tracker)
log.LogError("%+v: %+v", tracker, err)
handleError(err, m, "There was an error starting your Climate. Contact @rudi9719 for more information with code %+v")
return
}
}
}
climateState, err := v.ClimateState()
if err != nil {
tracker := uuid.NewString()
k.SendMessageByConvID(m.ConvID, "There was an error getting your Climate State. Contact @rudi9719 for more information with code %+v", tracker)
log.LogError("%+v: %+v", tracker, err)
handleError(err, m, "There was an error getting your Climate State. Contact @rudi9719 for more information with code %+v")
return
}
tempSetting := climateState.DriverTempSetting
@ -283,9 +249,7 @@ func lockVehicle(m chat1.MsgSummary) { @@ -283,9 +249,7 @@ func lockVehicle(m chat1.MsgSummary) {
}
err := v.LockDoors()
if err != nil {
tracker := uuid.NewString()
k.SendMessageByConvID(m.ConvID, "There was an error locking your doors. Contact @rudi9719 for more information with code %+v", tracker)
log.LogError("%+v: %+v", tracker, err)
handleError(err, m, "There was an error locking your doors. Contact @rudi9719 for more information with code %+v")
return
}
}
@ -297,9 +261,7 @@ func unlockVehicle(m chat1.MsgSummary) { @@ -297,9 +261,7 @@ func unlockVehicle(m chat1.MsgSummary) {
}
err := v.UnlockDoors()
if err != nil {
tracker := uuid.NewString()
k.SendMessageByConvID(m.ConvID, "There was an error unlocking your doors. Contact @rudi9719 for more information with code %+v", tracker)
log.LogError("%+v: %+v", tracker, err)
handleError(err, m, "There was an error unlocking your doors. Contact @rudi9719 for more information with code %+v")
return
}
}
@ -311,17 +273,13 @@ func startCharge(m chat1.MsgSummary) { @@ -311,17 +273,13 @@ func startCharge(m chat1.MsgSummary) {
}
state, err := v.ChargeState()
if err != nil {
tracker := uuid.NewString()
k.SendMessageByConvID(m.ConvID, "There was an error getting charge state. Contact @rudi9719 for more information with code %+v", tracker)
log.LogError("%+v: %+v", tracker, err)
handleError(err, m, "There was an error getting charge state. Contact @rudi9719 for more information with code %+v")
return
}
if state.ChargingState != "Disconnected" && state.FastChargerBrand != "Tesla" {
err := v.StartCharging()
if err != nil {
tracker := uuid.NewString()
k.SendMessageByConvID(m.ConvID, "There was an error starting your charge. Contact @rudi9719 for more information with code %+v", tracker)
log.LogError("%+v: %+v", tracker, err)
handleError(err, m, "There was an error starting your charge. Contact @rudi9719 for more information with code %+v")
return
}
} else {
@ -337,17 +295,13 @@ func stopCharge(m chat1.MsgSummary) { @@ -337,17 +295,13 @@ func stopCharge(m chat1.MsgSummary) {
}
state, err := v.ChargeState()
if err != nil {
tracker := uuid.NewString()
k.SendMessageByConvID(m.ConvID, "There was an error getting charge state. Contact @rudi9719 for more information with code %+v", tracker)
log.LogError("%+v: %+v", tracker, err)
handleError(err, m, "There was an error getting charge state. Contact @rudi9719 for more information with code %+v")
return
}
if state.ChargingState != "Disconnected" && state.FastChargerBrand != "Tesla" {
err := v.StopCharging()
if err != nil {
tracker := uuid.NewString()
k.SendMessageByConvID(m.ConvID, "There was an error stopping your charge. Contact @rudi9719 for more information with code %+v", tracker)
log.LogError("%+v: %+v", tracker, err)
handleError(err, m, "There was an error stopping your charge. Contact @rudi9719 for more information with code %+v")
return
}
} else {
@ -367,9 +321,7 @@ func enableStart(m chat1.MsgSummary) { @@ -367,9 +321,7 @@ func enableStart(m chat1.MsgSummary) {
}
_, err := k.KVPut(&m.Channel.Name, "teslabot", "startPass", parts[2])
if err != nil {
tracker := uuid.NewString()
k.SendMessageByConvID(m.ConvID, "There was an error storing your password. Contact @rudi9719 for more information with code %+v", tracker)
log.LogError("%+v: %+v", tracker, err)
handleError(err, m, "There was an error storing your password. Contact @rudi9719 for more information with code %+v")
return
}
@ -378,9 +330,7 @@ func enableStart(m chat1.MsgSummary) { @@ -378,9 +330,7 @@ func enableStart(m chat1.MsgSummary) {
func disableStart(m chat1.MsgSummary) {
_, err := k.KVDelete(&m.Channel.Name, "teslabot", "startPass")
if err != nil {
tracker := uuid.NewString()
k.SendMessageByConvID(m.ConvID, "There was an error deleting your password. Contact @rudi9719 for more information with code %+v", tracker)
log.LogError("%+v: %+v", tracker, err)
handleError(err, m, "There was an error deleting your password. Contact @rudi9719 for more information with code %+v")
return
}
}
@ -397,9 +347,7 @@ func startVehicle(m chat1.MsgSummary) { @@ -397,9 +347,7 @@ func startVehicle(m chat1.MsgSummary) {
}
err := v.Start(test.EntryValue)
if err != nil {
tracker := uuid.NewString()
k.SendMessageByConvID(m.ConvID, "There was an error starting your vehicle. Contact @rudi9719 for more information with code %+v", tracker)
log.LogError("%+v: %+v", tracker, err)
handleError(err, m, "There was an error starting your vehicle. Contact @rudi9719 for more information with code %+v")
return
}
k.SendMessageByConvID(m.ConvID, "Your vehicle has been started!")
@ -423,9 +371,7 @@ func openTrunk(m chat1.MsgSummary) { @@ -423,9 +371,7 @@ func openTrunk(m chat1.MsgSummary) {
case "front":
err := v.OpenTrunk(strings.ToLower(trunk))
if err != nil {
tracker := uuid.NewString()
k.SendMessageByConvID(m.ConvID, "There was an error opening your trunk. Contact @rudi9719 for more information with code %+v", tracker)
log.LogError("%+v: %+v", tracker, err)
handleError(err, m, "There was an error opening your trunk. Contact @rudi9719 for more information with code %+v")
return
}
default:
@ -434,3 +380,8 @@ func openTrunk(m chat1.MsgSummary) { @@ -434,3 +380,8 @@ func openTrunk(m chat1.MsgSummary) {
}
}
func handleError(err error, m chat1.MsgSummary, msg string) {
tracker := uuid.NewString()
k.SendMessageByConvID(m.ConvID, msg, tracker)
log.LogError("%+v: %+v", tracker, err)
}
Loading…
Cancel
Save