Fixing a bunch of bugs and making some improvements #19

Merged
skobkin merged 6 commits from fix_chat_type_middleware_nil_pointer into main 2024-03-12 20:08:50 +00:00
Showing only changes of commit d3c0bc28f1 - Show all commits

View file

@ -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,