Skip to content

Commit

Permalink
Merge pull request #12 from kkdai/add_uid
Browse files Browse the repository at this point in the history
feat: implement user ID filtering in query and add functions
  • Loading branch information
kkdai authored Jan 15, 2024
2 parents bd1a1f9 + 5abbd95 commit 2aecc40
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 17 deletions.
16 changes: 14 additions & 2 deletions bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func callbackHandler(w http.ResponseWriter, r *http.Request) {
switch message := e.Message.(type) {
// Handle only on text message
case webhook.TextMessageContent:
// TODO: Will add LLM namecard search function here.
log.Printf("Got text message, ID: %s, text: %s", message.Id, message.Text)

// Handle only on Sticker message
Expand All @@ -66,6 +67,17 @@ func callbackHandler(w http.ResponseWriter, r *http.Request) {

// Handle only image message
case webhook.ImageMessageContent:
// 取得用戶 ID
var uID string
switch source := e.Source.(type) {
case *webhook.UserSource:
uID = source.UserId
case *webhook.GroupSource:
uID = source.UserId
case *webhook.RoomSource:
uID = source.UserId
}

log.Println("Got img msg ID:", message.Id)
//Get image binary from LINE server based on message ID.
data, err := GetImageBinary(blob, message.Id)
Expand Down Expand Up @@ -103,7 +115,7 @@ func callbackHandler(w http.ResponseWriter, r *http.Request) {
}

// Check email first before adding to database.
dbUser, err := nDB.QueryDatabaseByEmail(person.Email)
dbUser, err := nDB.QueryDatabaseByEmail(uID, person.Email)
if err == nil && len(dbUser) > 0 {
log.Println("Already exist in DB", dbUser)
if err := replyText(e.ReplyToken, "已經存在於資料庫中,請勿重複輸入"+"\n"+jsonData); err != nil {
Expand All @@ -113,7 +125,7 @@ func callbackHandler(w http.ResponseWriter, r *http.Request) {
}

// Add namecard to notion database.
err = nDB.AddPageToDatabase(person.Name, person.Title, person.Address, person.Email, person.PhoneNumber)
err = nDB.AddPageToDatabase(uID, person.Name, person.Title, person.Address, person.Email, person.PhoneNumber)
if err != nil {
log.Println("Error adding page to database:", err)
}
Expand Down
41 changes: 29 additions & 12 deletions notion.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,24 @@ type NotionDBEntry struct {
}

// QueryDatabase 根據提供的屬性和值查詢 Notion 資料庫。
func (n *NotionDB) QueryDatabase(property, value string) ([]NotionDBEntry, error) {
func (n *NotionDB) QueryDatabase(UId, property, value string) ([]NotionDBEntry, error) {
client := notionapi.NewClient(notionapi.Token(n.Token))

// Add UId to the filter conditions
// 建立查詢過濾條件
filter := &notionapi.DatabaseQueryRequest{
Filter: &notionapi.PropertyFilter{
Property: property,
RichText: &notionapi.TextFilterCondition{
Equals: value,
Filter: notionapi.AndCompoundFilter{
notionapi.PropertyFilter{
Property: property,
RichText: &notionapi.TextFilterCondition{
Equals: value,
},
},
notionapi.PropertyFilter{
Property: "UID",
RichText: &notionapi.TextFilterCondition{
Equals: UId,
},
},
},
}
Expand Down Expand Up @@ -83,22 +92,30 @@ func (n *NotionDB) getPropertyValue(page *notionapi.Page, property string) strin
return ""
}

// QueryDatabaseByName 根據提供的標題和名稱查詢 Notion 資料庫。
func (n *NotionDB) QueryDatabaseByName(name string) ([]NotionDBEntry, error) {
return n.QueryDatabase("Name", name)
// QueryDatabaseByName 根據提供的名稱和UId查詢 Notion 資料庫。
func (n *NotionDB) QueryDatabaseByName(name, UId string) ([]NotionDBEntry, error) {
return n.QueryDatabase(UId, "Name", name)
}

// QueryDatabaseByEmail 根據提供的電子郵件地址查詢 Notion 資料庫。
func (n *NotionDB) QueryDatabaseByEmail(email string) ([]NotionDBEntry, error) {
return n.QueryDatabase("Email", email)
// QueryDatabaseByEmail 根據提供的電子郵件地址和UId查詢 Notion 資料庫。
func (n *NotionDB) QueryDatabaseByEmail(email, UId string) ([]NotionDBEntry, error) {
return n.QueryDatabase(UId, "Email", email)
}

// AddPageToDatabase adds a new page with the provided field values to the specified Notion database.
func (n *NotionDB) AddPageToDatabase(name string, title string, address string, email string, phoneNumber string) error {
func (n *NotionDB) AddPageToDatabase(Uid string, name string, title string, address string, email string, phoneNumber string) error {
client := notionapi.NewClient(notionapi.Token(n.Token))

// 建立 Properties 物件來設置頁面屬性
properties := notionapi.Properties{
"UID": notionapi.RichTextProperty{
RichText: []notionapi.RichText{
{
PlainText: Uid,
Text: &notionapi.Text{Content: Uid},
},
},
},
"Name": notionapi.TitleProperty{
Title: []notionapi.RichText{
{
Expand Down
6 changes: 3 additions & 3 deletions notion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ func TestQueryNotionDB(t *testing.T) {
Token: token,
}

entries, err := db.QueryDatabaseByName("111")
entries, err := db.QueryDatabaseByName("name", "uid")
if err != nil {
t.Fatal(err)
}
fmt.Printf("%+v\n", entries)

entries, err = db.QueryDatabaseByEmail("[email protected]")
entries, err = db.QueryDatabaseByEmail("[email protected]", "uid")
if err != nil {
t.Fatal(err)
}
Expand All @@ -48,7 +48,7 @@ func TestAddNotionDB(t *testing.T) {
Token: token,
}

err := db.AddPageToDatabase("name", "title", "address", "[email protected]", "phone")
err := db.AddPageToDatabase("uid", "name", "title", "address", "[email protected]", "phone")
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit 2aecc40

Please sign in to comment.