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")
|
|
|
|
)
|
|
|
|
|
|
|
|
type Bot struct {
|
|
|
|
api *telego.Bot
|
|
|
|
llm *llm.LlmConnector
|
|
|
|
extractor *extractor.Extractor
|
2024-03-11 20:15:27 +00:00
|
|
|
stats *stats.Stats
|
2024-03-12 20:01:05 +00:00
|
|
|
|
|
|
|
markdownV1Replacer *strings.Replacer
|
2024-03-10 01:51:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewBot(api *telego.Bot, llm *llm.LlmConnector, extractor *extractor.Extractor) *Bot {
|
|
|
|
return &Bot{
|
|
|
|
api: api,
|
|
|
|
llm: llm,
|
|
|
|
extractor: extractor,
|
2024-03-11 20:15:27 +00:00
|
|
|
stats: stats.NewStats(),
|
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
|
|
|
|
|
|
|
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
|
|
|
|
bh.Use(b.chatTypeStatsCounter)
|
|
|
|
|
2024-03-12 22:18:01 +00:00
|
|
|
// Command handlers
|
2024-03-10 01:51:01 +00:00
|
|
|
bh.Handle(b.startHandler, th.CommandEqual("start"))
|
|
|
|
bh.Handle(b.heyHandler, th.CommandEqual("hey"))
|
|
|
|
bh.Handle(b.summarizeHandler, th.CommandEqual("summarize"))
|
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"))
|
|
|
|
|
2024-03-12 22:18:01 +00:00
|
|
|
// Inline query handlers
|
|
|
|
bh.Handle(b.inlineHandler, th.AnyInlineQuery())
|
|
|
|
|
2024-03-10 01:51:01 +00:00
|
|
|
bh.Start()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-03-12 22:18:01 +00:00
|
|
|
func (b *Bot) inlineHandler(bot *telego.Bot, update telego.Update) {
|
|
|
|
iq := update.InlineQuery
|
|
|
|
slog.Info("inline query received", "query", iq.Query)
|
|
|
|
|
|
|
|
slog.Debug("query", "query", iq)
|
|
|
|
|
|
|
|
if len(iq.Query) < 3 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
b.stats.InlineQuery()
|
|
|
|
|
|
|
|
queryParts := strings.SplitN(iq.Query, " ", 2)
|
|
|
|
|
|
|
|
if len(queryParts) < 1 {
|
|
|
|
slog.Debug("Empty query. Skipping.")
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var response *telego.AnswerInlineQueryParams
|
|
|
|
|
|
|
|
switch isValidAndAllowedUrl(queryParts[0]) {
|
|
|
|
case true:
|
|
|
|
slog.Info("Inline /summarize request", "url", queryParts[0])
|
|
|
|
|
|
|
|
b.stats.SummarizeRequest()
|
|
|
|
|
|
|
|
article, err := b.extractor.GetArticleFromUrl(queryParts[0])
|
|
|
|
if err != nil {
|
|
|
|
slog.Error("Cannot retrieve an article using extractor", "error", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
llmReply, err := b.llm.Summarize(article.Text, llm.ModelMistralUncensored)
|
|
|
|
if err != nil {
|
|
|
|
slog.Error("Cannot get reply from LLM connector")
|
|
|
|
|
|
|
|
b.trySendInlineQueryError(iq, "LLM request error. Try again later.")
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
slog.Debug("Got completion. Going to send.", "llm-completion", llmReply)
|
|
|
|
|
|
|
|
response = tu.InlineQuery(
|
|
|
|
iq.ID,
|
|
|
|
tu.ResultArticle(
|
|
|
|
"reply_"+iq.ID,
|
|
|
|
"Summary for "+queryParts[0],
|
|
|
|
tu.TextMessage(b.escapeMarkdownV1Symbols(llmReply)).WithParseMode("Markdown"),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
case false:
|
|
|
|
b.stats.HeyRequest()
|
|
|
|
|
|
|
|
slog.Info("Inline /hey request", "text", iq.Query)
|
|
|
|
|
|
|
|
requestContext := createLlmRequestContextFromUpdate(update)
|
|
|
|
|
|
|
|
llmReply, err := b.llm.HandleSingleRequest(iq.Query, llm.ModelMistralUncensored, requestContext)
|
|
|
|
if err != nil {
|
|
|
|
slog.Error("Cannot get reply from LLM connector")
|
|
|
|
|
|
|
|
b.trySendInlineQueryError(iq, "LLM request error. Try again later.")
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
slog.Debug("Got completion. Going to send.", "llm-completion", llmReply)
|
|
|
|
|
|
|
|
response = tu.InlineQuery(
|
|
|
|
iq.ID,
|
|
|
|
tu.ResultArticle(
|
|
|
|
"reply_"+iq.ID,
|
|
|
|
"LLM reply to\""+iq.Query+"\"",
|
|
|
|
tu.TextMessage(b.escapeMarkdownV1Symbols(llmReply)).WithParseMode("Markdown"),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
err := bot.AnswerInlineQuery(response)
|
|
|
|
if err != nil {
|
|
|
|
slog.Error("Can't answer to inline query", "error", err)
|
|
|
|
|
|
|
|
b.trySendInlineQueryError(iq, "Couldn't send intended reply, sorry")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-10 01:51:01 +00:00
|
|
|
func (b *Bot) heyHandler(bot *telego.Bot, update telego.Update) {
|
2024-03-12 19:06:40 +00:00
|
|
|
slog.Info("/hey", "message-text", update.Message.Text)
|
2024-03-10 01:51:01 +00:00
|
|
|
|
2024-03-11 20:15:27 +00:00
|
|
|
b.stats.HeyRequest()
|
|
|
|
|
2024-03-12 03:12:25 +00:00
|
|
|
parts := strings.SplitN(update.Message.Text, " ", 2)
|
|
|
|
userMessage := "Hey!"
|
|
|
|
if len(parts) == 2 {
|
|
|
|
userMessage = parts[1]
|
|
|
|
}
|
|
|
|
|
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
|
|
|
requestContext := createLlmRequestContextFromUpdate(update)
|
2024-03-12 03:12:25 +00:00
|
|
|
|
|
|
|
llmReply, err := b.llm.HandleSingleRequest(userMessage, llm.ModelMistralUncensored, requestContext)
|
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) 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
|
|
|
}
|
|
|
|
|
|
|
|
llmReply, err := b.llm.Summarize(article.Text, llm.ModelMistralUncensored)
|
|
|
|
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"+
|
|
|
|
"/help - Show this help",
|
|
|
|
)))
|
|
|
|
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)
|
|
|
|
}
|