2024-03-10 01:51:01 +00:00
|
|
|
package bot
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"github.com/mymmrac/telego"
|
|
|
|
th "github.com/mymmrac/telego/telegohandler"
|
|
|
|
tu "github.com/mymmrac/telego/telegoutil"
|
|
|
|
"log/slog"
|
|
|
|
"strings"
|
|
|
|
"telegram-ollama-reply-bot/extractor"
|
|
|
|
"telegram-ollama-reply-bot/llm"
|
2024-03-11 20:15:27 +00:00
|
|
|
"telegram-ollama-reply-bot/stats"
|
2024-03-10 01:51:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrGetMe = errors.New("cannot retrieve api user")
|
|
|
|
ErrUpdatesChannel = errors.New("cannot get updates channel")
|
|
|
|
ErrHandlerInit = errors.New("cannot initialize handler")
|
|
|
|
)
|
|
|
|
|
2024-10-27 21:35:35 +00:00
|
|
|
type BotInfo struct {
|
|
|
|
Id int64
|
|
|
|
Username string
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
2024-03-10 01:51:01 +00:00
|
|
|
type Bot struct {
|
|
|
|
api *telego.Bot
|
|
|
|
llm *llm.LlmConnector
|
|
|
|
extractor *extractor.Extractor
|
2024-03-11 20:15:27 +00:00
|
|
|
stats *stats.Stats
|
2024-08-16 00:47:07 +00:00
|
|
|
models ModelSelection
|
2024-10-27 21:35:35 +00:00
|
|
|
history map[int64]*MessageRingBuffer
|
|
|
|
profile BotInfo
|
2024-03-12 20:01:05 +00:00
|
|
|
|
|
|
|
markdownV1Replacer *strings.Replacer
|
2024-03-10 01:51:01 +00:00
|
|
|
}
|
|
|
|
|
2024-08-16 00:47:07 +00:00
|
|
|
func NewBot(
|
|
|
|
api *telego.Bot,
|
|
|
|
llm *llm.LlmConnector,
|
|
|
|
extractor *extractor.Extractor,
|
|
|
|
models ModelSelection,
|
|
|
|
) *Bot {
|
2024-03-10 01:51:01 +00:00
|
|
|
return &Bot{
|
|
|
|
api: api,
|
|
|
|
llm: llm,
|
|
|
|
extractor: extractor,
|
2024-03-11 20:15:27 +00:00
|
|
|
stats: stats.NewStats(),
|
2024-08-16 00:47:07 +00:00
|
|
|
models: models,
|
2024-10-27 21:35:35 +00:00
|
|
|
history: make(map[int64]*MessageRingBuffer),
|
|
|
|
profile: BotInfo{0, "", ""},
|
2024-03-12 20:01:05 +00:00
|
|
|
|
|
|
|
markdownV1Replacer: strings.NewReplacer(
|
|
|
|
// https://core.telegram.org/bots/api#markdown-style
|
|
|
|
"_", "\\_",
|
|
|
|
//"*", "\\*",
|
|
|
|
//"`", "\\`",
|
|
|
|
//"[", "\\[",
|
|
|
|
),
|
2024-03-10 01:51:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bot) Run() error {
|
|
|
|
botUser, err := b.api.GetMe()
|
|
|
|
if err != nil {
|
2024-03-12 19:06:40 +00:00
|
|
|
slog.Error("Cannot retrieve api user", "error", err)
|
2024-03-10 01:51:01 +00:00
|
|
|
|
|
|
|
return ErrGetMe
|
|
|
|
}
|
|
|
|
|
2024-03-12 19:06:40 +00:00
|
|
|
slog.Info("Running api as", "id", botUser.ID, "username", botUser.Username, "name", botUser.FirstName, "is_bot", botUser.IsBot)
|
2024-03-10 01:51:01 +00:00
|
|
|
|
2024-10-27 21:35:35 +00:00
|
|
|
b.profile = BotInfo{
|
|
|
|
Id: botUser.ID,
|
|
|
|
Username: botUser.Username,
|
|
|
|
Name: botUser.FirstName,
|
|
|
|
}
|
|
|
|
|
2024-03-10 01:51:01 +00:00
|
|
|
updates, err := b.api.UpdatesViaLongPolling(nil)
|
|
|
|
if err != nil {
|
2024-03-12 19:06:40 +00:00
|
|
|
slog.Error("Cannot get update channel", "error", err)
|
2024-03-10 01:51:01 +00:00
|
|
|
|
|
|
|
return ErrUpdatesChannel
|
|
|
|
}
|
|
|
|
|
|
|
|
bh, err := th.NewBotHandler(b.api, updates)
|
|
|
|
if err != nil {
|
2024-03-12 19:06:40 +00:00
|
|
|
slog.Error("Cannot initialize bot handler", "error", err)
|
2024-03-10 01:51:01 +00:00
|
|
|
|
|
|
|
return ErrHandlerInit
|
|
|
|
}
|
|
|
|
|
|
|
|
defer bh.Stop()
|
|
|
|
defer b.api.StopLongPolling()
|
|
|
|
|
2024-03-11 20:15:27 +00:00
|
|
|
// Middlewares
|
2024-10-27 21:35:35 +00:00
|
|
|
bh.Use(b.chatHistory)
|
2024-03-11 20:15:27 +00:00
|
|
|
bh.Use(b.chatTypeStatsCounter)
|
|
|
|
|
2024-03-12 22:18:01 +00:00
|
|
|
// Command handlers
|
2024-10-27 21:35:35 +00:00
|
|
|
bh.Handle(b.textMessageHandler, th.AnyMessageWithText())
|
2024-03-10 01:51:01 +00:00
|
|
|
bh.Handle(b.startHandler, th.CommandEqual("start"))
|
2024-10-27 21:35:35 +00:00
|
|
|
bh.Handle(b.summarizeHandler, th.Or(th.CommandEqual("summarize"), th.CommandEqual("s")))
|
2024-03-11 20:19:05 +00:00
|
|
|
bh.Handle(b.statsHandler, th.CommandEqual("stats"))
|
2024-03-10 01:51:01 +00:00
|
|
|
bh.Handle(b.helpHandler, th.CommandEqual("help"))
|
|
|
|
|
|
|
|
bh.Start()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-10-27 21:35:35 +00:00
|
|
|
func (b *Bot) textMessageHandler(bot *telego.Bot, update telego.Update) {
|
|
|
|
slog.Debug("/any-message")
|
|
|
|
|
|
|
|
message := update.Message
|
|
|
|
|
|
|
|
switch {
|
|
|
|
// Mentions
|
|
|
|
case b.isMentionOfMe(update):
|
|
|
|
slog.Info("/any-message", "type", "mention")
|
|
|
|
b.processMention(message)
|
|
|
|
// Replies
|
|
|
|
case b.isReplyToMe(update):
|
|
|
|
slog.Info("/any-message", "type", "reply")
|
|
|
|
b.processMention(message)
|
|
|
|
// Private chat
|
|
|
|
case b.isPrivateWithMe(update):
|
|
|
|
slog.Info("/any-message", "type", "private")
|
|
|
|
b.processMention(message)
|
|
|
|
default:
|
|
|
|
slog.Debug("/any-message", "info", "Message is not mention, reply or private chat. Skipping.")
|
2024-03-12 22:18:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-27 21:35:35 +00:00
|
|
|
func (b *Bot) processMention(message *telego.Message) {
|
|
|
|
b.stats.Mention()
|
2024-03-11 20:15:27 +00:00
|
|
|
|
2024-10-27 21:35:35 +00:00
|
|
|
slog.Info("/mention", "chat", message.Chat.ID)
|
2024-03-12 03:12:25 +00:00
|
|
|
|
2024-10-27 21:35:35 +00:00
|
|
|
chatID := tu.ID(message.Chat.ID)
|
2024-03-10 01:51:01 +00:00
|
|
|
|
|
|
|
b.sendTyping(chatID)
|
|
|
|
|
2024-10-27 21:35:35 +00:00
|
|
|
requestContext := b.createLlmRequestContextFromMessage(message)
|
2024-03-12 03:12:25 +00:00
|
|
|
|
2024-10-27 21:35:35 +00:00
|
|
|
llmReply, err := b.llm.HandleChatMessage(message.Text, b.models.TextRequestModel, requestContext)
|
2024-03-10 01:51:01 +00:00
|
|
|
if err != nil {
|
|
|
|
slog.Error("Cannot get reply from LLM connector")
|
|
|
|
|
2024-10-27 21:35:35 +00:00
|
|
|
_, _ = b.api.SendMessage(b.reply(message, tu.Message(
|
2024-03-10 01:51:01 +00:00
|
|
|
chatID,
|
|
|
|
"LLM request error. Try again later.",
|
|
|
|
)))
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-03-12 22:18:01 +00:00
|
|
|
slog.Debug("Got completion. Going to send.", "llm-completion", llmReply)
|
2024-03-10 01:51:01 +00:00
|
|
|
|
2024-10-27 21:35:35 +00:00
|
|
|
reply := tu.Message(
|
2024-03-10 01:51:01 +00:00
|
|
|
chatID,
|
2024-03-12 20:01:05 +00:00
|
|
|
b.escapeMarkdownV1Symbols(llmReply),
|
2024-03-10 01:51:01 +00:00
|
|
|
).WithParseMode("Markdown")
|
|
|
|
|
2024-10-27 21:35:35 +00:00
|
|
|
_, err = b.api.SendMessage(b.reply(message, reply))
|
2024-03-10 01:51:01 +00:00
|
|
|
if err != nil {
|
2024-03-12 19:06:40 +00:00
|
|
|
slog.Error("Can't send reply message", "error", err)
|
2024-03-12 02:14:23 +00:00
|
|
|
|
2024-10-27 21:35:35 +00:00
|
|
|
b.trySendReplyError(message)
|
|
|
|
|
|
|
|
return
|
2024-03-10 01:51:01 +00:00
|
|
|
}
|
2024-10-27 21:35:35 +00:00
|
|
|
|
|
|
|
b.saveBotReplyToHistory(message, llmReply)
|
2024-03-10 01:51:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bot) summarizeHandler(bot *telego.Bot, update telego.Update) {
|
2024-03-12 19:06:40 +00:00
|
|
|
slog.Info("/summarize", "message-text", update.Message.Text)
|
2024-03-10 01:51:01 +00:00
|
|
|
|
2024-03-11 20:15:27 +00:00
|
|
|
b.stats.SummarizeRequest()
|
|
|
|
|
2024-03-10 01:51:01 +00:00
|
|
|
chatID := tu.ID(update.Message.Chat.ID)
|
|
|
|
|
|
|
|
b.sendTyping(chatID)
|
|
|
|
|
2024-03-12 22:18:01 +00:00
|
|
|
args := strings.SplitN(update.Message.Text, " ", 2)
|
2024-03-10 01:51:01 +00:00
|
|
|
|
|
|
|
if len(args) < 2 {
|
|
|
|
_, _ = bot.SendMessage(tu.Message(
|
|
|
|
tu.ID(update.Message.Chat.ID),
|
|
|
|
"Usage: /summarize <link>\r\n\r\n"+
|
|
|
|
"Example:\r\n"+
|
|
|
|
"/summarize https://kernel.org/get-notifications-for-your-patches.html",
|
|
|
|
))
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-03-12 21:32:18 +00:00
|
|
|
if !isValidAndAllowedUrl(args[1]) {
|
|
|
|
slog.Error("Provided text is not a valid URL", "text", args[1])
|
2024-03-10 01:51:01 +00:00
|
|
|
|
|
|
|
_, _ = b.api.SendMessage(b.reply(update.Message, tu.Message(
|
|
|
|
chatID,
|
|
|
|
"URL is not valid.",
|
|
|
|
)))
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
article, err := b.extractor.GetArticleFromUrl(args[1])
|
|
|
|
if err != nil {
|
2024-03-12 19:06:40 +00:00
|
|
|
slog.Error("Cannot retrieve an article using extractor", "error", err)
|
2024-03-10 01:51:01 +00:00
|
|
|
}
|
|
|
|
|
2024-08-16 00:47:07 +00:00
|
|
|
llmReply, err := b.llm.Summarize(article.Text, b.models.SummarizeModel)
|
2024-03-10 01:51:01 +00:00
|
|
|
if err != nil {
|
|
|
|
slog.Error("Cannot get reply from LLM connector")
|
|
|
|
|
|
|
|
_, _ = b.api.SendMessage(b.reply(update.Message, tu.Message(
|
|
|
|
chatID,
|
|
|
|
"LLM request error. Try again later.",
|
|
|
|
)))
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-03-12 22:18:01 +00:00
|
|
|
slog.Debug("Got completion. Going to send.", "llm-completion", llmReply)
|
2024-03-10 01:51:01 +00:00
|
|
|
|
|
|
|
message := tu.Message(
|
|
|
|
chatID,
|
2024-03-12 20:01:05 +00:00
|
|
|
b.escapeMarkdownV1Symbols(llmReply),
|
2024-03-10 01:51:01 +00:00
|
|
|
).WithParseMode("Markdown")
|
|
|
|
|
|
|
|
_, err = bot.SendMessage(b.reply(update.Message, message))
|
|
|
|
|
|
|
|
if err != nil {
|
2024-03-12 19:06:40 +00:00
|
|
|
slog.Error("Can't send reply message", "error", err)
|
2024-03-12 02:14:23 +00:00
|
|
|
|
|
|
|
b.trySendReplyError(update.Message)
|
2024-03-10 01:51:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bot) helpHandler(bot *telego.Bot, update telego.Update) {
|
|
|
|
slog.Info("/help")
|
|
|
|
|
|
|
|
chatID := tu.ID(update.Message.Chat.ID)
|
|
|
|
|
|
|
|
b.sendTyping(chatID)
|
|
|
|
|
|
|
|
_, err := bot.SendMessage(b.reply(update.Message, tu.Messagef(
|
|
|
|
chatID,
|
|
|
|
"Instructions:\r\n"+
|
|
|
|
"/hey <text> - Ask something from LLM\r\n"+
|
|
|
|
"/summarize <link> - Summarize text from the provided link\r\n"+
|
2024-10-27 21:35:35 +00:00
|
|
|
"/s <link> - Shorter version\r\n"+
|
|
|
|
"/help - Show this help\r\n\r\n"+
|
|
|
|
"Mention bot or reply to it's message to communicate with it",
|
2024-03-10 01:51:01 +00:00
|
|
|
)))
|
|
|
|
if err != nil {
|
2024-03-12 19:06:40 +00:00
|
|
|
slog.Error("Cannot send a message", "error", err)
|
2024-03-12 02:14:23 +00:00
|
|
|
|
|
|
|
b.trySendReplyError(update.Message)
|
2024-03-10 01:51:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bot) startHandler(bot *telego.Bot, update telego.Update) {
|
|
|
|
slog.Info("/start")
|
|
|
|
|
|
|
|
chatID := tu.ID(update.Message.Chat.ID)
|
|
|
|
|
|
|
|
b.sendTyping(chatID)
|
|
|
|
|
|
|
|
_, err := bot.SendMessage(b.reply(update.Message, tu.Message(
|
|
|
|
chatID,
|
|
|
|
"Hey!\r\n"+
|
|
|
|
"Check out /help to learn how to use this bot.",
|
|
|
|
)))
|
|
|
|
if err != nil {
|
2024-03-12 19:06:40 +00:00
|
|
|
slog.Error("Cannot send a message", "error", err)
|
2024-03-12 02:14:23 +00:00
|
|
|
|
|
|
|
b.trySendReplyError(update.Message)
|
2024-03-10 01:51:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-11 20:15:27 +00:00
|
|
|
func (b *Bot) statsHandler(bot *telego.Bot, update telego.Update) {
|
|
|
|
slog.Info("/stats")
|
|
|
|
|
|
|
|
chatID := tu.ID(update.Message.Chat.ID)
|
|
|
|
|
|
|
|
b.sendTyping(chatID)
|
|
|
|
|
|
|
|
_, err := bot.SendMessage(b.reply(update.Message, tu.Message(
|
|
|
|
chatID,
|
|
|
|
"Current bot stats:\r\n"+
|
|
|
|
"```json\r\n"+
|
|
|
|
b.stats.String()+"\r\n"+
|
|
|
|
"```",
|
|
|
|
)).WithParseMode("Markdown"))
|
|
|
|
if err != nil {
|
2024-03-12 19:06:40 +00:00
|
|
|
slog.Error("Cannot send a message", "error", err)
|
2024-03-12 02:14:23 +00:00
|
|
|
|
|
|
|
b.trySendReplyError(update.Message)
|
2024-03-11 20:15:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-12 20:01:05 +00:00
|
|
|
func (b *Bot) escapeMarkdownV1Symbols(input string) string {
|
|
|
|
return b.markdownV1Replacer.Replace(input)
|
|
|
|
}
|