Fix #18. Escaping underscore symbols to avoid Telegram's Bot API "Markdown" (v1) parser errors.
This commit is contained in:
parent
38fcd1a5a9
commit
d3c0bc28f1
18
bot/bot.go
18
bot/bot.go
|
@ -24,6 +24,8 @@ type Bot struct {
|
||||||
llm *llm.LlmConnector
|
llm *llm.LlmConnector
|
||||||
extractor *extractor.Extractor
|
extractor *extractor.Extractor
|
||||||
stats *stats.Stats
|
stats *stats.Stats
|
||||||
|
|
||||||
|
markdownV1Replacer *strings.Replacer
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewBot(api *telego.Bot, llm *llm.LlmConnector, extractor *extractor.Extractor) *Bot {
|
func NewBot(api *telego.Bot, llm *llm.LlmConnector, extractor *extractor.Extractor) *Bot {
|
||||||
|
@ -32,6 +34,14 @@ func NewBot(api *telego.Bot, llm *llm.LlmConnector, extractor *extractor.Extract
|
||||||
llm: llm,
|
llm: llm,
|
||||||
extractor: extractor,
|
extractor: extractor,
|
||||||
stats: stats.NewStats(),
|
stats: stats.NewStats(),
|
||||||
|
|
||||||
|
markdownV1Replacer: strings.NewReplacer(
|
||||||
|
// https://core.telegram.org/bots/api#markdown-style
|
||||||
|
"_", "\\_",
|
||||||
|
//"*", "\\*",
|
||||||
|
//"`", "\\`",
|
||||||
|
//"[", "\\[",
|
||||||
|
),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -110,7 +120,7 @@ func (b *Bot) heyHandler(bot *telego.Bot, update telego.Update) {
|
||||||
|
|
||||||
message := tu.Message(
|
message := tu.Message(
|
||||||
chatID,
|
chatID,
|
||||||
llmReply,
|
b.escapeMarkdownV1Symbols(llmReply),
|
||||||
).WithParseMode("Markdown")
|
).WithParseMode("Markdown")
|
||||||
|
|
||||||
_, err = bot.SendMessage(b.reply(update.Message, message))
|
_, err = bot.SendMessage(b.reply(update.Message, message))
|
||||||
|
@ -176,7 +186,7 @@ func (b *Bot) summarizeHandler(bot *telego.Bot, update telego.Update) {
|
||||||
|
|
||||||
message := tu.Message(
|
message := tu.Message(
|
||||||
chatID,
|
chatID,
|
||||||
llmReply,
|
b.escapeMarkdownV1Symbols(llmReply),
|
||||||
).WithParseMode("Markdown")
|
).WithParseMode("Markdown")
|
||||||
|
|
||||||
_, err = bot.SendMessage(b.reply(update.Message, message))
|
_, err = bot.SendMessage(b.reply(update.Message, message))
|
||||||
|
@ -282,6 +292,10 @@ func (b *Bot) createLlmRequestContext(update telego.Update) llm.RequestContext {
|
||||||
return rc
|
return rc
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *Bot) escapeMarkdownV1Symbols(input string) string {
|
||||||
|
return b.markdownV1Replacer.Replace(input)
|
||||||
|
}
|
||||||
|
|
||||||
func (b *Bot) reply(originalMessage *telego.Message, newMessage *telego.SendMessageParams) *telego.SendMessageParams {
|
func (b *Bot) reply(originalMessage *telego.Message, newMessage *telego.SendMessageParams) *telego.SendMessageParams {
|
||||||
return newMessage.WithReplyParameters(&telego.ReplyParameters{
|
return newMessage.WithReplyParameters(&telego.ReplyParameters{
|
||||||
MessageID: originalMessage.MessageID,
|
MessageID: originalMessage.MessageID,
|
||||||
|
|
Loading…
Reference in a new issue