Python 操作微信,ntchat库

       2022/11/10更新: 删库了,具体情况未知


 发现一个三方库:ntchat,可以实现对微信的操作,有微信版本限制(WeChatSetup3.6.0.18)。

        官方介绍:ntchat-GitHub

  • 基于pc微信的api接口, 类似itchat项目
  • 支持收发文本、群@、名片、图片、文件、视频、链接卡片等
  • 支持好友和群管理

        

向传输助手发送信息

# -*- coding: utf-8 -*-
import sys
import ntchat

wechat = ntchat.WeChat()

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

# 等待登录
wechat.wait_login()

# 向文件助手发送一条消息
wechat.send_text(to_wxid="filehelper", content="hello, filehelper")

try:
    while True:
        pass
except KeyboardInterrupt:
    ntchat.exit_()
    sys.exit()

获取联系人和群列表

# -*- coding: utf-8 -*-
import sys
import ntchat

wechat = ntchat.WeChat()

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

# 等待登录
wechat.wait_login()

# 获取联系人列表并输出
contacts = wechat.get_contacts()

print("联系人列表: ")
print(contacts)

rooms = wechat.get_rooms()
print("群列表: ")
print(rooms)


try:
    while True:
        pass
except KeyboardInterrupt:
    ntchat.exit_()
    sys.exit()

  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
对于Python微信知识自动答复,你可以使用itchat来实现。itchat是一个基于微信网页版的Python,可以实现登录微信、发送消息、接收消息等功能。 首先,你需要安装itchat,可以使用以下命令进行安装: ``` pip install itchat ``` 然后,你可以使用以下示例代码来实现微信知识自动答复: ```python import itchat @itchat.msg_register(itchat.content.TEXT) def auto_reply(msg): # 获取收到的消息内容 message = msg['Text'] # 在这里根据收到的消息内容进行逻辑判断和回复 reply = "" if message == "你好": reply = "你好,有什么问题可以问我哦!" elif message == "天气": reply = "今天天气晴朗,温度28°C。" else: reply = "抱歉,我暂时无法回答这个问题。" # 发送回复消息 itchat.send(reply, toUserName=msg['FromUserName']) # 登录微信并启动自动回复 itchat.auto_login() itchat.run() ``` 以上示例代码中,通过定义一个`auto_reply`函数作为消息注册回调函数,在收到文本消息时进行自动回复。你可以根据具体需求,在`auto_reply`函数中添加更多的逻辑判断和回复内容。 最后,使用`itchat.auto_login()`进行微信登录,并通过`itchat.run()`启动自动回复功能。 请注意,该示例代码只是简单的示范,实际应用中可能需要更复杂的逻辑判断和回复内容。另外,使用itchat需要保持电脑处于运行状态,并且登录的微信账号不能同时在其他设备上登录。 希望能帮到你!如有更多问题,请继续提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值