Extracting request context creation to separate file.

This commit is contained in:
Alexey Skobkin 2024-03-13 00:32:52 +03:00
parent d890faf461
commit ca005a9370
No known key found for this signature in database
GPG key ID: 5D5CEF6F221278E7
2 changed files with 58 additions and 33 deletions

View file

@ -257,39 +257,6 @@ func (b *Bot) statsHandler(bot *telego.Bot, update telego.Update) {
}
}
func (b *Bot) createLlmRequestContext(update telego.Update) llm.RequestContext {
message := update.Message
rc := llm.RequestContext{}
if message == nil {
slog.Debug("request context creation problem: no message provided. returning empty context.", "request-context", rc)
return rc
}
user := message.From
if user != nil {
rc.User = llm.UserContext{
Username: user.Username,
FirstName: user.FirstName,
LastName: user.LastName,
IsPremium: user.IsPremium,
}
}
chat := message.Chat
rc.Chat = llm.ChatContext{
Title: chat.Title,
Description: chat.Description,
Type: chat.Type,
}
slog.Debug("request context created", "request-context", rc)
return rc
}
func (b *Bot) escapeMarkdownV1Symbols(input string) string {
return b.markdownV1Replacer.Replace(input)
}

58
bot/request_context.go Normal file
View file

@ -0,0 +1,58 @@
package bot
import (
"github.com/mymmrac/telego"
"log/slog"
"telegram-ollama-reply-bot/llm"
)
func (b *Bot) createLlmRequestContext(update telego.Update) llm.RequestContext {
message := update.Message
iq := update.InlineQuery
rc := llm.RequestContext{
Empty: true,
Inline: false,
}
switch {
case message == nil && iq == nil:
slog.Debug("request context creation problem: no message provided. returning empty context.", "request-context", rc)
return rc
case iq != nil:
rc.Inline = true
}
rc.Empty = false
var user *telego.User
if rc.Inline {
user = &iq.From
} else {
user = message.From
}
if user != nil {
rc.User = llm.UserContext{
Username: user.Username,
FirstName: user.FirstName,
LastName: user.LastName,
IsPremium: user.IsPremium,
}
}
if !rc.Inline {
chat := message.Chat
rc.Chat = llm.ChatContext{
Title: chat.Title,
Description: chat.Description,
Type: chat.Type,
}
}
slog.Debug("request context created", "request-context", rc)
return rc
}