Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
29c5846
Delete test.py
smallevilbeast Aug 24, 2022
bc17f10
修复不能管理已经打开微信
smallevilbeast Aug 25, 2022
d8ecba2
修改版本号为0.1.2
smallevilbeast Aug 25, 2022
0fc8d21
删除无用图片
smallevilbeast Aug 25, 2022
7e1bf18
修改echo_bot例子不回复群消息
smallevilbeast Aug 25, 2022
c261a9b
修复消息例子
smallevilbeast Aug 25, 2022
0cd080d
新增添加群成员为好友接口
smallevilbeast Aug 27, 2022
594cc56
添加微信进程退出通知
smallevilbeast Aug 27, 2022
9505e69
添加退出例子,发布0.1.4版本
smallevilbeast Aug 27, 2022
708cc9e
新增web接口完整例子
smallevilbeast Aug 29, 2022
1e7d035
格式化代码
smallevilbeast Aug 29, 2022
a95b6ad
修复pyinstaller打包问题,发布0.1.5版本
smallevilbeast Aug 30, 2022
4391ad5
新增退出群接口
smallevilbeast Aug 30, 2022
8fc2592
新增cmd窗口关闭例子
smallevilbeast Aug 30, 2022
a1864a0
更新README
smallevilbeast Aug 30, 2022
61aa8ed
更新README
smallevilbeast Aug 30, 2022
a4be63c
新增常见问题文档
smallevilbeast Aug 30, 2022
1ed2f6b
更新readme
smallevilbeast Aug 30, 2022
f8906a8
更新readme
smallevilbeast Aug 30, 2022
c9179ed
更新readme
smallevilbeast Aug 30, 2022
8fc68a4
去除文件log
smallevilbeast Aug 31, 2022
1f8aa49
发布0.1.6版本
smallevilbeast Aug 31, 2022
3ac176a
更新README
smallevilbeast Aug 31, 2022
de9fee4
FAQ添加WeChatVersionNotMatchError异常说明
smallevilbeast Sep 1, 2022
dd054f1
更新fastapi例子
smallevilbeast Sep 1, 2022
f0f2e44
修复fastapi退出例子
smallevilbeast Sep 4, 2022
0c19723
进程退出消息通知MT_ALL
smallevilbeast Sep 4, 2022
f326669
添加模糊查询联系人和发送xml接口
smallevilbeast Sep 4, 2022
88a6c75
修复fastapi_example例子get_contact_detail请求的返回
smallevilbeast Sep 4, 2022
7b2db44
添加定时发送文本消息例子
smallevilbeast Sep 4, 2022
16369df
去除close_log2.py
smallevilbeast Sep 5, 2022
841b8bf
更新README
smallevilbeast Sep 5, 2022
138026a
更新faq.md
smallevilbeast Sep 5, 2022
254123f
添加获取公众号列表
smallevilbeast Sep 5, 2022
91d1257
search_contacts方法添加模糊搜索参数
smallevilbeast Sep 6, 2022
008e686
添加消息轰炸机示例
smallevilbeast Sep 6, 2022
80bc605
添加数据库查询例子
smallevilbeast Sep 6, 2022
da213b0
更新README
smallevilbeast Sep 7, 2022
b09ce23
更新fastapi_example打包命令
smallevilbeast Sep 7, 2022
e279b6e
修复fastapi_example的get_contacts错误
smallevilbeast Sep 7, 2022
6e30f1f
新增ImportError问题说明
smallevilbeast Sep 8, 2022
eac6a64
更新faq
smallevilbeast Sep 8, 2022
38d34ff
更新faq
smallevilbeast Sep 8, 2022
8db8998
增加拍一拍
smallevilbeast Sep 8, 2022
9a0cc00
修复在回调中调用同步接口返回None的问题
smallevilbeast Sep 9, 2022
7a7e927
更新models
smallevilbeast Sep 9, 2022
d91c807
更新
smallevilbeast Sep 9, 2022
cf69a36
添加发送群@例子
smallevilbeast Sep 10, 2022
5ff92ea
修复python3.6运行bug
smallevilbeast Sep 13, 2022
15b408d
使用绝对路径
smallevilbeast Sep 13, 2022
3f22840
更新fastapi
smallevilbeast Sep 13, 2022
f7ba8fd
修复消息回调函数有异常时程序会退出的问题
smallevilbeast Sep 19, 2022
476e842
fastapi添加修改备注接口
smallevilbeast Sep 20, 2022
99ac6bf
add transmit example
Sep 23, 2022
cd51f46
remove unuse import
Sep 23, 2022
cdaacf1
address comment
Sep 23, 2022
89ea755
Merge pull request #38 from IcePigZDB/add_transmit_example
smallevilbeast Sep 23, 2022
734eab0
更新send_text_ui例子
smallevilbeast Sep 23, 2022
ae45037
增强安全性
smallevilbeast Sep 28, 2022
f7fb727
fastapi例子open接口返回二维码结果
smallevilbeast Sep 28, 2022
38506a0
优化msg_register
smallevilbeast Oct 7, 2022
330f072
优化异常输出
smallevilbeast Oct 9, 2022
90c824d
去除sql_query例子
smallevilbeast Oct 9, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,8 @@ main.spec
build/
config.ini
ntchat/wc/*.pyd
ntchat/wc/*.dat
wheelhouse/
setup_conf.py
setup_conf.py
upload.bat
download/
32 changes: 25 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<h1 align="center">NtChat</h1>
<p align="center">
<a href="https://github.com/smallevilbeast/ntchat/releases"><img src="https://img.shields.io/badge/release-0.1.1-blue.svg?" alt="release"></a>
<a href="https://github.com/smallevilbeast/ntchat/releases"><img src="https://img.shields.io/badge/release-0.1.16-blue.svg?" alt="release"></a>
<a href="https://opensource.org/licenses/MIT"><img src="https://img.shields.io/badge/License-MIT-brightgreen.svg?" alt="License"></a>
</p>

Expand All @@ -14,7 +14,14 @@
- 支持好友和群管理

## 支持的微信版本下载
- [WeChatSetup3.6.0.18.exe](https://webcdn.m.qq.com/spcmgr/download/WeChat3.6.0.18.exe)
- 下载 [WeChatSetup3.6.0.18.exe](https://github.com/tom-snow/wechat-windows-versions/releases/download/v3.6.0.18/WeChatSetup-3.6.0.18.exe)

## 帮助文档
- 查看 [常见问题](docs/FAQ.md)
- 查看 [常用示例](examples)
- 查看 [NtChatHttp接口示例](fastapi_example)
- 加入群聊 [PyXCGUI&NtChat交流群](https://jq.qq.com/?_wv=1027&k=oIXzbTbI)
- 查看 [PyXCGUI项目](https://github.com/smallevilbeast/pyxcgui)

## 安装

Expand All @@ -38,7 +45,7 @@ import ntchat
wechat = ntchat.WeChat()

# 打开pc微信, smart: 是否管理已经登录的微信
wechat.open(smart=False)
wechat.open(smart=True)

# 等待登录
wechat.wait_login()
Expand All @@ -63,7 +70,7 @@ import ntchat
wechat = ntchat.WeChat()

# 打开pc微信, smart: 是否管理已经登录的微信
wechat.open(smart=False)
wechat.open(smart=True)

# 等待登录
wechat.wait_login()
Expand Down Expand Up @@ -97,7 +104,7 @@ import ntchat
wechat = ntchat.WeChat()

# 打开pc微信, smart: 是否管理已经登录的微信
wechat.open(smart=False)
wechat.open(smart=True)


# 注册消息回调
Expand All @@ -120,6 +127,14 @@ except KeyboardInterrupt:
sys.exit()
```

## 使用fastapi框架实现的web api接口

通过fastapi的swagger在线文档可以很方便的管理NtChat接口

[查看fastapi_example例子](./fastapi_example)

![vfazT0.jpg](https://s1.ax1x.com/2022/08/29/vfazT0.jpg)


## 使用pyxcgui界面库实现的简单例子

Expand All @@ -131,7 +146,7 @@ except KeyboardInterrupt:
# -*- coding: utf8 -*-
import xcgui
import ntchat
from xcgui import XApp, XWindow
from xcgui import XApp, XWindow, RunUiThread


class NtChatWindow(XWindow):
Expand All @@ -155,7 +170,9 @@ class NtChatWindow(XWindow):

def on_btn_open_clicked(self, sender, _):
self.wechat_instance = ntchat.WeChat()
self.wechat_instance.open()
self.wechat_instance.open(smart=True)

# 监听所有通知消息
self.wechat_instance.on(ntchat.MT_ALL, self.on_recv_message)

def on_btn_send_clicked(self, sender, _):
Expand All @@ -167,6 +184,7 @@ class NtChatWindow(XWindow):
else:
self.wechat_instance.send_text(self.edit_wxid.getText(), self.edit_content.getText())

@RunUiThread()
def on_recv_message(self, wechat, message):
text = self.edit_log.getText()
text += "\n"
Expand Down
102 changes: 102 additions & 0 deletions docs/FAQ.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
## 1. WeChatVersionNotMatchError异常
如果出现`ntchat.exception.WeChatVersionNotMatchError`异常, 请确认是否安装github上指定的微信版本,如果确认已经安装,还是报错,可以在代码中添加以下代码,跳过微信版本检测
```python
import ntchat
ntchat.set_wechat_exe_path(wechat_version='3.6.0.18')
```
如果还是无法正常使用,但确认已经安装过了3.6.0.18版本可以如下设置
```python
import ntchat

# wechat_exe_path设置成自己3.6.0.18版本的微信的安装路径
ntchat.set_wechat_exe_path(
wechat_exe_path=r"C:\Program Files (x86)\Tencent\WeChat\WeChat.exe",
wechat_version="3.6.0.18")
```

也可以使用注册表修复这个问题,将下面内容保存成WeChatFix.reg, 并双击运行, 如果安装时有修改安装路径,需要修改下面的InstallPath为自己设定的安装路径
```editorconfig
Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\SOFTWARE\Tencent\WeChat]
"Version"=dword:63060012
"InstallPath"="C:\Program Files (x86)\Tencent\WeChat"
```

## 2. `ImportError: cannot import name 'wcprobe' from 'ntchat.wc'`

出现在这个错误的原因是因为你在github下载的源码目录中运行程序,因为wcprobe是根据python版本自动编译生成的,所以源码目录中没有这个文件.

你需要将要`运行的例子文件移动到非源码目录下`,再去运行或打包

## 3. 如何多开

新建多个ntchat.WeChat实例,然后调用open方法:
```python
import ntchat

# 多开3个微信
for i in range(3):
wechat = ntchat.WeChat()
wechat.open(smart=False)
```
更完善的多实例管理查看[fastapi_example例子](./fastapi_example)

## 4. 如何监听输出所有的消息
```python
# 注册监听所有消息回调
@wechat.msg_register(ntchat.MT_ALL)
def on_recv_text_msg(wechat_instance: ntchat.WeChat, message):
print("########################")
print(message)
```
完全例子查看[examples/msg_register_all.py](../examples/msg_register_all.py)

## 5. 如何关闭NtChat的日志

`os.environ['NTCHAT_LOG'] = "ERROR"` 要在`import ntchat`前执行
```python
# -*- coding: utf-8 -*-
import os
import sys
import time
os.environ['NTCHAT_LOG'] = "ERROR"

import ntchat
```

## 6. 如何正常的关闭Cmd窗口

先使用`pip install pywin32` 安装pywin32模块, 然后在代码中添加以下代码, 完整例子查看[examples/cmd_close_event.py](../examples/cmd_close_event.py)
```python
import sys
import ntchat
import win32api

def on_exit(sig, func=None):
ntchat.exit_()
sys.exit()


# 当关闭cmd窗口时
win32api.SetConsoleCtrlHandler(on_exit, True)
```


## 7. pyinstaller打包exe
使用pyinstaller打包NtChat项目,需要添加`--collect-data=ntchat`选项

打包成单个exe程序
```bash
pyinstaller -F --collect-data=ntchat main.py
```

将所有的依赖文件打包到一个目录中
```bash
pyinstaller -y --collect-data=ntchat main.py
```

打包fastapi_example示例,需要添加`--paths=. --collect-data=ntchat`
```bash
pyinstaller -F --paths=. --collect-data=ntchat main.py
```
13 changes: 10 additions & 3 deletions examples/auto_accept_friend_request.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# -*- coding: utf-8 -*-
import sys
import time
import ntchat
import xml.dom.minidom

wechat = ntchat.WeChat()

# 打开pc微信, smart: 是否管理已经登录的微信
wechat.open(smart=False)
wechat.open(smart=True)


# 注册消息回调
Expand All @@ -21,12 +22,18 @@ def on_recv_text_msg(wechat_instance: ntchat.WeChat, message):
scene = dom.documentElement.getAttribute("scene")

# 自动同意好友申请
wechat_instance.accept_friend_request(encryptusername, ticket, int(scene))
ret = wechat_instance.accept_friend_request(encryptusername, ticket, int(scene))

if ret:
# 通过后向他发条消息
wechat_instance.send_text(to_wxid=ret["userName"], content="你好!!!!!")


# 以下是为了让程序不结束,如果有用于PyQt等有主循环消息的框架,可以去除下面代码
try:
while True:
pass
time.sleep(0.5)
except KeyboardInterrupt:
ntchat.exit_()
sys.exit()

43 changes: 43 additions & 0 deletions examples/bomber.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# -*- coding: utf-8 -*-
import os
os.environ['NTCHAT_LOG'] = "ERROR"

import time
import ntchat

wechat = ntchat.WeChat()
wechat.open(smart=True)

print("正在登录微信")
wechat.wait_login()

peer_wxid = None

while True:
contact_remark = input("请输入想发送的联系人备注: ")
contacts = wechat.search_contacts(remark=contact_remark)
if not contacts:
print(f"没有搜索到备注是{contact_remark}的联系人")
else:
print(f"搜索到{len(contacts)}个联系人: ")
print("0. 重新选择")
for i, contact in enumerate(contacts):
print(f"{i+1}. 昵称: {contact['nickname']}, 备注: {contact['remark']}")
seq = int(input("输入上面编号进行选择: "))
if seq != 0:
peer_wxid = contacts[seq-1]["wxid"]
break

content = input("请输入发送的内容: ")
number = int(input("请输入发送的次数: "))

for i in range(1, number+1):
time.sleep(0.1)
print("正在发送第%d遍" % i)
wechat.send_text(to_wxid=peer_wxid, content=content)


ntchat.exit_()



34 changes: 34 additions & 0 deletions examples/close_log.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# -*- coding: utf-8 -*-
import os
import sys
import time
os.environ['NTCHAT_LOG'] = "ERROR"

import ntchat

wechat = ntchat.WeChat()

# 打开pc微信, smart: 是否管理已经登录的微信
wechat.open(smart=True)


# 注册消息回调
@wechat.msg_register(ntchat.MT_RECV_TEXT_MSG)
def on_recv_text_msg(wechat_instance: ntchat.WeChat, message):
data = message["data"]
from_wxid = data["from_wxid"]
self_wxid = wechat_instance.get_login_info()["wxid"]
room_wxid = data["room_wxid"]

# 判断消息不是自己发的并且不是群消息时,回复对方
if from_wxid != self_wxid and not room_wxid:
wechat_instance.send_text(to_wxid=from_wxid, content=f"你发送的消息是: {data['msg']}")


# 以下是为了让程序不结束,如果有用于PyQt等有主循环消息的框架,可以去除下面代码
try:
while True:
time.sleep(0.5)
except KeyboardInterrupt:
ntchat.exit_()
sys.exit()
47 changes: 47 additions & 0 deletions examples/cmd_close_event.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# -*- coding: utf-8 -*-
import sys
import time
import ntchat
try:
import win32api
except ImportError:
print("Error: this example require pywin32, use `pip install pywin32` install")
sys.exit()

wechat = ntchat.WeChat()

# 打开pc微信, smart: 是否管理已经登录的微信
wechat.open(smart=True)


# 注册消息回调
@wechat.msg_register(ntchat.MT_RECV_TEXT_MSG)
def on_recv_text_msg(wechat_instance: ntchat.WeChat, message):
data = message["data"]
from_wxid = data["from_wxid"]
self_wxid = wechat_instance.get_login_info()["wxid"]

# 判断消息不是自己发的,并回复对方
if from_wxid != self_wxid:
wechat_instance.send_text(to_wxid=from_wxid, content=f"你发送的消息是: {data['msg']}")


def exit_application():
ntchat.exit_()
sys.exit()


def on_exit(sig, func=None):
exit_application()


# 当关闭cmd窗口时
win32api.SetConsoleCtrlHandler(on_exit, True)

# 以下是为了让程序不结束,如果有用于PyQt等有主循环消息的框架,可以去除下面代码
try:
while True:
time.sleep(0.5)
# 当Ctrl+C结束程序时
except KeyboardInterrupt:
exit_application()
Loading