Skip to content

Commit

Permalink
feat: add search for memos (#47)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
okainov and boojack authored Aug 14, 2024
1 parent b54a558 commit 7d67fc9
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 41 additions & 0 deletions memogram.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 7d67fc9

Please sign in to comment.