mirror of
https://github.com/kemko/memes-telegram-integration.git
synced 2026-01-01 15:55:41 +03:00
feat: add search for memos (#47)
* feat: add search for memos Add "/search string" command which performs search for notes. Fixes: #42 * fix: decrease page size to 10 Co-authored-by: boojack <stevenlgtm@gmail.com> --------- Co-authored-by: boojack <stevenlgtm@gmail.com>
This commit is contained in:
@@ -81,3 +81,4 @@ Or you can start the service with Docker Compose. This can be combined with the
|
||||
- `/start <access_token>`: Start the bot with your Memos access token.
|
||||
- Send text messages: Save the message content as a memo.
|
||||
- Send files (photos, documents): Save the files as resources in a memo.
|
||||
- `/search <words>`: Search for the memos.
|
||||
|
||||
41
memogram.go
41
memogram.go
@@ -78,6 +78,9 @@ func (s *Service) handler(ctx context.Context, b *bot.Bot, m *models.Update) {
|
||||
if strings.HasPrefix(m.Message.Text, "/start ") {
|
||||
s.startHandler(ctx, b, m)
|
||||
return
|
||||
} else if strings.HasPrefix(m.Message.Text, "/search ") {
|
||||
s.searchHandler(ctx, b, m)
|
||||
return
|
||||
}
|
||||
|
||||
userID := m.Message.From.ID
|
||||
@@ -205,6 +208,44 @@ func (s *Service) startHandler(ctx context.Context, b *bot.Bot, m *models.Update
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Service) searchHandler(ctx context.Context, b *bot.Bot, m *models.Update) {
|
||||
userID := m.Message.From.ID
|
||||
searchString := strings.TrimPrefix(m.Message.Text, "/search ")
|
||||
|
||||
filterString := "content_search == ['" + searchString + "']"
|
||||
|
||||
accessToken, _ := userAccessTokenCache.Load(userID)
|
||||
ctx = metadata.NewOutgoingContext(ctx, metadata.Pairs("Authorization", fmt.Sprintf("Bearer %s", accessToken.(string))))
|
||||
results, err := s.client.MemoService.ListMemos(ctx, &v1pb.ListMemosRequest{
|
||||
PageSize: 10,
|
||||
Filter: filterString,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
slog.Error("failed to search memos", slog.Any("err", err))
|
||||
return
|
||||
}
|
||||
|
||||
memos := results.GetMemos()
|
||||
|
||||
if len(memos) == 0 {
|
||||
b.SendMessage(ctx, &bot.SendMessageParams{
|
||||
ChatID: m.Message.Chat.ID,
|
||||
Text: "No memos found for the specified search criteria.",
|
||||
})
|
||||
} else {
|
||||
for _, memo := range results.GetMemos() {
|
||||
tgMessage := memo.Name + "\n" + memo.Content
|
||||
b.SendMessage(ctx, &bot.SendMessageParams{
|
||||
ChatID: m.Message.Chat.ID,
|
||||
Text: tgMessage,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (s *Service) saveResourceFromFile(ctx context.Context, file *models.File, memo *v1pb.Memo) (*v1pb.Resource, error) {
|
||||
fileLink := s.bot.FileDownloadLink(file)
|
||||
response, err := http.Get(fileLink)
|
||||
|
||||
Reference in New Issue
Block a user