]> git.giacomolli.uk - onechat/blobdiff - telegram.go
Show styled replies only on terminal output
[onechat] / telegram.go
index 2a9345a3a4b6823c0a17039c207dcf437f12d640..f2c687ad967a9956d482d983fadee252ae924ed8 100644 (file)
@@ -7,7 +7,7 @@ import (
        "errors"
        "fmt"
        "io"
        "errors"
        "fmt"
        "io"
-       "log"
+       "log/slog"
        "net/http"
        "os"
        "path/filepath"
        "net/http"
        "os"
        "path/filepath"
@@ -97,7 +97,7 @@ func loadTelegramConfig(dir string) (TelegramConfig, bool, error) {
 // newTelegramBot returns the bot of owner on api. The offset file is at
 // offsetPath, and logger gets the failures. No log line holds the text of a
 // message.
 // newTelegramBot returns the bot of owner on api. The offset file is at
 // offsetPath, and logger gets the failures. No log line holds the text of a
 // message.
-func newTelegramBot(api *TelegramAPI, owner int64, offsetPath string, logger *log.Logger) *TelegramBot {
+func newTelegramBot(api *TelegramAPI, owner int64, offsetPath string, logger *slog.Logger) *TelegramBot {
        return &TelegramBot{
                api:        api,
                owner:      owner,
        return &TelegramBot{
                api:        api,
                owner:      owner,
@@ -154,7 +154,7 @@ type TelegramBot struct {
        api        *TelegramAPI
        owner      int64
        offsetPath string
        api        *TelegramAPI
        owner      int64
        offsetPath string
-       logger     *log.Logger
+       logger     *slog.Logger
        minWait    time.Duration
        maxWait    time.Duration
 
        minWait    time.Duration
        maxWait    time.Duration
 
@@ -379,7 +379,7 @@ func (b *TelegramBot) pause(ctx context.Context, outage *TelegramOutage, err err
 func (b *TelegramBot) handle(ctx context.Context, l *TurnLoop, whatsapp *WhatsApp, update TelegramUpdate) error {
        message := update.Message
        if message == nil || message.Chat.Type != "private" || message.Chat.ID != b.owner || message.From == nil || message.From.ID != b.owner {
 func (b *TelegramBot) handle(ctx context.Context, l *TurnLoop, whatsapp *WhatsApp, update TelegramUpdate) error {
        message := update.Message
        if message == nil || message.Chat.Type != "private" || message.Chat.ID != b.owner || message.From == nil || message.From.ID != b.owner {
-               b.logger.Print("message ignored: not from the owner")
+               b.logger.Info("message ignored: not from the owner")
 
                return nil
        }
 
                return nil
        }
@@ -440,7 +440,7 @@ func (b *TelegramBot) deliver(ctx context.Context, part TelegramPart) {
                var answer *TelegramError
 
                if len(part.entities) > 0 && errors.As(err, &answer) && answer.Status == http.StatusBadRequest {
                var answer *TelegramError
 
                if len(part.entities) > 0 && errors.As(err, &answer) && answer.Status == http.StatusBadRequest {
-                       b.logger.Printf("reply sent as plain text: %v", err)
+                       b.logger.Warn("reply sent as plain text", "err", err)
 
                        part.entities = nil
 
 
                        part.entities = nil
 
@@ -448,7 +448,7 @@ func (b *TelegramBot) deliver(ctx context.Context, part TelegramPart) {
                }
 
                if !retry {
                }
 
                if !retry {
-                       b.logger.Printf("reply lost: %v", err)
+                       b.logger.Error("reply lost", "err", err)
 
                        return
                }
 
                        return
                }
@@ -479,7 +479,7 @@ func (b *TelegramBot) deliverRich(ctx context.Context, markdown string) {
                }
 
                if telegramRejected(err) {
                }
 
                if telegramRejected(err) {
-                       b.logger.Printf("reply sent with simple formatting: %v", err)
+                       b.logger.Warn("reply sent with simple formatting", "err", err)
 
                        for _, part := range telegramReplyParts(telegramUnescapeTags(markdown)) {
                                b.deliver(ctx, part)
 
                        for _, part := range telegramReplyParts(telegramUnescapeTags(markdown)) {
                                b.deliver(ctx, part)
@@ -489,7 +489,7 @@ func (b *TelegramBot) deliverRich(ctx context.Context, markdown string) {
                }
 
                if !retry {
                }
 
                if !retry {
-                       b.logger.Printf("reply lost: %v", err)
+                       b.logger.Error("reply lost", "err", err)
 
                        return
                }
 
                        return
                }
@@ -520,7 +520,7 @@ func (b *TelegramBot) notice(text string) {
 // nothing. The next answer of Telegram logs back and the length of the outage.
 // loss and back say what the user loses and gets again.
 type TelegramOutage struct {
 // nothing. The next answer of Telegram logs back and the length of the outage.
 // loss and back say what the user loses and gets again.
 type TelegramOutage struct {
-       logger *log.Logger
+       logger *slog.Logger
        loss   string
        back   string
        start  time.Time
        loss   string
        back   string
        start  time.Time
@@ -533,7 +533,7 @@ func (o *TelegramOutage) Fail(err error, delay time.Duration) {
        }
 
        o.start = time.Now()
        }
 
        o.start = time.Now()
-       o.logger.Printf("%s: %v (retry in %s)", o.loss, err, durationText(delay))
+       o.logger.Warn(o.loss, "err", err, "retry", delay)
 }
 
 // End logs the end of the outage that runs, with its length.
 }
 
 // End logs the end of the outage that runs, with its length.
@@ -542,7 +542,7 @@ func (o *TelegramOutage) End() {
                return
        }
 
                return
        }
 
-       o.logger.Printf("%s after %s", o.back, durationText(time.Since(o.start)))
+       o.logger.Info(o.back, "after", time.Since(o.start))
 
        o.start = time.Time{}
 }
 
        o.start = time.Time{}
 }
@@ -721,10 +721,10 @@ func telegramRichCut(markdown string, limit int) (int, string, string) {
 
                end += start + 1
                text := strings.TrimRight(markdown[start:end], "\r\n")
 
                end += start + 1
                text := strings.TrimRight(markdown[start:end], "\r\n")
-               _, opens := telegramCodeFence(text)
+               _, opens := markdownCodeFence(text)
                switch {
                case fence != "":
                switch {
                case fence != "":
-                       if telegramClosesFence(text, fence) {
+                       if markdownClosesFence(text, fence) {
                                fence = ""
                        } else if end > len(opening)+1 && end+len(fence)+1 <= limit {
                                code, inFence, inOpening = end, fence, opening
                                fence = ""
                        } else if end > len(opening)+1 && end+len(fence)+1 <= limit {
                                code, inFence, inOpening = end, fence, opening