Files
server-configs/add-self-contact-fixed.py
2026-02-13 22:24:27 +08:00

133 lines
3.6 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
添加自己到Notion联系人数据库修正版
"""
import requests
import json
from datetime import datetime
# Notion API 配置
NOTION_API_KEY = "ntn_c43902219395mirQBetIfYoww1qKCAF14GBRUQeDee29o2"
DATABASE_ID = "2fb105ad-7873-81cf-90cc-dad8f1277ded" # 联系人数据库 ID
# 联系人信息
CONTACT_INFO = {
"name": "自己",
"email": "Yaxing_feng@dgmaorui.com",
"company": "公司邮箱",
"tags": ["自己"],
"notes": "用于接收每日新闻摘要的公司邮箱"
}
def add_contact_to_notion():
"""添加联系人到Notion数据库"""
print("=" * 60)
print("添加联系人到Notion")
print("=" * 60)
print(f"联系人: {CONTACT_INFO['name']}")
print(f"邮箱: {CONTACT_INFO['email']}")
print(f"公司: {CONTACT_INFO['company']}")
print("=" * 60)
# Notion API 端点
url = "https://api.notion.com/v1/pages"
# 请求头
headers = {
"Authorization": f"Bearer {NOTION_API_KEY}",
"Content-Type": "application/json",
"Notion-Version": "2022-06-28"
}
# 请求体(使用正确的属性名称)
payload = {
"parent": {
"database_id": DATABASE_ID
},
"properties": {
"姓名": {
"title": [
{
"text": {
"content": CONTACT_INFO["name"]
}
}
]
},
"邮箱": {
"email": CONTACT_INFO["email"]
},
"公司": {
"rich_text": [
{
"text": {
"content": CONTACT_INFO["company"]
}
}
]
},
"标签": {
"multi_select": [
{"name": tag} for tag in CONTACT_INFO["tags"]
]
},
"备注": {
"rich_text": [
{
"text": {
"content": CONTACT_INFO["notes"]
}
}
]
}
}
}
try:
# 发送请求
response = requests.post(url, headers=headers, json=payload)
if response.status_code == 200:
result = response.json()
print("\n✅ 联系人添加成功!")
print("=" * 60)
print(f"联系人ID: {result['id']}")
print(f"联系人名称: {CONTACT_INFO['name']}")
print(f"邮箱: {CONTACT_INFO['email']}")
print(f"公司: {CONTACT_INFO['company']}")
print("=" * 60)
return True
else:
print(f"\n❌ 添加失败")
print(f"状态码: {response.status_code}")
print(f"错误信息: {response.text}")
return False
except Exception as e:
print(f"\n❌ 发生错误: {e}")
return False
def main():
"""主函数"""
print("开始添加联系人到Notion...")
print()
success = add_contact_to_notion()
if success:
print("\n💡 提示:")
print("1. 联系人已添加到Notion数据库")
print("2. 标签包含'自己'")
print("3. 公司字段标记为'公司邮箱'")
else:
print("\n🔧 解决方案:")
print("1. 检查Notion API密钥")
print("2. 检查数据库ID是否正确")
print("3. 检查网络连接")
if __name__ == "__main__":
main()