Files

88 lines
2.1 KiB
Python
Raw Permalink Normal View History

2026-08-03 18:42:47 +08:00
#!/usr/bin/env python3
"""
获取微信ID并发送测试消息
"""
import json
import subprocess
import select
def call_mcp_tool(tool_name: str, params: dict):
"""调用 MCP 工具"""
mcp_request = {
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"name": tool_name,
"arguments": params
},
"id": 1
}
proc = subprocess.Popen(
["bunx", "mcp-wechat-server"],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True
)
request_json = json.dumps(mcp_request) + "\n"
proc.stdin.write(request_json)
proc.stdin.flush()
proc.stdin.close()
ready, _, _ = select.select([proc.stdout], [], [], 30)
if ready:
response_line = proc.stdout.readline()
proc.terminate()
try:
response = json.loads(response_line)
if "result" in response:
result = response["result"]
if "content" in result and len(result["content"]) > 0:
return result["content"][0].get("text", "")
except:
pass
else:
proc.terminate()
return None
def main():
print("="*60)
print("🔍 获取微信联系人列表")
print("="*60)
# 尝试获取联系人
result = call_mcp_tool("get_contacts", {})
if result:
print(f"\n✅ 获取成功:")
print(result)
else:
print("\n❌ 无法获取联系人")
print("\n尝试直接发送消息到指定微信号...")
# 直接尝试发送消息
wechat_id = "w402468w"
message = "🧪 这是来自 bbav20311 的测试消息!如果收到,说明微信集成成功!"
print(f"\n💬 发送消息到 {wechat_id}...")
result = call_mcp_tool("send_text_message", {
"user_id": wechat_id,
"message": message
})
if result:
print(f"✅ 发送结果: {result}")
else:
print("❌ 发送失败")
if __name__ == "__main__":
main()