Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ DEFAULT_ENABLE_SILENT=false
DEFAULT_ENABLE_NSFW=false
DEFAULT_MEDIA_ALBUM_LIMIT=10
DEFAULT_LANGUAGE=en
DEFAULT_DELETE_LINKS=false

# other
REPO_URL=https://github.com/govdbot/govd
PROFILER_PORT=6060 # default pprof port
METRICS_PORT=8080 # default prometheus metrics port
LOG_LEVEL=info
WHITELIST=id1,id2,id3
CAPTIONS_HEADER="<a href='{{url}}'>source</a> - @{{username}}"
CAPTIONS_HEADER="{{mention}}:\n<a href='{{url}}'>source</a> - @{{username}}"
CAPTIONS_DESCRIPTION="<blockquote expandable>{{text}}</blockquote>"
ADMINS=id1,id2
AUTOMATIC_LANGUAGE_DETECTION=true
Expand Down
1 change: 1 addition & 0 deletions internal/core/inline.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ func HandleInlineResultTask(
taskResult.Media,
bot.Username,
extractorCtx.Chat.Captions,
ctx.EffectiveUser,
)

err = SendInlineFormats(
Expand Down
1 change: 1 addition & 0 deletions internal/core/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func HandleDownloadTask(
taskResult.Media,
bot.Username,
extractorCtx.Chat.Captions,
ctx.EffectiveUser,
)

_, err = SendFormats(
Expand Down
25 changes: 23 additions & 2 deletions internal/core/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/govdbot/govd/internal/util"
"github.com/govdbot/govd/internal/util/download"
"github.com/govdbot/govd/internal/util/libav"
"github.com/PaulSonOfLars/gotgbot/v2"
)

var ErrNoMedia = errors.New("no media found")
Expand Down Expand Up @@ -86,22 +87,42 @@ func insertVideoInfo(format *models.MediaFormat, filePath string) {
format.Height = height
}

func formatCaption(media *models.Media, username string, isEnabled bool) string {
func formatCaption(media *models.Media, botUsername string, isEnabled bool, user *gotgbot.User) string {
caption := media.Caption
if len(caption) > 600 {
caption = caption[:600] + "..."
}

formatText := func(s string) string {
s = strings.ReplaceAll(s, "{{username}}", username)
var displayName string
if user.Username != "" {
displayName = "@" + user.Username
} else {
displayName = user.FirstName
if user.LastName != "" {
displayName += " " + user.LastName
}
}

// Wrap the name/username in a permanent ID link to handle username changes
mention := fmt.Sprintf("<a href=\"tg://user?id=%d\">%s</a>", user.Id, displayName)

s = strings.ReplaceAll(s, "{{username}}", botUsername)
s = strings.ReplaceAll(s, "{{url}}", media.ContentURL)
s = strings.ReplaceAll(s, "{{text}}", util.Unquote(caption))
s = strings.ReplaceAll(s, "{{mention}}", mention)
return s
}

var description string
header := formatText(config.Env.CaptionsHeader)
if isEnabled && caption != "" {
description = formatText(config.Env.CaptionsDescription)
}

if description == "" {
return header
}
return header + "\n" + description
}

Expand Down