最近发现了一个新的python微信机器人SDK—ntchat,用起来比wechaty感觉要简单一点,项目比较新,感兴趣的同学可以尝试一下
项目地址:ntchat
前置条件#
- 下载支持的微信版本
- 安装ntchat:pip install ntchat
- 在电脑上使用第一步下载的微信登陆账号
正式开始#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
| # -*- coding: utf-8 -*-
import sys
import ntchat
wechat = ntchat.WeChat()
wechat.open(smart=True)
@wechat.msg_register(ntchat.MT_RECV_TEXT_MSG)
def on_recv_text_msg(wechat: ntchat.WeChat, message):
data = message["data"]
msg = data["msg"]
from_wxid = data["from_wxid"]
self_wxid = wechat.get_login_info()["wxid"]
if from_wxid == self_wxid:
return
if msg == "hello":
wechat.send_text(to_wxid=from_wxid, content=f"world")
try:
while True:
pass
except KeyboardInterrupt:
ntchat.exit_()
sys.exit()
|
- python main.py 运行文件,向登陆的微信号发送hello,正常情况下自动回复world即成功
代码简单说明#
- try-except部分是为了使得py文件持续运行,如果有其他方法可以省略这部分代码