Skip to content

Commit

Permalink
refactor: add convertToTGMarkdown function for Telegram bot
Browse files Browse the repository at this point in the history
  • Loading branch information
vaayne committed Jul 19, 2024
1 parent 399ddfd commit 8e6bb77
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 1 deletion.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module vibrain
go 1.22.4

require (
github.com/Mad-Pixels/goldmark-tgmd v0.0.10
github.com/caarlos0/env/v11 v11.1.0
github.com/golang-jwt/jwt/v5 v5.2.1
github.com/google/uuid v1.6.0
Expand All @@ -12,6 +13,7 @@ require (
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/riverqueue/river v0.9.0
github.com/riverqueue/river/riverdriver/riverpgxv5 v0.9.0
github.com/yuin/goldmark v1.6.0
golang.org/x/oauth2 v0.21.0
gopkg.in/telebot.v3 v3.3.6
)
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/DataDog/datadog-go v3.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ=
github.com/Mad-Pixels/goldmark-tgmd v0.0.10 h1:tDJ/twV0+NfM9kgVbcGLKzi8Ol+g0vExAWjZdGBSvh8=
github.com/Mad-Pixels/goldmark-tgmd v0.0.10/go.mod h1:miGhUbpxU/iDE66oKrDqbL0j0Jq7eGjk9vObhgsbOI8=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
Expand Down Expand Up @@ -413,6 +415,8 @@ github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
github.com/yuin/goldmark v1.6.0 h1:boZcn2GTjpsynOsC0iJHnBWa4Bi0qzfJjthwauItG68=
github.com/yuin/goldmark v1.6.0/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
go.etcd.io/etcd/api/v3 v3.5.4/go.mod h1:5GB2vv4A4AOn3yk7MftYGHkUfGtDHnEraIjym4dYz5A=
go.etcd.io/etcd/client/pkg/v3 v3.5.4/go.mod h1:IJHfcCEKxYu1Os13ZdwCwIUTUVGYTSAM3YSwc9/Ac1g=
go.etcd.io/etcd/client/v2 v2.305.4/go.mod h1:Ud+VUwIi9/uQHOMA+4ekToJ12lTxlv0zB/+DHwTGEbU=
Expand Down
43 changes: 43 additions & 0 deletions internal/port/bots/handlers/render.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package handlers

import (
"bytes"
"sync"
"vibrain/internal/pkg/logger"

tgmd "github.com/Mad-Pixels/goldmark-tgmd"
"github.com/yuin/goldmark"
)

var mdParser goldmark.Markdown

var once sync.Once

func convertToTGMarkdown(md string) string {

once.Do(func() {
mdParser = tgmd.TGMD()
tgmd.Config.UpdateHeading1(tgmd.Element{
Prefix: "\n📌 ",
Postfix: "\n",
Style: tgmd.BoldTg,
})
tgmd.Config.UpdateHeading2(tgmd.Element{
Prefix: "✏ ",
Style: tgmd.BoldTg,
})
tgmd.Config.UpdateHeading2(tgmd.Element{
Prefix: "📚 ",
Style: tgmd.BoldTg,
})
})

mdBytes := []byte(md)
var buf bytes.Buffer
if err := mdParser.Convert(mdBytes, &buf); err != nil {
logger.Default.Warn("Failed to convert markdown to HTML", "error", err.Error())
buf = *bytes.NewBuffer(mdBytes)
return md
}
return buf.String()
}
2 changes: 1 addition & 1 deletion internal/port/bots/handlers/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TextHandler(c tele.Context) error {
resp += chunk
resp = strings.ReplaceAll(resp, "\\n", "\n")
cache.New().Set(fmt.Sprintf("WebSummary:%s", url), resp, 24*time.Hour)
if _, err := c.Bot().Edit(msg, resp); err != nil {
if _, err := c.Bot().Edit(msg, convertToTGMarkdown(resp), tele.ModeMarkdownV2); err != nil {
if strings.Contains(err.Error(), "message is not modified") {
return nil
}
Expand Down

0 comments on commit 8e6bb77

Please sign in to comment.