test.py 3.7 KB
Newer Older
L
ljc545w 已提交
1 2 3 4 5 6 7 8 9 10 11
# -*- coding: utf-8 -*-
"""
Created on Sat Apr 16 14:06:24 2022

@author: lijinchao-002
"""
import time
from wxRobot import WeChatRobot

# 一个示例回调,将收到的文本消息转发给filehelper
def ReceiveMessageCallBack(robot,message):
L
ljc545w 已提交
12 13
    chatwith = message.get('sendto') or message.get('from')
    if message['type'] == 1 and not message['isSendMessage'] and chatwith != 'filehelper':
L
ljc545w 已提交
14
        robot.robot.CSendText('filehelper',message['message'])
L
ljc545w 已提交
15 16 17 18
    chatwith = message.get('sendto') or message.get('from')
    wxSender = robot.GetWxUserInfo(chatwith)
    sender = wxSender['wxNickName'] if wxSender['wxNickName'] != 'null' else chatwith
    print("来自 {}\n".format(sender),message)
L
ljc545w 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
    
def test_SendText():
    import os
    path = os.path.split(os.path.realpath(__file__))[0]
    # image full path
    imgpath = os.path.join(path,'test\\测试图片.png')
    # file full path
    filepath = os.path.join(path,'test\\测试文件')
    wx = WeChatRobot()
    wx.StartService()
    myinfo = wx.GetSelfInfo()
    chatwith = wx.GetFriendByWxNickName("文件传输助手")
    session = wx.GetChatSession(chatwith.get('wxid'))
    filehelper = wx.GetWxUserInfo(chatwith.get('wxid'))
    session.SendText('个人信息:{}'.format(str(myinfo.get('wxNickName'))))
    session.SendText('好友信息:{}'.format(str(filehelper.get('wxNickName'))))
    if os.path.exists(imgpath): session.SendImage(imgpath)
    if os.path.exists(filepath): session.SendFile(filepath)
    session.SendArticle("天气预报","点击查看","http://www.baidu.com")
    shared = wx.GetFriendByWxNickName("码农翻身")
    if shared: session.SendCard(shared.get('wxid'),shared.get('wxNickName'))
    wx.StopService()
    
def test_FriendStatus():
    f = open('Friendstatus.txt','wt',encoding = 'utf-8')
    wx = WeChatRobot()
    wx.StartService()
    FriendList = wx.GetFriendList()
    wx.CheckFriendStatusInit()
    index = "\t".join(['微信号','昵称','备注','状态','\n'])
    f.writelines(index)
    for Friend in FriendList:
        result = '\t'.join(
            [Friend.get('wxNumber'),Friend.get('wxNickName'),Friend.get('wxRemark'),
              wx.CheckFriendStatus(Friend.get('wxid'))])
        print(result)
        result += '\n'
        f.writelines(result)
        time.sleep(1)
        break
    f.close()
    wx.StopService()
    
def test_ReceiveMessage():
    wx = WeChatRobot()
    wx.StartService()
L
ljc545w 已提交
65
    wx.robot.CStartReceiveMessage()
L
ljc545w 已提交
66 67 68 69 70 71
    wx.StartReceiveMessage(CallBackFunc = ReceiveMessageCallBack)
    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        pass
L
ljc545w 已提交
72
    wx.StopService()
L
ljc545w 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
    
def test_ExecuteSQL():
    wx = WeChatRobot()
    wx.StartService()
    try:
        dbs = wx.GetDbHandles()
        dbname = 'MicroMsg.db'
        handle = dbs.get(dbname).get('Handle')
        sql = 'select a.UserName as `wxID`,a.Alias as `微信号`,a.EncryptUserName as `V3数据`,\
               a.Type as `联系人类型`,a.VerifyFlag as `添加方式`,a.Remark as `备注`,a.NickName as `昵称`,b.bigHeadImgUrl as `头像` \
               from Contact a inner join ContactHeadImgUrl b where a.UserName=b.usrName and a.Type=3 limit 10'
        result = wx.ExecuteSQL(handle,sql)
        print(result)
    except:
        pass
    wx.StopService()
    
def test_BackupDb():
    wx = WeChatRobot()
    wx.StartService()
    try:
        dbs = wx.GetDbHandles()
        dbname = 'MicroMsg.db'
        handle = dbs.get(dbname).get('Handle')
        rc = wx.BackupSQLiteDB(handle,'D:\\WeChatBackup\\{}'.format(dbname))
        print(rc)
    except:
        pass
    wx.StopService()

if __name__ == '__main__':
L
ljc545w 已提交
104 105 106
    wx = WeChatRobot()
    print(wx.GetWeChatVer())
    wx.StopService()