ItChat
A complete and graceful API for Wechat. 微信个人号接口、微信机器人及命令行微信,三十行即可自定义个人号机器人。
Top Related Projects
微信机器人 / 可能是最优雅的微信个人号 API ✨✨
Conversational RPA SDK for Chatbot Makers. Join our Discord: https://discord.gg/7q8NBZbQzt
Python Wechaty is a Conversational RPA SDK for Chatbot Makers written in Python
Mac微信功能拓展/微信插件/微信小助手(A plugin for Mac WeChat)
Quick Overview
ItChat is a Python library that provides a simple interface for interacting with WeChat, a popular Chinese messaging app. It allows users to automate WeChat operations, such as sending messages, managing contacts, and handling group chats, through a Python script.
Pros
- Easy to use and implement for WeChat automation
- Supports both personal and official accounts
- Provides a wide range of functionalities, including text, image, and file messaging
- Active community and regular updates
Cons
- Limited to WeChat platform, not applicable for other messaging apps
- May be affected by WeChat's policy changes or security updates
- Documentation is primarily in Chinese, which may be challenging for non-Chinese speakers
- Potential for misuse in spamming or unauthorized automation
Code Examples
- Logging in and sending a text message:
import itchat
itchat.auto_login()
itchat.send('Hello, WeChat!', toUserName='filehelper')
- Handling incoming messages:
@itchat.msg_register(itchat.content.TEXT)
def text_reply(msg):
return f'You said: {msg.text}'
itchat.auto_login(hotReload=True)
itchat.run()
- Sending an image:
@itchat.msg_register(itchat.content.TEXT)
def send_image(msg):
if msg.text == 'image':
itchat.send_image('path/to/image.jpg', toUserName=msg.fromUserName)
itchat.auto_login(hotReload=True)
itchat.run()
Getting Started
To get started with ItChat, follow these steps:
-
Install ItChat using pip:
pip install itchat
-
Import the library and log in:
import itchat itchat.auto_login(hotReload=True)
-
Send a message:
itchat.send('Hello, World!', toUserName='filehelper')
-
Run the script:
itchat.run()
Note: You'll need to scan a QR code with your WeChat app to log in when running the script for the first time.
Competitor Comparisons
微信机器人 / 可能是最优雅的微信个人号 API ✨✨
Pros of wxpy
- More actively maintained with recent updates
- Cleaner and more Pythonic API design
- Better documentation and examples
Cons of wxpy
- Smaller community and fewer contributors
- Less comprehensive feature set compared to ItChat
- May have compatibility issues with some WeChat versions
Code Comparison
ItChat:
import itchat
@itchat.msg_register(itchat.content.TEXT)
def text_reply(msg):
return msg.text
itchat.auto_login()
itchat.run()
wxpy:
from wxpy import *
bot = Bot()
@bot.register()
def reply_message(msg):
return msg.text
bot.join()
Both libraries provide similar functionality for interacting with WeChat, but wxpy offers a more streamlined and intuitive API. ItChat uses decorators for message handling, while wxpy uses a more object-oriented approach with the Bot class.
wxpy's code is generally more concise and easier to read, making it a good choice for developers who prioritize clean code and simplicity. However, ItChat's larger community and more extensive feature set may be beneficial for complex projects or those requiring specific functionalities.
Ultimately, the choice between wxpy and ItChat depends on the project requirements, developer preferences, and the need for specific features or community support.
Conversational RPA SDK for Chatbot Makers. Join our Discord: https://discord.gg/7q8NBZbQzt
Pros of Wechaty
- Multi-platform support (WeChat, WhatsApp, Gitter, etc.)
- Supports multiple programming languages (TypeScript, JavaScript, Python, Go, Java, PHP, C#)
- More active development and larger community
Cons of Wechaty
- Steeper learning curve due to more complex architecture
- Requires more setup and configuration
- Potentially higher resource usage due to its broader scope
Code Comparison
ItChat:
import itchat
@itchat.msg_register(itchat.content.TEXT)
def text_reply(msg):
return msg.text
itchat.auto_login()
itchat.run()
Wechaty:
import { Wechaty } from 'wechaty'
const bot = new Wechaty()
bot.on('message', async msg => {
console.log(`Message: ${msg}`)
})
await bot.start()
Both libraries provide simple ways to create chatbots, but Wechaty's multi-platform nature is evident in its more generic approach. ItChat is more focused on WeChat-specific functionality, making it easier to use for WeChat-only applications. Wechaty's code is more verbose but offers greater flexibility for cross-platform development.
Python Wechaty is a Conversational RPA SDK for Chatbot Makers written in Python
Pros of python-wechaty
- Cross-platform support (Windows, macOS, Linux)
- More active development and community support
- Supports multiple messaging protocols beyond WeChat
Cons of python-wechaty
- Steeper learning curve due to more complex architecture
- Requires more setup and configuration
- Larger codebase and dependencies
Code Comparison
ItChat:
import itchat
@itchat.msg_register(itchat.content.TEXT)
def text_reply(msg):
return msg.text
itchat.auto_login()
itchat.run()
python-wechaty:
from wechaty import Wechaty, Contact
from wechaty_puppet import MessageType
class MyBot(Wechaty):
async def on_message(self, msg: MessageType):
if msg.text() == 'ding':
await msg.say('dong')
bot = MyBot()
bot.start()
Summary
ItChat is simpler and easier to use for basic WeChat automation tasks, while python-wechaty offers more flexibility and features for advanced users. ItChat has a more straightforward API, but python-wechaty provides better cross-platform support and is actively maintained. The choice between the two depends on the specific requirements of your project and your familiarity with more complex bot frameworks.
Mac微信功能拓展/微信插件/微信小助手(A plugin for Mac WeChat)
Pros of WeChatExtension-ForMac
- Specifically designed for macOS, offering a native experience
- Includes additional features like message recall prevention and auto-reply
- Actively maintained with frequent updates
Cons of WeChatExtension-ForMac
- Limited to macOS platform, not cross-platform like ItChat
- Requires installation and modification of the official WeChat client
- May be less suitable for automated bot development compared to ItChat
Code Comparison
WeChatExtension-ForMac (Objective-C):
- (void)hook_methodSignatureForSelector:(SEL)aSelector {
if ([NSStringFromSelector(aSelector) isEqualToString:@"sendLogoutCGIWithCompletion:"]) {
return;
}
[self hook_methodSignatureForSelector:aSelector];
}
ItChat (Python):
def send(self, msg, toUserName=None):
if toUserName is None:
toUserName = self.storageClass.userName
url = '%s/webwxsendmsg' % self.loginInfo['url']
payloads = {
'BaseRequest': self.loginInfo['BaseRequest'],
'Msg': msg.get('Msg', {
'Type': 1,
'Content': msg['Text'],
'FromUserName': self.storageClass.userName,
'ToUserName': toUserName,
'LocalID': int(time.time() * 1e4),
'ClientMsgId': int(time.time() * 1e4),
}),
}
headers = {'ContentType': 'application/json; charset=UTF-8'}
r = self.s.post(url, data=json.dumps(payloads, ensure_ascii=False).encode('utf8'), headers=headers)
return r.json()['BaseResponse']['Ret'] == 0
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
itchat
itchatæ¯ä¸ä¸ªå¼æºç微信个人å·æ¥å£ï¼ä½¿ç¨pythonè°ç¨å¾®ä¿¡ä»æªå¦æ¤ç®åã
使ç¨ä¸å°ä¸åè¡ç代ç ï¼ä½ å°±å¯ä»¥å®æä¸ä¸ªè½å¤å¤çææä¿¡æ¯ç微信æºå¨äººã
å½ç¶ï¼è¯¥apiç使ç¨è¿ä¸æ¢ä¸ä¸ªæºå¨äººï¼æ´å¤çåè½ççä½ æ¥åç°ï¼æ¯å¦è¿äºã
该æ¥å£ä¸å ¬ä¼å·æ¥å£itchatmpå ±äº«ç±»ä¼¼çæä½æ¹å¼ï¼å¦ä¹ ä¸æ¬¡ææ¡ä¸¤ä¸ªå·¥å ·ã
å¦ä»å¾®ä¿¡å·²ç»æ为äºä¸ªäººç¤¾äº¤çå¾å¤§ä¸é¨åï¼å¸æè¿ä¸ªé¡¹ç®è½å¤å¸®å©ä½ æ©å±ä½ ç个人ç微信å·ãæ¹ä¾¿èªå·±ççæ´»ã
å®è£
å¯ä»¥éè¿æ¬å½ä»¤å®è£ itchatï¼
pip install itchat
ç®åå ¥é¨å®ä¾
æäºitchatï¼å¦æä½ æ³è¦ç»æä»¶ä¼ è¾å©æåä¸æ¡ä¿¡æ¯ï¼åªéè¦è¿æ ·ï¼
import itchat
itchat.auto_login()
itchat.send('Hello, filehelper', toUserName='filehelper')
å¦æä½ æ³è¦åå¤åç»èªå·±çææ¬æ¶æ¯ï¼åªéè¦è¿æ ·ï¼
import itchat
@itchat.msg_register(itchat.content.TEXT)
def text_reply(msg):
return msg.text
itchat.auto_login()
itchat.run()
ä¸äºè¿é¶åºç¨å¯ä»¥å¨ä¸é¢çå¼æºæºå¨äººçæºç åè¿é¶åºç¨ä¸çå°ï¼æè ä½ ä¹å¯ä»¥é è§ææ¡£ã
è¯ä¸è¯
è¿æ¯ä¸ä¸ªåºäºè¿ä¸é¡¹ç®çå¼æºå°æºå¨äººï¼ç¾é»ä¸å¦ä¸è§ï¼æå ´è¶£å¯ä»¥å°è¯ä¸ä¸ã
ç±äºå¥½åæ°éå®å¨å¢é¿è¿å¿«ï¼èªå¨éè¿å¥½åéªè¯çåè½æ¼ç¤ºææ¶å ³éã
æªå±
è¿é¶åºç¨
ç¹æ®çåå ¸ä½¿ç¨æ¹å¼
éè¿æå°itchatçç¨æ·ä»¥å注åæ¶æ¯çåæ°ï¼å¯ä»¥åç°è¿äºå¼é½æ¯åå ¸ã
ä½å®é ä¸itchatç²¾å¿æé äºç¸åºçæ¶æ¯ãç¨æ·ã群èãå ¬ä¼å·ç±»ã
å ¶ææçé®å¼é½å¯ä»¥éè¿è¿ä¸æ¹å¼è®¿é®ï¼
@itchat.msg_register(TEXT)
def _(msg):
# equals to print(msg['FromUserName'])
print(msg.fromUserName)
å±æ§å为é®å¼é¦åæ¯å°ååçå 容ã
author = itchat.search_friends(nickName='LittleCoder')[0]
author.send('greeting, littlecoder!')
åç±»åæ¶æ¯ç注å
éè¿å¦ä¸ä»£ç ï¼å¾®ä¿¡å·²ç»å¯ä»¥å°±æ¥å¸¸çåç§ä¿¡æ¯è¿è¡è·åä¸åå¤ã
import itchat, time
from itchat.content import *
@itchat.msg_register([TEXT, MAP, CARD, NOTE, SHARING])
def text_reply(msg):
msg.user.send('%s: %s' % (msg.type, msg.text))
@itchat.msg_register([PICTURE, RECORDING, ATTACHMENT, VIDEO])
def download_files(msg):
msg.download(msg.fileName)
typeSymbol = {
PICTURE: 'img',
VIDEO: 'vid', }.get(msg.type, 'fil')
return '@%s@%s' % (typeSymbol, msg.fileName)
@itchat.msg_register(FRIENDS)
def add_friend(msg):
msg.user.verify()
msg.user.send('Nice to meet you!')
@itchat.msg_register(TEXT, isGroupChat=True)
def text_reply(msg):
if msg.isAt:
msg.user.send(u'@%s\u2005I received: %s' % (
msg.actualNickName, msg.text))
itchat.auto_login(True)
itchat.run(True)
å½ä»¤è¡äºç»´ç
éè¿ä»¥ä¸å½ä»¤å¯ä»¥å¨ç»éçæ¶å使ç¨å½ä»¤è¡æ¾ç¤ºäºç»´ç ï¼
itchat.auto_login(enableCmdQR=True)
é¨åç³»ç»å¯è½åå¹ å®½åº¦æåºå ¥ï¼å¯ä»¥éè¿å°enableCmdQRèµå¼ä¸ºç¹å®çåæ°è¿è¡è°æ´ï¼
# å¦é¨åçlinuxç³»ç»ï¼åå符ç宽度为ä¸ä¸ªå符ï¼æ£å¸¸åºä¸ºä¸¤å符ï¼ï¼æ
èµå¼ä¸º2
itchat.auto_login(enableCmdQR=2)
é»è®¤æ§å¶å°èæ¯è²ä¸ºæè²ï¼é»è²ï¼ï¼è¥èæ¯è²ä¸ºæµ è²ï¼ç½è²ï¼ï¼å¯ä»¥å°enableCmdQRèµå¼ä¸ºè´å¼ï¼
itchat.auto_login(enableCmdQR=-1)
éåºç¨åºåæåç»éç¶æ
éè¿å¦ä¸å½ä»¤ç»éï¼å³ä½¿ç¨åºå ³éï¼ä¸å®æ¶é´å éæ°å¼å¯ä¹å¯ä»¥ä¸ç¨éæ°æ«ç ã
itchat.auto_login(hotReload=True)
ç¨æ·æç´¢
使ç¨search_friends
æ¹æ³å¯ä»¥æç´¢ç¨æ·ï¼æåç§æç´¢æ¹å¼ï¼
- ä» è·åèªå·±çç¨æ·ä¿¡æ¯
- è·åç¹å®
UserName
çç¨æ·ä¿¡æ¯ - è·åå¤æ³¨ã微信å·ãæµç§°ä¸çä»»ä½ä¸é¡¹çäº
name
é®å¼çç¨æ· - è·åå¤æ³¨ã微信å·ãæµç§°åå«çäºç¸åºé®å¼çç¨æ·
å ¶ä¸ä¸ãå项å¯ä»¥ä¸å使ç¨ï¼ä¸é¢æ¯ç¤ºä¾ç¨åºï¼
# è·åèªå·±çç¨æ·ä¿¡æ¯ï¼è¿åèªå·±çå±æ§åå
¸
itchat.search_friends()
# è·åç¹å®UserNameçç¨æ·ä¿¡æ¯
itchat.search_friends(userName='@abcdefg1234567')
# è·åä»»ä½ä¸é¡¹çäºnameé®å¼çç¨æ·
itchat.search_friends(name='littlecodersh')
# è·ååå«å¯¹åºç¸åºé®å¼çç¨æ·
itchat.search_friends(wechatAccount='littlecodersh')
# ä¸ãå项åè½å¯ä»¥ä¸å使ç¨
itchat.search_friends(name='LittleCoderæºå¨äºº', wechatAccount='littlecodersh')
å ³äºå ¬ä¼å·ã群èçè·åä¸æç´¢å¨ææ¡£ä¸ææ´å 详ç»çä»ç»ã
é件çä¸è½½ä¸åé
itchatçé件ä¸è½½æ¹æ³åå¨å¨msgçTexté®ä¸ã
åéçæ件çæ件åï¼å¾çç»åºçé»è®¤æ件åï¼é½åå¨å¨msgçFileNameé®ä¸ã
ä¸è½½æ¹æ³æ¥åä¸ä¸ªå¯ç¨çä½ç½®åæ°ï¼å æ¬æ件åï¼ï¼å¹¶å°æ件ç¸åºçåå¨ã
@itchat.msg_register([PICTURE, RECORDING, ATTACHMENT, VIDEO])
def download_files(msg):
msg.download(msg.fileName)
itchat.send('@%s@%s' % (
'img' if msg['Type'] == 'Picture' else 'fil', msg['FileName']),
msg['FromUserName'])
return '%s received' % msg['Type']
å¦æä½ ä¸éè¦ä¸è½½å°æ¬å°ï¼ä» æ³è¦è¯»åäºè¿å¶ä¸²è¿è¡è¿ä¸æ¥å¤çå¯ä»¥ä¸ä¼ å ¥åæ°ï¼æ¹æ³å°ä¼è¿åå¾ççäºè¿å¶ä¸²ã
@itchat.msg_register([PICTURE, RECORDING, ATTACHMENT, VIDEO])
def download_files(msg):
with open(msg.fileName, 'wb') as f:
f.write(msg.download())
ç¨æ·å¤å¼
使ç¨å¦ä¸å½ä»¤å¯ä»¥å®æå¤å¼çæä½ï¼
import itchat
newInstance = itchat.new_instance()
newInstance.auto_login(hotReload=True, statusStorageDir='newInstance.pkl')
@newInstance.msg_register(itchat.content.TEXT)
def reply(msg):
return msg.text
newInstance.run()
éåºåç»éå®æåè°ç¨ç¹å®æ¹æ³
ç»éå®æåçæ¹æ³éè¦èµå¼å¨loginCallback
ä¸ã
èéåºåçæ¹æ³éè¦èµå¼å¨exitCallback
ä¸ã
import time
import itchat
def lc():
print('finish login')
def ec():
print('exit')
itchat.auto_login(loginCallback=lc, exitCallback=ec)
time.sleep(3)
itchat.logout()
è¥ä¸è®¾ç½®loginCallbackçå¼ï¼åå°ä¼èªå¨å é¤äºç»´ç å¾çå¹¶æ¸ ç©ºå½ä»¤è¡æ¾ç¤ºã
常è§é®é¢ä¸è§£ç
Q: å¦ä½éè¿è¿ä¸ªå å°èªå·±ç微信å·å为æ§å¶å¨ï¼
A: æ两ç§æ¹å¼ï¼åéãæ¥åèªå·±UserNameçæ¶æ¯ï¼åéæ¥æ¶æä»¶ä¼ è¾å©æï¼filehelperï¼çæ¶æ¯
Q: 为ä»ä¹æåéä¿¡æ¯çæ¶åé¨åä¿¡æ¯æ²¡ææåååºæ¥ï¼
A: æäºè´¦å·æ¯å¤©çæ æ³ç»èªå·±çè´¦å·åéä¿¡æ¯çï¼å»ºè®®ä½¿ç¨filehelper
代æ¿ã
ä½è
LittleCoder: ææ¶åç»´æ¤Python2 Python3çæ¬ã
tempdban: åè®®ãææ¶åæ¥å¸¸ç»´æ¤ã
Chyroc: å®æ第ä¸çæ¬çPython3ææ¶ã
类似项ç®
youfou/wxpy: ä¼ç§çapiå è£ åé å¥æ件ï¼å¾®ä¿¡æºå¨äºº/ä¼é ç微信个人å·API
liuwons/wxBot: 类似çåºäºPythonç微信æºå¨äºº
zixia/wechaty: åºäºJavascript(ES6)ç微信个人账å·æºå¨äººNodeJSæ¡æ¶/åº
sjdy521/Mojo-Weixin: 使ç¨Perlè¯è¨ç¼åç微信客æ·ç«¯æ¡æ¶ï¼å¯éè¿æ件æä¾åºäºHTTPåè®®çapiæ¥å£ä¾å ¶ä»è¯è¨è°ç¨
HanSon/vbot: åºäºPHP7ç微信个人å·æºå¨äººï¼éè¿å®ç°å¿åå½æ°å¯ä»¥æ¹ä¾¿å°å®ç°åç§èªå®ä¹çåè½
yaphone/itchat4j: ç¨Javaæ©å±ä¸ªäººå¾®ä¿¡å·çè½å
kanjielu/jeeves: 使ç¨springbootå¼åç微信æºå¨äºº
é®é¢å建议
å¦ææä»ä¹é®é¢æè 建议é½å¯ä»¥å¨è¿ä¸ªIssueåæ讨论
æè ä¹å¯ä»¥å¨gitterä¸äº¤æµï¼
å½ç¶ä¹å¯ä»¥å å ¥æ们æ°å»ºçQQ群讨论ï¼549762872, 205872856
Top Related Projects
微信机器人 / 可能是最优雅的微信个人号 API ✨✨
Conversational RPA SDK for Chatbot Makers. Join our Discord: https://discord.gg/7q8NBZbQzt
Python Wechaty is a Conversational RPA SDK for Chatbot Makers written in Python
Mac微信功能拓展/微信插件/微信小助手(A plugin for Mac WeChat)
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot