{"id":13434609,"url":"https://github.com/littlecodersh/ItChat","last_synced_at":"2025-03-17T19:31:14.138Z","repository":{"id":37502685,"uuid":"49935814","full_name":"littlecodersh/ItChat","owner":"littlecodersh","description":"A complete and graceful API for Wechat. 微信个人号接口、微信机器人及命令行微信，三十行即可自定义个人号机器人。","archived":false,"fork":false,"pushed_at":"2023-09-28T07:46:58.000Z","size":955,"stargazers_count":25502,"open_issues_count":289,"forks_count":5632,"subscribers_count":884,"default_branch":"master","last_synced_at":"2024-10-14T09:42:52.312Z","etag":null,"topics":["api","itchat","robot","wechat"],"latest_commit_sha":null,"homepage":"http://itchat.readthedocs.io","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/littlecodersh.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2016-01-19T07:49:48.000Z","updated_at":"2024-10-14T06:58:08.000Z","dependencies_parsed_at":"2022-07-12T15:50:51.221Z","dependency_job_id":"1c4fd9ab-539d-4723-8cef-5db61fba2a54","html_url":"https://github.com/littlecodersh/ItChat","commit_stats":{"total_commits":187,"total_committers":18,"mean_commits":10.38888888888889,"dds":0.1657754010695187,"last_synced_commit":"d5ce5db32ca15cef8eefa548a438a9fcc4502a6d"},"previous_names":[],"tags_count":34,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/littlecodersh%2FItChat","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/littlecodersh%2FItChat/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/littlecodersh%2FItChat/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/littlecodersh%2FItChat/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/littlecodersh","download_url":"https://codeload.github.com/littlecodersh/ItChat/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221699022,"owners_count":16865968,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api","itchat","robot","wechat"],"created_at":"2024-07-31T03:00:18.808Z","updated_at":"2024-10-27T15:31:30.438Z","avatar_url":"https://github.com/littlecodersh.png","language":"Python","funding_links":[],"categories":["WeChat","Uncategorized","Python","Python (144)","网络服务","Wechat","工具集"],"sub_categories":["Utility","Uncategorized","网络服务_其他","代码工具-py"],"readme":"# itchat\n\n[![Gitter][gitter-picture]][gitter] ![py27][py27] ![py35][py35] [English version][english-version]\n\nitchat是一个开源的微信个人号接口，使用python调用微信从未如此简单。\n\n使用不到三十行的代码，你就可以完成一个能够处理所有信息的微信机器人。\n\n当然，该api的使用远不止一个机器人，更多的功能等着你来发现，比如[这些][tutorial2]。\n\n该接口与公众号接口[itchatmp][itchatmp]共享类似的操作方式，学习一次掌握两个工具。\n\n如今微信已经成为了个人社交的很大一部分，希望这个项目能够帮助你扩展你的个人的微信号、方便自己的生活。\n\n## 安装\n\n可以通过本命令安装itchat：\n\n```python\npip install itchat\n```\n\n## 简单入门实例\n\n有了itchat，如果你想要给文件传输助手发一条信息，只需要这样：\n\n```python\nimport itchat\n\nitchat.auto_login()\n\nitchat.send('Hello, filehelper', toUserName='filehelper')\n```\n\n如果你想要回复发给自己的文本消息，只需要这样：\n\n```python\nimport itchat\n\n@itchat.msg_register(itchat.content.TEXT)\ndef text_reply(msg):\n    return msg.text\n\nitchat.auto_login()\nitchat.run()\n```\n\n一些进阶应用可以在下面的开源机器人的源码和进阶应用中看到，或者你也可以阅览[文档][document]。\n\n## 试一试\n\n这是一个基于这一项目的[开源小机器人][robot-source-code]，百闻不如一见，有兴趣可以尝试一下。\n\n由于好友数量实在增长过快，自动通过好友验证的功能演示暂时关闭。\n\n![QRCode][robot-qr]\n\n## 截屏\n\n![file-autoreply][robot-demo-file] ![login-page][robot-demo-login]\n\n## 进阶应用\n\n### 特殊的字典使用方式\n\n通过打印itchat的用户以及注册消息的参数，可以发现这些值都是字典。\n\n但实际上itchat精心构造了相应的消息、用户、群聊、公众号类。\n\n其所有的键值都可以通过这一方式访问：\n\n```python\n@itchat.msg_register(TEXT)\ndef _(msg):\n    # equals to print(msg['FromUserName'])\n    print(msg.fromUserName)\n```\n\n属性名为键值首字母小写后的内容。\n\n```python\nauthor = itchat.search_friends(nickName='LittleCoder')[0]\nauthor.send('greeting, littlecoder!')\n```\n\n### 各类型消息的注册\n\n通过如下代码，微信已经可以就日常的各种信息进行获取与回复。\n\n```python\nimport itchat, time\nfrom itchat.content import *\n\n@itchat.msg_register([TEXT, MAP, CARD, NOTE, SHARING])\ndef text_reply(msg):\n    msg.user.send('%s: %s' % (msg.type, msg.text))\n\n@itchat.msg_register([PICTURE, RECORDING, ATTACHMENT, VIDEO])\ndef download_files(msg):\n    msg.download(msg.fileName)\n    typeSymbol = {\n        PICTURE: 'img',\n        VIDEO: 'vid', }.get(msg.type, 'fil')\n    return '@%s@%s' % (typeSymbol, msg.fileName)\n\n@itchat.msg_register(FRIENDS)\ndef add_friend(msg):\n    msg.user.verify()\n    msg.user.send('Nice to meet you!')\n\n@itchat.msg_register(TEXT, isGroupChat=True)\ndef text_reply(msg):\n    if msg.isAt:\n        msg.user.send(u'@%s\\u2005I received: %s' % (\n            msg.actualNickName, msg.text))\n\nitchat.auto_login(True)\nitchat.run(True)\n```\n\n### 命令行二维码\n\n通过以下命令可以在登陆的时候使用命令行显示二维码：\n\n```python\nitchat.auto_login(enableCmdQR=True)\n```\n\n部分系统可能字幅宽度有出入，可以通过将enableCmdQR赋值为特定的倍数进行调整：\n\n```python\n# 如部分的linux系统，块字符的宽度为一个字符（正常应为两字符），故赋值为2\nitchat.auto_login(enableCmdQR=2)\n```\n\n默认控制台背景色为暗色（黑色），若背景色为浅色（白色），可以将enableCmdQR赋值为负值：\n\n```python\nitchat.auto_login(enableCmdQR=-1)\n```\n\n### 退出程序后暂存登陆状态\n\n通过如下命令登陆，即使程序关闭，一定时间内重新开启也可以不用重新扫码。\n\n```python\nitchat.auto_login(hotReload=True)\n```\n\n### 用户搜索\n\n使用`search_friends`方法可以搜索用户，有四种搜索方式：\n1. 仅获取自己的用户信息\n2. 获取特定`UserName`的用户信息\n3. 获取备注、微信号、昵称中的任何一项等于`name`键值的用户\n4. 获取备注、微信号、昵称分别等于相应键值的用户\n\n其中三、四项可以一同使用，下面是示例程序：\n\n```python\n# 获取自己的用户信息，返回自己的属性字典\nitchat.search_friends()\n# 获取特定UserName的用户信息\nitchat.search_friends(userName='@abcdefg1234567')\n# 获取任何一项等于name键值的用户\nitchat.search_friends(name='littlecodersh')\n# 获取分别对应相应键值的用户\nitchat.search_friends(wechatAccount='littlecodersh')\n# 三、四项功能可以一同使用\nitchat.search_friends(name='LittleCoder机器人', wechatAccount='littlecodersh')\n```\n\n关于公众号、群聊的获取与搜索在文档中有更加详细的介绍。\n\n### 附件的下载与发送\n\nitchat的附件下载方法存储在msg的Text键中。\n\n发送的文件的文件名（图片给出的默认文件名）都存储在msg的FileName键中。\n\n下载方法接受一个可用的位置参数（包括文件名），并将文件相应的存储。\n\n```python\n@itchat.msg_register([PICTURE, RECORDING, ATTACHMENT, VIDEO])\ndef download_files(msg):\n    msg.download(msg.fileName)\n    itchat.send('@%s@%s' % (\n        'img' if msg['Type'] == 'Picture' else 'fil', msg['FileName']),\n        msg['FromUserName'])\n    return '%s received' % msg['Type']\n```\n\n如果你不需要下载到本地，仅想要读取二进制串进行进一步处理可以不传入参数，方法将会返回图片的二进制串。\n\n```python\n@itchat.msg_register([PICTURE, RECORDING, ATTACHMENT, VIDEO])\ndef download_files(msg):\n    with open(msg.fileName, 'wb') as f:\n        f.write(msg.download())\n```\n\n### 用户多开\n\n使用如下命令可以完成多开的操作：\n\n```python\nimport itchat\n\nnewInstance = itchat.new_instance()\nnewInstance.auto_login(hotReload=True, statusStorageDir='newInstance.pkl')\n\n@newInstance.msg_register(itchat.content.TEXT)\ndef reply(msg):\n    return msg.text\n\nnewInstance.run()\n```\n\n### 退出及登陆完成后调用特定方法\n\n登陆完成后的方法需要赋值在`loginCallback`中。\n\n而退出后的方法需要赋值在`exitCallback`中。\n\n```python\nimport time\n\nimport itchat\n\ndef lc():\n    print('finish login')\ndef ec():\n    print('exit')\n\nitchat.auto_login(loginCallback=lc, exitCallback=ec)\ntime.sleep(3)\nitchat.logout()\n```\n\n若不设置loginCallback的值，则将会自动删除二维码图片并清空命令行显示。\n\n## 常见问题与解答\n\nQ: 如何通过这个包将自己的微信号变为控制器？\n\nA: 有两种方式：发送、接受自己UserName的消息；发送接收文件传输助手（filehelper）的消息\n\nQ: 为什么我发送信息的时候部分信息没有成功发出来？\n\nA: 有些账号是天生无法给自己的账号发送信息的，建议使用`filehelper`代替。\n\n## 作者\n\n[LittleCoder][littlecodersh]: 构架及维护Python2 Python3版本。\n\n[tempdban][tempdban]: 协议、构架及日常维护。\n\n[Chyroc][Chyroc]: 完成第一版本的Python3构架。\n\n## 类似项目\n\n[youfou/wxpy][youfou-wxpy]: 优秀的api包装和配套插件，微信机器人/优雅的微信个人号API\n\n[liuwons/wxBot][liuwons-wxBot]: 类似的基于Python的微信机器人\n\n[zixia/wechaty][zixia-wechaty]: 基于Javascript(ES6)的微信个人账号机器人NodeJS框架/库\n\n[sjdy521/Mojo-Weixin][Mojo-Weixin]: 使用Perl语言编写的微信客户端框架，可通过插件提供基于HTTP协议的api接口供其他语言调用\n\n[HanSon/vbot][HanSon-vbot]: 基于PHP7的微信个人号机器人，通过实现匿名函数可以方便地实现各种自定义的功能\n\n[yaphone/itchat4j][yaphone-itchat4j]: 用Java扩展个人微信号的能力\n\n[kanjielu/jeeves][kanjielu-jeeves]: 使用springboot开发的微信机器人\n\n## 问题和建议\n\n如果有什么问题或者建议都可以在这个[Issue][issue#1]和我讨论\n\n或者也可以在gitter上交流：[![Gitter][gitter-picture]][gitter]\n\n当然也可以加入我们新建的QQ群讨论：549762872, 205872856\n\n[gitter-picture]: https://badges.gitter.im/littlecodersh/ItChat.svg\n[gitter]: https://gitter.im/littlecodersh/ItChat?utm_source=badge\u0026utm_medium=badge\u0026utm_campaign=pr-badge\n[py27]: https://img.shields.io/badge/python-2.7-ff69b4.svg\n[py35]: https://img.shields.io/badge/python-3.5-red.svg\n[english-version]: https://github.com/littlecodersh/ItChat/blob/master/README_EN.md\n[itchatmp]: https://github.com/littlecodersh/itchatmp\n[document]: https://itchat.readthedocs.org/zh/latest/\n[tutorial2]: http://python.jobbole.com/86532/\n[robot-source-code]: https://gist.github.com/littlecodersh/ec8ddab12364323c97d4e36459174f0d\n[robot-qr]: http://7xrip4.com1.z0.glb.clouddn.com/ItChat%2FQRCode2.jpg?imageView/2/w/400/\n[robot-demo-file]: http://7xrip4.com1.z0.glb.clouddn.com/ItChat%2FScreenshots%2F%E5%BE%AE%E4%BF%A1%E8%8E%B7%E5%8F%96%E6%96%87%E4%BB%B6%E5%9B%BE%E7%89%87.png?imageView/2/w/300/\n[robot-demo-login]: http://7xrip4.com1.z0.glb.clouddn.com/ItChat%2FScreenshots%2F%E7%99%BB%E5%BD%95%E7%95%8C%E9%9D%A2%E6%88%AA%E5%9B%BE.jpg?imageView/2/w/450/\n[littlecodersh]: https://github.com/littlecodersh\n[tempdban]: https://github.com/tempdban\n[Chyroc]: https://github.com/Chyroc\n[youfou-wxpy]: https://github.com/youfou/wxpy\n[liuwons-wxBot]: https://github.com/liuwons/wxBot\n[zixia-wechaty]: https://github.com/zixia/wechaty\n[Mojo-Weixin]: https://github.com/sjdy521/Mojo-Weixin\n[HanSon-vbot]: https://github.com/hanson/vbot\n[yaphone-itchat4j]: https://github.com/yaphone/itchat4j\n[kanjielu-jeeves]: https://github.com/kanjielu/jeeves\n[issue#1]: https://github.com/littlecodersh/ItChat/issues/1\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flittlecodersh%2FItChat","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flittlecodersh%2FItChat","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flittlecodersh%2FItChat/lists"}