mirror of https://github.com/usememos/memos
feat: format message from telegram and upload attachments (#1924)
* feat: format message from telegram and download documents * fix: remove bool in expression * refactor: convert to markdown * refactor: resolve remarks and add support new message types * refactor: resolve remarks * feat: add test for mime type --------- Co-authored-by: Александр Тумайкин <AATumaykin@tsum.ru>pull/1950/head
parent
f074bb1be2
commit
c5a1f4c839
@ -0,0 +1,14 @@
|
|||||||
|
package telegram
|
||||||
|
|
||||||
|
// Animation represents an animation file.
|
||||||
|
type Animation struct {
|
||||||
|
FileID string `json:"file_id"` // FileID is the identifier for this file, which can be used to download or reuse the file
|
||||||
|
FileUniqueID string `json:"file_unique_id"` // FileUniqueID is the unique identifier for this file, which is supposed to be the same over time and for different bots. Can't be used to download or reuse the file.
|
||||||
|
Width int `json:"width"` // Width video width as defined by sender
|
||||||
|
Height int `json:"height"` // Height video height as defined by sender
|
||||||
|
Duration int `json:"duration"` // Duration of the video in seconds as defined by sender
|
||||||
|
Thumbnail *PhotoSize `json:"thumb"` // Thumbnail animation thumbnail as defined by sender
|
||||||
|
FileName string `json:"file_name"` // FileName original animation filename as defined by sender
|
||||||
|
MimeType string `json:"mime_type"` // MimeType of the file as defined by sender
|
||||||
|
FileSize int `json:"file_size"`
|
||||||
|
}
|
@ -0,0 +1,38 @@
|
|||||||
|
package telegram
|
||||||
|
|
||||||
|
import (
|
||||||
|
"path"
|
||||||
|
|
||||||
|
"github.com/usememos/memos/common/log"
|
||||||
|
"go.uber.org/zap"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Attachment struct {
|
||||||
|
FileName string
|
||||||
|
MimeType string
|
||||||
|
FileSize int64
|
||||||
|
Data []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
var mimeTypes = map[string]string{
|
||||||
|
".jpg": "image/jpeg",
|
||||||
|
".png": "image/png",
|
||||||
|
".mp4": "video/mp4", // for video note
|
||||||
|
".oga": "audio/ogg", // for voice
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b Attachment) GetMimeType() string {
|
||||||
|
if b.MimeType != "" {
|
||||||
|
return b.MimeType
|
||||||
|
}
|
||||||
|
|
||||||
|
mime, ok := mimeTypes[path.Ext(b.FileName)]
|
||||||
|
if !ok {
|
||||||
|
// Handle unknown file extension
|
||||||
|
log.Warn("Unknown file type for ", zap.String("filename", b.FileName))
|
||||||
|
|
||||||
|
return "application/octet-stream"
|
||||||
|
}
|
||||||
|
|
||||||
|
return mime
|
||||||
|
}
|
@ -0,0 +1,83 @@
|
|||||||
|
package telegram
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGetMimeType(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
mimeType string
|
||||||
|
fileName string
|
||||||
|
expected string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
fileName: "file.jpg",
|
||||||
|
mimeType: "image/jpeg",
|
||||||
|
expected: "image/jpeg",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
fileName: "file.png",
|
||||||
|
mimeType: "image/png",
|
||||||
|
expected: "image/png",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
fileName: "file.pdf",
|
||||||
|
mimeType: "application/pdf",
|
||||||
|
expected: "application/pdf",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
fileName: "file.php",
|
||||||
|
mimeType: "application/x-php",
|
||||||
|
expected: "application/x-php",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
fileName: "file.xlsx",
|
||||||
|
mimeType: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
||||||
|
expected: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
fileName: "file.oga",
|
||||||
|
mimeType: "audio/ogg",
|
||||||
|
expected: "audio/ogg",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
fileName: "file.jpg",
|
||||||
|
expected: "image/jpeg",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
fileName: "file.png",
|
||||||
|
expected: "image/png",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
fileName: "file.mp4",
|
||||||
|
expected: "video/mp4",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
fileName: "file.pdf",
|
||||||
|
expected: "application/octet-stream",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
fileName: "file.oga",
|
||||||
|
expected: "audio/ogg",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
fileName: "file.xlsx",
|
||||||
|
expected: "application/octet-stream",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
fileName: "file.txt",
|
||||||
|
expected: "application/octet-stream",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
attachment := Attachment{
|
||||||
|
FileName: test.fileName,
|
||||||
|
MimeType: test.mimeType,
|
||||||
|
}
|
||||||
|
|
||||||
|
require.Equal(t, test.expected, attachment.GetMimeType())
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
package telegram
|
||||||
|
|
||||||
|
// Audio represents an audio file to be treated as music by the Telegram clients.
|
||||||
|
type Audio struct {
|
||||||
|
FileID string `json:"file_id"` // FileID is an identifier for this file, which can be used to download or reuse the file
|
||||||
|
FileUniqueID string `json:"file_unique_id"` // FileUniqueID is the unique identifier for this file, which is supposed to be the same over time and for different bots. Can't be used to download or reuse the file.
|
||||||
|
Duration int `json:"duration"` // Duration of the audio in seconds as defined by sender
|
||||||
|
Performer string `json:"performer"` // Performer of the audio as defined by sender or by audio tags
|
||||||
|
Title string `json:"title"` // Title of the audio as defined by sender or by audio tags
|
||||||
|
FileName string `json:"file_name"` // FileName is the original filename as defined by sender
|
||||||
|
MimeType string `json:"mime_type"` // MimeType of the file as defined by sender
|
||||||
|
FileSize int `json:"file_size"` // FileSize file size
|
||||||
|
Thumbnail *PhotoSize `json:"thumb"` // Thumbnail is the album cover to which the music file belongs
|
||||||
|
}
|
@ -1,9 +1,19 @@
|
|||||||
package telegram
|
package telegram
|
||||||
|
|
||||||
|
type ChatType string
|
||||||
|
|
||||||
|
const (
|
||||||
|
Private = "private"
|
||||||
|
Group = "group"
|
||||||
|
SuperGroup = "supergroup"
|
||||||
|
Channel = "channel"
|
||||||
|
)
|
||||||
|
|
||||||
type Chat struct {
|
type Chat struct {
|
||||||
ID int `json:"id"`
|
ID int `json:"id"`
|
||||||
Title string `json:"title"`
|
Title string `json:"title"` // Title for supergroups, channels and group chats
|
||||||
Type string `json:"type"`
|
Type ChatType `json:"type"` // Type of chat, can be either “private”, “group”, “supergroup” or “channel”
|
||||||
FirstName string `json:"first_name"`
|
FirstName string `json:"first_name"` // FirstName of the other party in a private chat
|
||||||
LastName string `json:"last_name"`
|
LastName string `json:"last_name"` // LastName of the other party in a private chat
|
||||||
|
UserName string `json:"username"` // UserName for private chats, supergroups and channels if available
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,11 @@
|
|||||||
|
package telegram
|
||||||
|
|
||||||
|
// Document represents a general file.
|
||||||
|
type Document struct {
|
||||||
|
FileID string `json:"file_id"` // FileID is an identifier for this file, which can be used to download or reuse the file
|
||||||
|
FileUniqueID string `json:"file_unique_id"` // FileUniqueID is the unique identifier for this file, which is supposed to be the same over time and for different bots. Can't be used to download or reuse the file.
|
||||||
|
Thumbnail *PhotoSize `json:"thumb"` // Thumbnail document thumbnail as defined by sender
|
||||||
|
FileName string `json:"file_name"` // FileName original filename as defined by sender
|
||||||
|
MimeType string `json:"mime_type"` // MimeType of the file as defined by sender
|
||||||
|
FileSize int `json:"file_size"`
|
||||||
|
}
|
@ -0,0 +1,31 @@
|
|||||||
|
package telegram
|
||||||
|
|
||||||
|
type MessageEntityType string
|
||||||
|
|
||||||
|
const (
|
||||||
|
Mention = "mention" // “mention” (@username)
|
||||||
|
Hashtag = "hashtag" // “hashtag” (#hashtag)
|
||||||
|
CashTag = "cashtag" // “cashtag” ($USD)
|
||||||
|
BotCommand = "bot_command" // “bot_command” (/start@jobs_bot)
|
||||||
|
URL = "url" // “url” (https://telegram.org)
|
||||||
|
Email = "email" // “email” (do-not-reply@telegram.org)
|
||||||
|
PhoneNumber = "phone_number" // “phone_number” (+1-212-555-0123)
|
||||||
|
Bold = "bold" // “bold” (bold text)
|
||||||
|
Italic = "italic" // “italic” (italic text)
|
||||||
|
Underline = "underline" // “underline” (underlined text)
|
||||||
|
Strikethrough = "strikethrough" // “strikethrough” (strikethrough text)
|
||||||
|
Code = "code" // “code” (monowidth string)
|
||||||
|
Pre = "pre" // “pre” (monowidth block)
|
||||||
|
TextLink = "text_link" // “text_link” (for clickable text URLs)
|
||||||
|
TextMention = "text_mention" // “text_mention” (for users without usernames)
|
||||||
|
)
|
||||||
|
|
||||||
|
// MessageEntity represents one special entity in a text message.
|
||||||
|
type MessageEntity struct {
|
||||||
|
Type MessageEntityType `json:"type"` // Type of the entity.
|
||||||
|
Offset int `json:"offset"` // Offset in UTF-16 code units to the start of the entity
|
||||||
|
Length int `json:"length"`
|
||||||
|
URL string `json:"url"` // URL for “text_link” only, url that will be opened after user taps on the text
|
||||||
|
User *User `json:"user"` // User for “text_mention” only, the mentioned user
|
||||||
|
Language string `json:"language"` // Language for “pre” only, the programming language of the entity text
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
package telegram
|
||||||
|
|
||||||
|
// Video represents a video file.
|
||||||
|
type Video struct {
|
||||||
|
FileID string `json:"file_id"` // FileID identifier for this file, which can be used to download or reuse
|
||||||
|
FileUniqueID string `json:"file_unique_id"` // FileUniqueID is the unique identifier for this file, which is supposed to be the same over time and for different bots. Can't be used to download or reuse the file.
|
||||||
|
Width int `json:"width"` // Width video width as defined by sender
|
||||||
|
Height int `json:"height"` // Height video height as defined by sender
|
||||||
|
Duration int `json:"duration"` // Duration of the video in seconds as defined by sender
|
||||||
|
Thumbnail *PhotoSize `json:"thumb"` // Thumbnail video thumbnail
|
||||||
|
FileName string `json:"file_name"` // FileName is the original filename as defined by sender
|
||||||
|
MimeType string `json:"mime_type"` // MimeType of a file as defined by sender
|
||||||
|
FileSize int `json:"file_size"`
|
||||||
|
}
|
@ -0,0 +1,11 @@
|
|||||||
|
package telegram
|
||||||
|
|
||||||
|
// VideoNote object represents a video message.
|
||||||
|
type VideoNote struct {
|
||||||
|
FileID string `json:"file_id"` // FileID identifier for this file, which can be used to download or reuse the file
|
||||||
|
FileUniqueID string `json:"file_unique_id"` // FileUniqueID is the unique identifier for this file, which is supposed to be the same over time and for different bots. Can't be used to download or reuse the file.
|
||||||
|
Length int `json:"length"` // Length video width and height (diameter of the video message) as defined by sender
|
||||||
|
Duration int `json:"duration"` // Duration of the video in seconds as defined by sender
|
||||||
|
Thumbnail *PhotoSize `json:"thumb,omitempty"` // Thumbnail video thumbnail
|
||||||
|
FileSize int `json:"file_size"`
|
||||||
|
}
|
@ -0,0 +1,10 @@
|
|||||||
|
package telegram
|
||||||
|
|
||||||
|
// Voice represents a voice note.
|
||||||
|
type Voice struct {
|
||||||
|
FileID string `json:"file_id"` // FileID identifier for this file, which can be used to download or reuse the file
|
||||||
|
FileUniqueID string `json:"file_unique_id"` // FileUniqueID is the unique identifier for this file, which is supposed to be the same over time and for different bots. Can't be used to download or reuse the file.
|
||||||
|
Duration int `json:"duration"` // Duration of the audio in seconds as defined by sender
|
||||||
|
MimeType string `json:"mime_type"` // MimeType of the file as defined by sender
|
||||||
|
FileSize int `json:"file_size"`
|
||||||
|
}
|
Loading…
Reference in New Issue