找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

查看: 3380|回复: 0

[代码与实例] 【开源项目推荐】NtChat 基于pc微信的Python机器人接口,提...

1

主题

1

帖子

1

积分

贫民

积分
1
evilbeast 发表于 2022-9-1 18:30:57 | 显示全部楼层 |阅读模式

项目地址:  https://github.com/smallevilbeast/ntchat


介绍
  • 基于pc微信的api接口, 类似itchat项目
  • 支持收发文本、群@、名片、图片、文件、视频、链接卡片等
  • 支持好友和群管理
安装
  1. pip install ntchat
复制代码


国内源安装
  1. pip install -i https://pypi.tuna.tsinghua.edu.cn/simple ntchat
复制代码
简单入门实例
有了ntchat,如果你想要给文件传输助手发一条信息,只需要这样
  1. # -*- coding: utf-8 -*-
  2. import sys
  3. import ntchat

  4. wechat = ntchat.WeChat()

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

  7. # 等待登录
  8. wechat.wait_login()

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

  11. try:
  12.     while True:
  13.         pass
  14. except KeyboardInterrupt:
  15.     ntchat.exit_()
  16.     sys.exit()
复制代码
获取联系人和群列表
  1. # -*- coding: utf-8 -*-
  2. import sys
  3. import ntchat

  4. wechat = ntchat.WeChat()

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

  7. # 等待登录
  8. wechat.wait_login()

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

  11. print("联系人列表: ")
  12. print(contacts)

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


  16. try:
  17.     while True:
  18.         pass
  19. except KeyboardInterrupt:
  20.     ntchat.exit_()
  21.     sys.exit()
复制代码
监听消息并自动回复
  1. # -*- coding: utf-8 -*-
  2. import sys
  3. import ntchat

  4. wechat = ntchat.WeChat()

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


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

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


  16. try:
  17.     while True:
  18.         pass
  19. except KeyboardInterrupt:
  20.     ntchat.exit_()
  21.     sys.exit()
复制代码
使用fastapi框架实现的web api接口
通过fastapi的swagger在线文档可以很方便的管理NtChat接口




回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表