first commit: New project
This commit is contained in:
55
examples/send_example.py
Normal file
55
examples/send_example.py
Normal file
@@ -0,0 +1,55 @@
|
||||
"""
|
||||
使用示例:发送Memo到Memos服务器
|
||||
|
||||
在使用前,请确保在 .env 文件中配置以下环境变量:
|
||||
MEMOS_BASE_URL=<your_memos_base_url>
|
||||
MEMOS_TOKEN=<your_token>
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
from src.send import MemosAPI, send_memo
|
||||
|
||||
def main():
|
||||
"""发送Memo示例"""
|
||||
try:
|
||||
# 方法1:使用便捷函数
|
||||
print("=== 方法1:使用便捷函数 ===")
|
||||
result = send_memo(
|
||||
content="这是通过Memos CLI发送的测试内容",
|
||||
visibility="PRIVATE",
|
||||
tags=["测试", "CLI"]
|
||||
)
|
||||
print(f"Memo发送成功!ID: {result.get('id')}")
|
||||
print(f"内容: {result.get('content')}")
|
||||
print(f"创建时间: {result.get('createdTs')}")
|
||||
|
||||
except Exception as e:
|
||||
print(f"便捷函数发送失败: {e}")
|
||||
|
||||
try:
|
||||
# 方法2:使用API客户端类
|
||||
print("\n=== 方法2:使用API客户端类 ===")
|
||||
api = MemosAPI()
|
||||
|
||||
# 测试连接
|
||||
if api.test_connection():
|
||||
print("成功连接到Memos服务器")
|
||||
|
||||
# 发送Memo
|
||||
result = api.send_memo(
|
||||
content="这是通过Memos API客户端发送的内容",
|
||||
visibility="PUBLIC",
|
||||
tags=["API测试", "Python"]
|
||||
)
|
||||
print(f"Memo发送成功!ID: {result.get('id')}")
|
||||
else:
|
||||
print("无法连接到Memos服务器")
|
||||
|
||||
except Exception as e:
|
||||
print(f"API客户端发送失败: {e}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user