some fixes
This commit is contained in:
13
bot.go
13
bot.go
@@ -50,6 +50,7 @@ type BotSettings struct {
|
|||||||
UpdateTypes []string
|
UpdateTypes []string
|
||||||
LoggerBasePath string
|
LoggerBasePath string
|
||||||
UseRequestLogger bool
|
UseRequestLogger bool
|
||||||
|
WriteToFile bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func LoadSettingsFromEnv() *BotSettings {
|
func LoadSettingsFromEnv() *BotSettings {
|
||||||
@@ -60,6 +61,7 @@ func LoadSettingsFromEnv() *BotSettings {
|
|||||||
Prefixes: LoadPrefixesFromEnv(),
|
Prefixes: LoadPrefixesFromEnv(),
|
||||||
UpdateTypes: strings.Split(os.Getenv("UPDATE_TYPES"), ";"),
|
UpdateTypes: strings.Split(os.Getenv("UPDATE_TYPES"), ";"),
|
||||||
UseRequestLogger: os.Getenv("USE_REQ_LOG") == "true",
|
UseRequestLogger: os.Getenv("USE_REQ_LOG") == "true",
|
||||||
|
WriteToFile: os.Getenv("WRITE_TO_FILE") == "true",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -99,10 +101,16 @@ func NewBot(settings *BotSettings) *Bot {
|
|||||||
if settings.Debug {
|
if settings.Debug {
|
||||||
level = DEBUG
|
level = DEBUG
|
||||||
}
|
}
|
||||||
bot.logger = CreateLogger().Level(level).OpenFile(fmt.Sprintf("%s/main.log", strings.TrimRight(settings.LoggerBasePath, "/")))
|
bot.logger = CreateLogger().Level(level)
|
||||||
|
if settings.WriteToFile {
|
||||||
|
bot.logger = bot.logger.OpenFile(fmt.Sprintf("%s/main.log", strings.TrimRight(settings.LoggerBasePath, "/")))
|
||||||
|
}
|
||||||
bot.logger = bot.logger.PrintTraceback(true)
|
bot.logger = bot.logger.PrintTraceback(true)
|
||||||
if settings.UseRequestLogger {
|
if settings.UseRequestLogger {
|
||||||
bot.requestLogger = CreateLogger().Level(level).Prefix("REQUESTS").OpenFile(fmt.Sprintf("%s/requests.log", strings.TrimRight(settings.LoggerBasePath, "/")))
|
bot.requestLogger = CreateLogger().Level(level).Prefix("REQUESTS")
|
||||||
|
if settings.WriteToFile {
|
||||||
|
bot.requestLogger = bot.requestLogger.OpenFile(fmt.Sprintf("%s/requests.log", strings.TrimRight(settings.LoggerBasePath, "/")))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return bot
|
return bot
|
||||||
@@ -375,6 +383,7 @@ func (ctx *MsgContext) Error(err error) {
|
|||||||
ChatID: ctx.Msg.Chat.ID,
|
ChatID: ctx.Msg.Chat.ID,
|
||||||
Text: fmt.Sprintf(ctx.Bot.errorTemplate, err.Error()),
|
Text: fmt.Sprintf(ctx.Bot.errorTemplate, err.Error()),
|
||||||
})
|
})
|
||||||
|
ctx.Bot.logger.Error(err)
|
||||||
|
|
||||||
if sendErr != nil {
|
if sendErr != nil {
|
||||||
ctx.Bot.logger.Error(sendErr)
|
ctx.Bot.logger.Error(sendErr)
|
||||||
|
|||||||
60
utils.go
60
utils.go
@@ -39,9 +39,9 @@ func Map[T, V any](ts []T, fn func(T) V) []V {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func EscapeMarkdown(s string) string {
|
func EscapeMarkdown(s string) string {
|
||||||
s = strings.ReplaceAll(s, "_", "\\_")
|
s = strings.ReplaceAll(s, "_", `\_`)
|
||||||
s = strings.ReplaceAll(s, "*", "\\*")
|
s = strings.ReplaceAll(s, "*", `\*`)
|
||||||
s = strings.ReplaceAll(s, "[", "\\[")
|
s = strings.ReplaceAll(s, "[", `\[`)
|
||||||
return strings.ReplaceAll(s, "`", "\\`")
|
return strings.ReplaceAll(s, "`", "\\`")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -52,3 +52,57 @@ func EscapeMarkdownV2(s string) string {
|
|||||||
}
|
}
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetUnclosedTag(markdown string) string {
|
||||||
|
// order is important!
|
||||||
|
var tags = []string{
|
||||||
|
"```",
|
||||||
|
"`",
|
||||||
|
"*",
|
||||||
|
"_",
|
||||||
|
}
|
||||||
|
var currentTag = ""
|
||||||
|
|
||||||
|
markdownRunes := []rune(markdown)
|
||||||
|
|
||||||
|
var i = 0
|
||||||
|
outer:
|
||||||
|
for i < len(markdownRunes) {
|
||||||
|
// skip escaped characters (only outside tags)
|
||||||
|
if markdownRunes[i] == '\\' && currentTag == "" {
|
||||||
|
i += 2
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if currentTag != "" {
|
||||||
|
if strings.HasPrefix(string(markdownRunes[i:]), currentTag) {
|
||||||
|
// turn a tag off
|
||||||
|
i += len(currentTag)
|
||||||
|
currentTag = ""
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for _, tag := range tags {
|
||||||
|
if strings.HasPrefix(string(markdownRunes[i:]), tag) {
|
||||||
|
// turn a tag on
|
||||||
|
currentTag = tag
|
||||||
|
i += len(currentTag)
|
||||||
|
continue outer
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
|
||||||
|
return currentTag
|
||||||
|
}
|
||||||
|
func IsValid(markdown string) bool {
|
||||||
|
return GetUnclosedTag(markdown) == ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func FixMarkdown(markdown string) string {
|
||||||
|
tag := GetUnclosedTag(markdown)
|
||||||
|
if tag == "" {
|
||||||
|
return markdown
|
||||||
|
}
|
||||||
|
return markdown + tag
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,10 +3,10 @@ package laniakea
|
|||||||
import "os"
|
import "os"
|
||||||
|
|
||||||
const (
|
const (
|
||||||
VersionString = "0.1.4"
|
VersionString = "0.2.0"
|
||||||
VersionMajor = 0
|
VersionMajor = 0
|
||||||
VersionMinor = 1
|
VersionMinor = 2
|
||||||
VersionPatch = 4
|
VersionPatch = 0
|
||||||
)
|
)
|
||||||
|
|
||||||
var GoVersion = os.Getenv("GoV")
|
var GoVersion = os.Getenv("GoV")
|
||||||
|
|||||||
Reference in New Issue
Block a user