2021-02-12 23:21:57 +08:00
|
|
|
|
#!/usr/bin/env python
|
|
|
|
|
# -*- coding: UTF-8 -*-
|
|
|
|
|
|
|
|
|
|
import time
|
|
|
|
|
import wcferry as sdk
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2022-08-20 20:48:04 +08:00
|
|
|
|
help(sdk) # 查看SDK支持的方法和属性
|
2021-02-12 23:21:57 +08:00
|
|
|
|
|
|
|
|
|
# 初始化SDK,如果成功,返回0;否则失败
|
|
|
|
|
status = sdk.WxInitSDK()
|
|
|
|
|
if status != 0:
|
|
|
|
|
print("初始化失败")
|
|
|
|
|
exit(-1)
|
|
|
|
|
|
|
|
|
|
print("初始化成功")
|
2022-08-20 15:15:04 +08:00
|
|
|
|
WxMsgTypes = sdk.WxGetMsgTypes() # 获取消息类型
|
|
|
|
|
print(WxMsgTypes) # 查看消息类型
|
2021-02-12 23:21:57 +08:00
|
|
|
|
|
2021-08-28 08:33:24 +08:00
|
|
|
|
time.sleep(2)
|
|
|
|
|
print("打印通讯录......")
|
|
|
|
|
contacts = sdk.WxGetContacts()
|
|
|
|
|
for k, v in contacts.items():
|
|
|
|
|
print(k, v.wxCode, v.wxName, v.wxCountry, v.wxProvince, v.wxCity, v.wxGender)
|
|
|
|
|
|
2021-02-12 23:21:57 +08:00
|
|
|
|
time.sleep(2)
|
|
|
|
|
print("发送文本消息......")
|
2022-08-20 20:48:04 +08:00
|
|
|
|
sdk.WxSendTextMsg("filehelper", "message from WeChatFerry...") # 往文件传输助手发消息
|
|
|
|
|
# sdk.WxSendTextMsg("xxxx@chatroom", "message from WeChatFerry...") # 往群里发消息(需要改成正确的 ID,下同)
|
|
|
|
|
# sdk.WxSendTextMsg("xxxx@chatroom", "message from WeChatFerry... @ ", "wxid_xxxxxxxxxxxx") # 往群里发消息,@某人
|
|
|
|
|
# sdk.WxSendTextMsg("xxxx@chatroom", "message from WeChatFerry... @ ", "notify@all") # 往群里发消息,@所有人
|
2021-02-12 23:21:57 +08:00
|
|
|
|
|
2021-08-22 21:57:16 +08:00
|
|
|
|
time.sleep(2)
|
|
|
|
|
print("发送图片消息......")
|
|
|
|
|
sdk.WxSendImageMsg("filehelper", "test.jpg")
|
|
|
|
|
|
2022-08-20 20:48:04 +08:00
|
|
|
|
dbs = sdk.WxGetDbNames()
|
|
|
|
|
for db in dbs:
|
|
|
|
|
print(db)
|
|
|
|
|
|
|
|
|
|
tables = sdk.WxGetDbTables(dbs[0])
|
|
|
|
|
for t in tables:
|
|
|
|
|
print(f"{t.table}\n{t.sql}\n\n")
|
|
|
|
|
|
2021-02-12 23:21:57 +08:00
|
|
|
|
# 接收消息。先定义消息处理回调
|
|
|
|
|
def OnTextMsg(msg: sdk.WxMessage):
|
2021-08-28 08:33:24 +08:00
|
|
|
|
s = "收到"
|
2021-02-12 23:21:57 +08:00
|
|
|
|
if msg.self == 1: # 忽略自己发的消息
|
2021-08-28 08:33:24 +08:00
|
|
|
|
s += f"来自自己的消息"
|
|
|
|
|
print(f"\n{s}")
|
|
|
|
|
|
2021-02-12 23:21:57 +08:00
|
|
|
|
return 0
|
|
|
|
|
|
2022-08-20 20:48:04 +08:00
|
|
|
|
msgType = WxMsgTypes.get(msg.type, '未知类型')
|
2021-08-28 08:33:24 +08:00
|
|
|
|
nickName = contacts.get(msg.wxId, {'wxName': 'NoBody'}).wxName
|
2021-02-12 23:21:57 +08:00
|
|
|
|
if msg.source == 0:
|
2021-08-28 08:33:24 +08:00
|
|
|
|
s += f"来自好友[{nickName}]的{msgType}消息:"
|
2021-02-12 23:21:57 +08:00
|
|
|
|
else:
|
2021-08-28 08:33:24 +08:00
|
|
|
|
groupName = contacts.get(msg.roomId, {'wxName': 'Unknown'}).wxName
|
|
|
|
|
s += f"来自群[{groupName}]的[{nickName}]的{msgType}消息:"
|
2021-02-12 23:21:57 +08:00
|
|
|
|
|
|
|
|
|
s += f"\r\n{msg.content}"
|
|
|
|
|
if msg.type != 0x01:
|
|
|
|
|
s += f"\r\n{msg.xml}"
|
|
|
|
|
|
|
|
|
|
print(f"\n{s}")
|
|
|
|
|
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
print("Message: 接收通知中......")
|
2022-08-20 15:15:04 +08:00
|
|
|
|
sdk.WxEnableRecvMsg(OnTextMsg) # 设置回调,接收消息
|
2021-02-12 23:21:57 +08:00
|
|
|
|
while True:
|
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
main()
|