-
Notifications
You must be signed in to change notification settings - Fork 9
/
notion.go
248 lines (219 loc) · 6.76 KB
/
notion.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package main
import (
"context"
"fmt"
"log"
"github.com/jomei/notionapi"
)
// Person 定義了 JSON 資料的結構體
type Person struct {
Name string `json:"name"`
Title string `json:"title"`
Address string `json:"address"`
Email string `json:"email"`
Phone string `json:"phone"`
Company string `json:"company"`
}
// DatabaseEntry 定義了 Notion 資料庫條目的結構體。
type NotionDB struct {
DatabaseID string
Token string
UID string
}
// QueryDatabaseWithFilter 根據提供的過濾器查詢 Notion 資料庫。
func (n *NotionDB) queryDatabaseWithFilter(filter *notionapi.DatabaseQueryRequest) ([]Person, error) {
client := notionapi.NewClient(notionapi.Token(n.Token))
result, err := client.Database.Query(context.Background(), notionapi.DatabaseID(n.DatabaseID), filter)
if err != nil {
return nil, fmt.Errorf("error querying database: %w", err)
}
var entries []Person
for _, page := range result.Results {
entry := n.createEntryFromPage(&page)
entries = append(entries, entry)
}
return entries, nil
}
// QueryDatabase 根據提供的屬性和值查詢 Notion 資料庫。
func (n *NotionDB) QueryDatabase(property, value string) ([]Person, error) {
filter := ¬ionapi.DatabaseQueryRequest{
Filter: notionapi.AndCompoundFilter{
notionapi.PropertyFilter{
Property: property,
RichText: ¬ionapi.TextFilterCondition{
Equals: value,
},
},
notionapi.PropertyFilter{
Property: "UID",
RichText: ¬ionapi.TextFilterCondition{
Equals: n.UID,
},
},
},
}
return n.queryDatabaseWithFilter(filter)
}
// QueryDatabaseContains 根據提供的屬性和值查詢 Notion 資料庫。
func (n *NotionDB) QueryContainsDatabase(property, value string) ([]Person, error) {
filter := ¬ionapi.DatabaseQueryRequest{
Filter: notionapi.AndCompoundFilter{
notionapi.PropertyFilter{
Property: property,
RichText: ¬ionapi.TextFilterCondition{
Contains: value,
},
},
notionapi.PropertyFilter{
Property: "UID",
RichText: ¬ionapi.TextFilterCondition{
Equals: n.UID,
},
},
},
}
return n.queryDatabaseWithFilter(filter)
}
func (n *NotionDB) QueryDatabaseContainsByEmail(email string) ([]Person, error) {
return n.QueryContainsDatabase("Email", email)
}
// QueryDatabaseByEmail 根據提供的電子郵件地址查詢 Notion 資料庫。
func (n *NotionDB) QueryDatabaseByEmail(email string) ([]Person, error) {
return n.QueryDatabase("Email", email)
}
// AddPageToDatabase adds a new page with the provided field values to the specified Notion database.
func (n *NotionDB) AddPageToDatabase(person Person) error {
client := notionapi.NewClient(notionapi.Token(n.Token))
// 建立 Properties 物件來設置頁面屬性
properties := notionapi.Properties{
"UID": notionapi.TitleProperty{
Title: []notionapi.RichText{
{
PlainText: n.UID,
Text: ¬ionapi.Text{Content: n.UID},
},
},
},
"Name": notionapi.RichTextProperty{
RichText: []notionapi.RichText{
{
PlainText: person.Name,
Text: ¬ionapi.Text{Content: person.Name},
},
},
},
"Title": notionapi.RichTextProperty{
RichText: []notionapi.RichText{
{
PlainText: person.Title,
Text: ¬ionapi.Text{Content: person.Title},
},
},
},
"Address": notionapi.RichTextProperty{
RichText: []notionapi.RichText{
{
PlainText: person.Address,
Text: ¬ionapi.Text{Content: person.Address},
},
},
},
"Email": notionapi.RichTextProperty{
RichText: []notionapi.RichText{
{
PlainText: person.Email,
Text: ¬ionapi.Text{Content: person.Email},
},
},
},
"Phone": notionapi.RichTextProperty{
RichText: []notionapi.RichText{
{
PlainText: person.Phone,
Text: ¬ionapi.Text{Content: person.Phone},
},
},
},
"Company": notionapi.RichTextProperty{
RichText: []notionapi.RichText{
{
PlainText: person.Company,
Text: ¬ionapi.Text{Content: person.Company},
},
},
},
}
// 創建一個新頁面的請求
pageRequest := ¬ionapi.PageCreateRequest{
Parent: notionapi.Parent{
DatabaseID: notionapi.DatabaseID(n.DatabaseID),
},
Properties: properties,
}
// 調用 Notion API 來創建新頁面
_, err := client.Page.Create(context.Background(), pageRequest)
if err != nil {
log.Println("Error creating page:", err)
return err
}
log.Println("Page added successfully:", n.UID, person)
return nil
}
// createEntryFromPage creates a Person from a page.
func (n *NotionDB) createEntryFromPage(page *notionapi.Page) Person {
entry := Person{}
entry.Name = n.getPropertyValue(page, "Name")
entry.Title = n.getPropertyValue(page, "Title")
entry.Address = n.getPropertyValue(page, "Address")
entry.Email = n.getPropertyValue(page, "Email")
entry.Phone = n.getPropertyValue(page, "Phone")
entry.Company = n.getPropertyValue(page, "Company")
return entry
}
// getPropertyValue gets the plain text value of a property from a page.
func (n *NotionDB) getPropertyValue(page *notionapi.Page, property string) string {
if prop, ok := page.Properties[property].(*notionapi.RichTextProperty); ok && len(prop.RichText) > 0 {
return prop.RichText[0].PlainText
}
return ""
}
// QueryDatabaseByName 根據提供的名稱查詢 Notion 資料庫。
func (n *NotionDB) QueryDatabaseByName(name string) ([]Person, error) {
return n.QueryDatabase("Name", name)
}
// QueryDatabaseContainsByName 根據提供的名稱查詢 Notion 資料庫。
func (n *NotionDB) QueryDatabaseContainsByName(name string) ([]Person, error) {
return n.QueryContainsDatabase("Name", name)
}
// QueryDatabaseContainsByName 根據提供的名稱查詢 Notion 資料庫。
func (n *NotionDB) QueryDatabaseContainsByTitle(name string) ([]Person, error) {
return n.QueryContainsDatabase("Title", name)
}
// QueryDatabaseContains 根據提供的名稱查詢 Notion 資料庫。
func (n *NotionDB) QueryDatabaseContains(query string) ([]Person, error) {
// 初始化一個空的結果集
var combinedResult []Person
// 進行名稱查詢
nameResult, err := n.QueryDatabaseContainsByName(query)
log.Println("QueryDatabaseContainsByName", nameResult, err)
if err != nil {
return nil, err
}
combinedResult = append(combinedResult, nameResult...)
// 進行電子郵件查詢
emailResult, err := n.QueryDatabaseContainsByEmail(query)
log.Println("QueryDatabaseContainsByEmail", emailResult, err)
if err != nil {
return nil, err
}
combinedResult = append(combinedResult, emailResult...)
// 進行標題查詢
titleResult, err := n.QueryDatabaseContainsByTitle(query)
log.Println("QueryDatabaseContainsByTitle", titleResult, err)
if err != nil {
return nil, err
}
combinedResult = append(combinedResult, titleResult...)
// 返回結合的結果
return combinedResult, nil
}