135 lines
3.2 KiB
Go
135 lines
3.2 KiB
Go
package main
|
|
|
|
import (
|
|
"strings"
|
|
|
|
maxschemes "github.com/max-messenger/max-bot-api-client-go/schemes"
|
|
)
|
|
|
|
func tgName(msg *TGMessage) string {
|
|
if msg.From == nil {
|
|
if msg.SenderChat != nil {
|
|
return msg.SenderChat.Title
|
|
}
|
|
return "Unknown"
|
|
}
|
|
name := msg.From.FirstName
|
|
if msg.From.LastName != "" {
|
|
name += " " + msg.From.LastName
|
|
}
|
|
return name
|
|
}
|
|
|
|
// formatAttribution собирает строку "Имя: текст" или "Имя:\nтекст" в зависимости от настройки.
|
|
func formatAttribution(name, text string, newline bool) string {
|
|
if newline {
|
|
return name + ":\n" + text
|
|
}
|
|
return name + ": " + text
|
|
}
|
|
|
|
// formatTgCaption — для пересылки (текст или caption)
|
|
func formatTgCaption(msg *TGMessage, prefix, newline bool) string {
|
|
name := tgName(msg)
|
|
text := msg.Text
|
|
if text == "" {
|
|
text = msg.Caption
|
|
}
|
|
if prefix {
|
|
return formatAttribution("[TG] "+name, text, newline)
|
|
}
|
|
return formatAttribution(name, text, newline)
|
|
}
|
|
|
|
// formatTgMessage — для edit (полный формат)
|
|
func formatTgMessage(msg *TGMessage, prefix, newline bool) string {
|
|
name := tgName(msg)
|
|
text := msg.Text
|
|
if text == "" {
|
|
text = msg.Caption
|
|
}
|
|
if text == "" {
|
|
return ""
|
|
}
|
|
if prefix {
|
|
return formatAttribution("[TG] "+name, text, newline)
|
|
}
|
|
return formatAttribution(name, text, newline)
|
|
}
|
|
|
|
func maxName(upd *maxschemes.MessageCreatedUpdate) string {
|
|
name := upd.Message.Sender.Name
|
|
if name == "" {
|
|
name = upd.Message.Sender.Username
|
|
}
|
|
return name
|
|
}
|
|
|
|
// formatMaxCaption — для пересылки
|
|
func formatMaxCaption(upd *maxschemes.MessageCreatedUpdate, prefix, newline bool) string {
|
|
name := maxName(upd)
|
|
text := upd.Message.Body.Text
|
|
if prefix {
|
|
return formatAttribution("[MAX] "+name, text, newline)
|
|
}
|
|
return formatAttribution(name, text, newline)
|
|
}
|
|
|
|
// formatTgCrosspostCaption — для кросспостинга каналов (без attribution и префиксов)
|
|
func formatTgCrosspostCaption(msg *TGMessage) string {
|
|
text := msg.Text
|
|
if text == "" {
|
|
text = msg.Caption
|
|
}
|
|
return text
|
|
}
|
|
|
|
// formatMaxCrosspostCaption — для кросспостинга каналов (без attribution и префиксов)
|
|
func formatMaxCrosspostCaption(upd *maxschemes.MessageCreatedUpdate) string {
|
|
return upd.Message.Body.Text
|
|
}
|
|
|
|
// mimeToFilename генерирует имя файла из MIME-типа, если оригинальное имя отсутствует.
|
|
func mimeToFilename(base, mime string) string {
|
|
ext := ""
|
|
// sub = часть после "/" в mime type
|
|
if i := strings.Index(mime, "/"); i >= 0 {
|
|
sub := mime[i+1:]
|
|
switch sub {
|
|
case "mp4":
|
|
ext = ".mp4"
|
|
case "webm":
|
|
ext = ".webm"
|
|
case "x-matroska":
|
|
ext = ".mkv"
|
|
case "quicktime":
|
|
ext = ".mov"
|
|
case "mpeg":
|
|
ext = ".mpeg"
|
|
case "ogg":
|
|
ext = ".ogg"
|
|
case "pdf":
|
|
ext = ".pdf"
|
|
case "gif":
|
|
ext = ".gif"
|
|
default:
|
|
ext = "." + sub
|
|
}
|
|
}
|
|
return base + ext
|
|
}
|
|
|
|
// fileNameFromURL извлекает имя файла из URL, fallback "file".
|
|
func fileNameFromURL(rawURL string) string {
|
|
if idx := strings.LastIndex(rawURL, "/"); idx >= 0 {
|
|
name := rawURL[idx+1:]
|
|
if q := strings.Index(name, "?"); q >= 0 {
|
|
name = name[:q]
|
|
}
|
|
if name != "" {
|
|
return name
|
|
}
|
|
}
|
|
return "file"
|
|
}
|