From 452b7336c29de6e35951deff962fbd2628924d52 Mon Sep 17 00:00:00 2001 From: Matthieu 'JP' DERASSE Date: Mon, 17 Jul 2023 13:54:58 +0000 Subject: [PATCH] fix(specialChar): Fix telegram special char --- alert/alert.go | 2 +- bot/bot.go | 26 +++++++++++++++++--------- bot/config.go | 26 ++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 10 deletions(-) diff --git a/alert/alert.go b/alert/alert.go index 01c19a0..aebeb73 100644 --- a/alert/alert.go +++ b/alert/alert.go @@ -33,7 +33,7 @@ func Init(alertChan chan api.Availability) { } // define an interval and the ticker for this interval - interval := time.Duration(2) * time.Second + interval := time.Duration(3) * time.Second // create a new Ticker tk := time.NewTicker(interval) diff --git a/bot/bot.go b/bot/bot.go index f33a789..e21fedb 100644 --- a/bot/bot.go +++ b/bot/bot.go @@ -70,8 +70,8 @@ func handleAlert(bot *tele.Bot, config *botConfig, alertChan chan api.Availabili fmt.Sprintf( "ALERT\\!\\! Found a %s%s in *%s* color is: *%d*€ [View](%s)", demoStr, - availability.TrimName, - strings.Join(availability.Paint, " and "), + cleanString(availability.TrimName), + cleanString(strings.Join(availability.Paint, " and ")), availability.Price, availability.GetOrderLink(), ), @@ -180,8 +180,8 @@ func price(c tele.Context) error { 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 "), + cleanString(propulsion.TrimName), + cleanString(strings.Join(propulsion.Paint, " and ")), propulsion.Price, propulsion.GetOrderLink(), ) @@ -192,8 +192,8 @@ func price(c tele.Context) error { } 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 "), + cleanString(longRange.TrimName), + cleanString(strings.Join(longRange.Paint, " and ")), longRange.Price, longRange.GetOrderLink(), ) @@ -226,9 +226,9 @@ func list(c tele.Context) error { "%s%s%s, color *%s* available in *%s* for *%d*€ [View](%s)\n", availabilitiesStr, demoStr, - availability.TrimName, - strings.Join(availability.Paint, " and "), - strings.ReplaceAll(availability.City, "-", "\\-"), + cleanString(availability.TrimName), + cleanString(strings.Join(availability.Paint, " and ")), + cleanString(availability.City), availability.Price, availability.GetOrderLink(), ) @@ -236,3 +236,11 @@ func list(c tele.Context) error { return c.Reply(availabilitiesStr, &homeMenu) } + +func cleanString(in string) string { + for _, str := range specialChars { + in = strings.ReplaceAll(in, str, fmt.Sprintf("\\%s", str)) + } + + return in +} diff --git a/bot/config.go b/bot/config.go index 1373724..f52beb6 100644 --- a/bot/config.go +++ b/bot/config.go @@ -9,6 +9,32 @@ import ( "git.dev.m-and-m.ovh/mderasse/tesla/api" ) +var specialChars = []string{ + "\\", + "_", + "*", + "[", + "]", + "(", + ")", + "~", + "`", + ">", + "<", + "&", + "#", + "+", + "-", + "=", + "|", + "{", + "}", + ".", + "!", + "(", + ")", +} + var carFilter api.AvailabilityQueryParams = api.AvailabilityQueryParams{ Model: "my", Condition: "new",