URL scheme whitelist and Inline Queries. #21

Merged
skobkin merged 3 commits from fix_chat_type_middleware_nil_pointer into main 2024-03-12 22:20:06 +00:00
2 changed files with 58 additions and 33 deletions
Showing only changes of commit ca005a9370 - Show all commits

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 { func (b *Bot) escapeMarkdownV1Symbols(input string) string {
return b.markdownV1Replacer.Replace(input) 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
}