Files
memoscli/examples/send_example.py

55 lines
1.6 KiB
Python
Raw 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.

"""
使用示例发送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()