mirror of
https://github.com/usememos/memos.git
synced 2025-02-14 10:20:49 +01:00
chore: update tg message handler
This commit is contained in:
parent
425e85f0f9
commit
6b6edc3791
@ -13,7 +13,6 @@ func (b *Bot) handleSingleMessages(ctx context.Context, messages []Message) erro
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if attachment != nil {
|
if attachment != nil {
|
||||||
attachments = append(attachments, *attachment)
|
attachments = append(attachments, *attachment)
|
||||||
}
|
}
|
||||||
@ -33,7 +32,7 @@ func (b *Bot) handleGroupMessages(ctx context.Context, groupMessages []Message)
|
|||||||
messages := make(map[string]Message, len(groupMessages))
|
messages := make(map[string]Message, len(groupMessages))
|
||||||
attachments := make(map[string][]Attachment, len(groupMessages))
|
attachments := make(map[string][]Attachment, len(groupMessages))
|
||||||
|
|
||||||
// Group all captions, blobs and messages
|
// Group all captions, blobs and messages.
|
||||||
for _, message := range groupMessages {
|
for _, message := range groupMessages {
|
||||||
groupID := *message.MediaGroupID
|
groupID := *message.MediaGroupID
|
||||||
|
|
||||||
@ -53,9 +52,9 @@ func (b *Bot) handleGroupMessages(ctx context.Context, groupMessages []Message)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle each group message
|
// Handle each group message.
|
||||||
for groupID, message := range messages {
|
for groupID, message := range messages {
|
||||||
// replace Caption with all Caption in the group
|
// replace Caption with all Caption in the group.
|
||||||
caption := captions[groupID]
|
caption := captions[groupID]
|
||||||
message.Caption = &caption
|
message.Caption = &caption
|
||||||
err := b.handler.MessageHandle(ctx, b, message, attachments[groupID])
|
err := b.handler.MessageHandle(ctx, b, message, attachments[groupID])
|
||||||
|
@ -16,6 +16,7 @@ const (
|
|||||||
Strikethrough = "strikethrough" // “strikethrough” (strikethrough text)
|
Strikethrough = "strikethrough" // “strikethrough” (strikethrough text)
|
||||||
Code = "code" // “code” (monowidth string)
|
Code = "code" // “code” (monowidth string)
|
||||||
Pre = "pre" // “pre” (monowidth block)
|
Pre = "pre" // “pre” (monowidth block)
|
||||||
|
Spoiler = "spoiler" // “spoiler” (hidden text)
|
||||||
TextLink = "text_link" // “text_link” (for clickable text URLs)
|
TextLink = "text_link" // “text_link” (for clickable text URLs)
|
||||||
TextMention = "text_mention" // “text_mention” (for users without usernames)
|
TextMention = "text_mention" // “text_mention” (for users without usernames)
|
||||||
)
|
)
|
||||||
|
@ -6,17 +6,22 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"slices"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
"unicode/utf16"
|
"unicode/utf16"
|
||||||
|
|
||||||
"github.com/lithammer/shortuuid/v4"
|
"github.com/lithammer/shortuuid/v4"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
"github.com/yourselfhosted/gomark/ast"
|
||||||
|
"github.com/yourselfhosted/gomark/parser"
|
||||||
|
"github.com/yourselfhosted/gomark/parser/tokenizer"
|
||||||
|
|
||||||
"github.com/usememos/memos/plugin/telegram"
|
"github.com/usememos/memos/plugin/telegram"
|
||||||
"github.com/usememos/memos/plugin/webhook"
|
"github.com/usememos/memos/plugin/webhook"
|
||||||
storepb "github.com/usememos/memos/proto/gen/store"
|
storepb "github.com/usememos/memos/proto/gen/store"
|
||||||
apiv1 "github.com/usememos/memos/server/route/api/v1"
|
apiv1 "github.com/usememos/memos/server/route/api/v1"
|
||||||
|
apiv2 "github.com/usememos/memos/server/route/api/v2"
|
||||||
"github.com/usememos/memos/store"
|
"github.com/usememos/memos/store"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -48,6 +53,7 @@ func (t *TelegramHandler) MessageHandle(ctx context.Context, bot *telegram.Bot,
|
|||||||
return errors.Wrap(err, "Failed to SendReplyMessage")
|
return errors.Wrap(err, "Failed to SendReplyMessage")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
messageSenderID := strconv.FormatInt(message.From.ID, 10)
|
||||||
var creatorID int32
|
var creatorID int32
|
||||||
userSettingList, err := t.store.ListUserSettings(ctx, &store.FindUserSetting{
|
userSettingList, err := t.store.ListUserSettings(ctx, &store.FindUserSetting{
|
||||||
Key: storepb.UserSettingKey_USER_SETTING_TELEGRAM_USER_ID,
|
Key: storepb.UserSettingKey_USER_SETTING_TELEGRAM_USER_ID,
|
||||||
@ -56,11 +62,12 @@ func (t *TelegramHandler) MessageHandle(ctx context.Context, bot *telegram.Bot,
|
|||||||
return errors.Wrap(err, "Failed to find userSettingList")
|
return errors.Wrap(err, "Failed to find userSettingList")
|
||||||
}
|
}
|
||||||
for _, userSetting := range userSettingList {
|
for _, userSetting := range userSettingList {
|
||||||
if userSetting.GetTelegramUserId() == strconv.FormatInt(message.From.ID, 10) {
|
if userSetting.GetTelegramUserId() == messageSenderID {
|
||||||
creatorID = userSetting.UserId
|
creatorID = userSetting.UserId
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If creatorID is not found, ask the user to set the telegram userid in UserSetting of memos.
|
||||||
if creatorID == 0 {
|
if creatorID == 0 {
|
||||||
_, err := bot.EditMessage(ctx, message.Chat.ID, reply.MessageID, fmt.Sprintf("Please set your telegram userid %d in UserSetting of memos", message.From.ID), nil)
|
_, err := bot.EditMessage(ctx, message.Chat.ID, reply.MessageID, fmt.Sprintf("Please set your telegram userid %d in UserSetting of memos", message.From.ID), nil)
|
||||||
return err
|
return err
|
||||||
@ -71,26 +78,46 @@ func (t *TelegramHandler) MessageHandle(ctx context.Context, bot *telegram.Bot,
|
|||||||
CreatorID: creatorID,
|
CreatorID: creatorID,
|
||||||
Visibility: store.Private,
|
Visibility: store.Private,
|
||||||
}
|
}
|
||||||
|
|
||||||
if message.Text != nil {
|
if message.Text != nil {
|
||||||
create.Content = convertToMarkdown(*message.Text, message.Entities)
|
create.Content = convertToMarkdown(*message.Text, message.Entities)
|
||||||
}
|
}
|
||||||
|
|
||||||
if message.Caption != nil {
|
if message.Caption != nil {
|
||||||
create.Content = convertToMarkdown(*message.Caption, message.CaptionEntities)
|
create.Content = convertToMarkdown(*message.Caption, message.CaptionEntities)
|
||||||
}
|
}
|
||||||
|
|
||||||
if message.ForwardFromChat != nil {
|
if message.ForwardFromChat != nil {
|
||||||
create.Content += fmt.Sprintf("\n\n[Message link](%s)", message.GetMessageLink())
|
create.Content += fmt.Sprintf("\n\n[Message link](%s)", message.GetMessageLink())
|
||||||
}
|
}
|
||||||
|
|
||||||
memoMessage, err := t.store.CreateMemo(ctx, create)
|
memoMessage, err := t.store.CreateMemo(ctx, create)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
_, err := bot.EditMessage(ctx, message.Chat.ID, reply.MessageID, fmt.Sprintf("Failed to CreateMemo: %s", err), nil)
|
_, err := bot.EditMessage(ctx, message.Chat.ID, reply.MessageID, fmt.Sprintf("Failed to CreateMemo: %s", err), nil)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// create resources
|
// Dynamically upsert tags from memo content.
|
||||||
|
nodes, err := parser.Parse(tokenizer.Tokenize(create.Content))
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "Failed to parse content")
|
||||||
|
}
|
||||||
|
tags := []string{}
|
||||||
|
apiv2.TraverseASTNodes(nodes, func(node ast.Node) {
|
||||||
|
if tagNode, ok := node.(*ast.Tag); ok {
|
||||||
|
tag := tagNode.Content
|
||||||
|
if !slices.Contains(tags, tag) {
|
||||||
|
tags = append(tags, tag)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
for _, tag := range tags {
|
||||||
|
_, err := t.store.UpsertTag(ctx, &store.Tag{
|
||||||
|
Name: tag,
|
||||||
|
CreatorID: creatorID,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "Failed to upsert tag")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create memo related resources.
|
||||||
for _, attachment := range attachments {
|
for _, attachment := range attachments {
|
||||||
// Fill the common field of create
|
// Fill the common field of create
|
||||||
create := store.Resource{
|
create := store.Resource{
|
||||||
@ -117,9 +144,7 @@ func (t *TelegramHandler) MessageHandle(ctx context.Context, bot *telegram.Bot,
|
|||||||
|
|
||||||
keyboard := generateKeyboardForMemoID(memoMessage.ID)
|
keyboard := generateKeyboardForMemoID(memoMessage.ID)
|
||||||
_, err = bot.EditMessage(ctx, message.Chat.ID, reply.MessageID, fmt.Sprintf("Saved as %s Memo %d", memoMessage.Visibility, memoMessage.ID), keyboard)
|
_, err = bot.EditMessage(ctx, message.Chat.ID, reply.MessageID, fmt.Sprintf("Saved as %s Memo %d", memoMessage.Visibility, memoMessage.ID), keyboard)
|
||||||
|
|
||||||
_ = t.dispatchMemoRelatedWebhook(ctx, *memoMessage, "memos.memo.created")
|
_ = t.dispatchMemoRelatedWebhook(ctx, *memoMessage, "memos.memo.created")
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -233,6 +258,9 @@ func convertToMarkdown(text string, messageEntities []telegram.MessageEntity) st
|
|||||||
case telegram.TextLink:
|
case telegram.TextLink:
|
||||||
before = "["
|
before = "["
|
||||||
after = fmt.Sprintf(`](%s)`, e.URL)
|
after = fmt.Sprintf(`](%s)`, e.URL)
|
||||||
|
case telegram.Spoiler:
|
||||||
|
before = "||"
|
||||||
|
after = "||"
|
||||||
}
|
}
|
||||||
|
|
||||||
if before != "" {
|
if before != "" {
|
||||||
|
@ -111,7 +111,7 @@ func (s *APIV2Service) RenameTag(ctx context.Context, request *apiv2pb.RenameTag
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, status.Errorf(codes.Internal, "failed to parse memo: %v", err)
|
return nil, status.Errorf(codes.Internal, "failed to parse memo: %v", err)
|
||||||
}
|
}
|
||||||
traverseASTNodes(nodes, func(node ast.Node) {
|
TraverseASTNodes(nodes, func(node ast.Node) {
|
||||||
if tag, ok := node.(*ast.Tag); ok && tag.Content == request.OldName {
|
if tag, ok := node.(*ast.Tag); ok && tag.Content == request.OldName {
|
||||||
tag.Content = request.NewName
|
tag.Content = request.NewName
|
||||||
}
|
}
|
||||||
@ -215,7 +215,7 @@ func (s *APIV2Service) GetTagSuggestions(ctx context.Context, request *apiv2pb.G
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Dynamically upsert tags from memo content.
|
// Dynamically upsert tags from memo content.
|
||||||
traverseASTNodes(nodes, func(node ast.Node) {
|
TraverseASTNodes(nodes, func(node ast.Node) {
|
||||||
if tagNode, ok := node.(*ast.Tag); ok {
|
if tagNode, ok := node.(*ast.Tag); ok {
|
||||||
tag := tagNode.Content
|
tag := tagNode.Content
|
||||||
if !slices.Contains(tagNameList, tag) {
|
if !slices.Contains(tagNameList, tag) {
|
||||||
@ -248,24 +248,24 @@ func (s *APIV2Service) convertTagFromStore(ctx context.Context, tag *store.Tag)
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func traverseASTNodes(nodes []ast.Node, fn func(ast.Node)) {
|
func TraverseASTNodes(nodes []ast.Node, fn func(ast.Node)) {
|
||||||
for _, node := range nodes {
|
for _, node := range nodes {
|
||||||
fn(node)
|
fn(node)
|
||||||
switch n := node.(type) {
|
switch n := node.(type) {
|
||||||
case *ast.Paragraph:
|
case *ast.Paragraph:
|
||||||
traverseASTNodes(n.Children, fn)
|
TraverseASTNodes(n.Children, fn)
|
||||||
case *ast.Heading:
|
case *ast.Heading:
|
||||||
traverseASTNodes(n.Children, fn)
|
TraverseASTNodes(n.Children, fn)
|
||||||
case *ast.Blockquote:
|
case *ast.Blockquote:
|
||||||
traverseASTNodes(n.Children, fn)
|
TraverseASTNodes(n.Children, fn)
|
||||||
case *ast.OrderedList:
|
case *ast.OrderedList:
|
||||||
traverseASTNodes(n.Children, fn)
|
TraverseASTNodes(n.Children, fn)
|
||||||
case *ast.UnorderedList:
|
case *ast.UnorderedList:
|
||||||
traverseASTNodes(n.Children, fn)
|
TraverseASTNodes(n.Children, fn)
|
||||||
case *ast.TaskList:
|
case *ast.TaskList:
|
||||||
traverseASTNodes(n.Children, fn)
|
TraverseASTNodes(n.Children, fn)
|
||||||
case *ast.Bold:
|
case *ast.Bold:
|
||||||
traverseASTNodes(n.Children, fn)
|
TraverseASTNodes(n.Children, fn)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user