ItChat是一個很是優秀的開源微信我的號接口,使用Python語言開發,提供了簡單易用的API,能夠很方便地對我的微信號進行擴展,實現自動回覆,微信掛機機器人等。 經過閱讀和學習ItChat python源碼,經過NodeJs完成了一個JS版本,因爲主要靈感來源於itchat項目,因此這個項目的就暫時定名爲ItChat4JS吧。javascript
能夠經過本命令安裝ItChat4JS:java
npm install itchat4js
複製代碼
有了ItChat4JS,若是你想要給文件傳輸助手發一條信息,只須要這樣:python
import ItChat4JS, { sendTextMsg } from 'itchat4js';
const itChat4JsIns = new ItChat4JS();
const doFn = async () => {
await itChat4JsIns.login();
await sendTextMsg('Hello FileHelper', 'filehelper');
};
doFn();
複製代碼
若是你想要回復發給本身的文本消息,只須要這樣:git
import ItChat4JS, { sendTextMsg, EMIT_NAME, MESSAGE_TYPE } from 'itchat4js';
const itChat4JsIns = new ItChat4JS();
itChat4JsIns.listen(EMIT_NAME.FRIEND, MESSAGE_TYPE.TEXT, async (msgInfo, toUserInfo) => {
const { text } = msgInfo;
const { UserName } = toUserInfo;
sendTextMsg(text, UserName)
});
itChat4JsIns.run();
複製代碼
import { EMIT_NAME } from 'itchat4js';
console.log('消息來源分類', EMIT_NAME)
複製代碼
FRIEND:來自於微信好友的消息 CHAT_ROOM:來自於羣聊的消息 MASSIVE_PLATFORM:來自於公衆號、訂閱號的消息github
import { MESSAGE_TYPE } from 'itchat4js';
console.log('消息類型分類', MESSAGE_TYPE)
複製代碼
TEXT:文本消息 MAP:地圖、位置消息 CARD:名片 NOTE:筆記 SHARING:分享 PICTURE:圖片 RECORDING: VOICE:語音 ATTACHMENT:附件文件 VIDEO:視頻 FRIENDS:添加好友申請 SYSTEM:系統消息npm
import ItChat4JS, { sendTextMsg, EMIT_NAME, MESSAGE_TYPE } from 'itchat4js';
const itChat4JsIns = new ItChat4JS();
const msgArr = [
MESSAGE_TYPE.TEXT,
MESSAGE_TYPE.PICTURE,
MESSAGE_TYPE.FRIENDS,
MESSAGE_TYPE.VIDEO,
];
itChat4JsIns.listen(EMIT_NAME.FRIEND, msgArr, async (msgInfo, toUserInfo) => {
const { text, type, download } = msgInfo;
if (type === MESSAGE_TYPE.PICTURE) {
await download('download/friend/image');
} else if (type === MESSAGE_TYPE.FRIENDS) {
const { status, verifyContent, autoUpdate: { UserName } } = text;
itChat4JsIns.verifyFriend(UserName, status, verifyContent)
} else if (type === MESSAGE_TYPE.TEXT) {
console.log(text)
} else if (type === MESSAGE_TYPE.VIDEO) {
await download('download/friend/video');
}
});
itChat4JsIns.run();
複製代碼
異步方法,用戶掃碼登陸,receiving:是否自動接收消息,默認false微信
import ItChat4JS from 'itchat4js';
const itChat4JsIns = new ItChat4JS();
const doFn = async () => {
await itChat4JsIns.login();
};
doFn();
複製代碼
emitName:消息來源,參見EMIT_NAME msgType: 消息類型,MESSAGE_TYPE。能夠是string也能夠是Array,也能夠undefined或null callback:處理消息的實際操做異步
callback參數:msgInfo, toUserInfo msgInfo:通過處理以後的消息信息,包含: text:文本內容 type:消息信息 filename:文件名 async download(path='', filename='', toStream=false):下載 download 異步下載,能夠指定下載路徑和文件名。path不傳,會以項目根路徑爲目錄下載,filename不傳會自動生成,下載以後不保存,以stream的形式返回,默認值false(目前還不支持)async
import ItChat4JS, { sendTextMsg, EMIT_NAME, MESSAGE_TYPE } from 'itchat4js';
const itChat4JsIns = new ItChat4JS();
itChat4JsIns.listen(EMIT_NAME.FRIEND, MESSAGE_TYPE.TEXT, async (msgInfo, toUserInfo) => {
const { text } = msgInfo;
const { UserName } = toUserInfo;
sendTextMsg(text, UserName)
});
itChat4JsIns.run();
複製代碼
userName:申請加好友的UserName status: 操做類型,默認值爲2。2:添加好友,3:驗證好友申請 verifyContent:添加和被添加的驗證內容 autoUpdate:是否更新本地好友信息,默認值爲trueide
import ItChat4JS, { EMIT_NAME, MESSAGE_TYPE } from 'itchat4js';
const itChat4JsIns = new ItChat4JS();
itChat4JsIns.listen(EMIT_NAME.FRIEND, MESSAGE_TYPE.FRIENDS, async (msgInfo, toUserInfo) => {
const { text } = msgInfo;
const { status, verifyContent, autoUpdate: { UserName } } = text;
itChat4JsIns.verifyFriend(UserName, status, verifyContent)
});
itChat4JsIns.run();
複製代碼
name:UserName或者NickName
import ItChat4JS from 'itchat4js';
const itChat4JsIns = new ItChat4JS();
const doFn = async () => {
await itChat4JsIns.login();
console.log(itChat4JsIns.getContactInfoByName('xxx'));
};
doFn()
複製代碼
userName:UserName,NickName或RemarkName alias:備註
import ItChat4JS from 'itchat4js';
const itChat4JsIns = new ItChat4JS();
const doFn = async () => {
await itChat4JsIns.login();
const info = itChat4JsIns.getContactInfoByName('xxx');
itChat4JsIns.setAlias(info.UserName, '備註名稱');
};
doFn()
複製代碼
userName:UserName,NickName或RemarkName isPinned:是否置頂
import ItChat4JS from 'itchat4js';
const itChat4JsIns = new ItChat4JS();
const doFn = async () => {
await itChat4JsIns.login();
const info = itChat4JsIns.getContactInfoByName('xxx');
itChat4JsIns.setPinned(info.UserName, true);
};
doFn()
複製代碼
memberList:用戶列表([{UserName: 'xxx}]) topic:羣名稱
import ItChat4JS from 'itchat4js';
const itChat4JsIns = new ItChat4JS();
const doFn = async () => {
await itChat4JsIns.login();
const userArr = itChat4JsIns.getContactInfoByName(['AAA', 'BBB']);
itChat4JsIns.createChatRoom(userArr, '羣聊名稱');
};
doFn()
複製代碼
chatRoomUserName:羣UserName name:羣名稱
import ItChat4JS from 'itchat4js';
const itChat4JsIns = new ItChat4JS();
const doFn = async () => {
await itChat4JsIns.login();
const info = itChat4JsIns.getContactInfoByName('XXX');
itChat4JsIns.setChatRoomName(info.UserName, '羣聊名稱');
};
doFn()
複製代碼
chatRoomUserName:羣UserName memberList:用戶列表([{UserName: 'xxx}])
import ItChat4JS from 'itchat4js';
const itChat4JsIns = new ItChat4JS();
const doFn = async () => {
await itChat4JsIns.login();
const userArr = NodeWeChatIns.getContactInfoByName(['AAA', 'BBB']);
const info = itChat4JsIns.getContactInfoByName('XXX');
itChat4JsIns.deleteMemberFromChatRoom(info.UserName, userArr);
};
doFn()
複製代碼
chatRoomUserName:羣UserName memberList:用戶列表([{UserName: 'xxx}]) useInvitation:是否以發出邀請的形式
import ItChat4JS from 'itchat4js';
const itChat4JsIns = new ItChat4JS();
const doFn = async () => {
await itChat4JsIns.login();
const userArr = NodeWeChatIns.getContactInfoByName(['AAA', 'BBB']);
const info = itChat4JsIns.getContactInfoByName('XXX');
itChat4JsIns.addMemberIntoChatRoom(info.UserName, userArr, true);
};
doFn()
複製代碼
異步方法,向指定好友發送文件。通常須要兩步:一、上傳,二、發送消息
兩種方式發送:一、傳入文件路徑,自動上傳和發送。二、傳入stream信息,自動上傳和發送
fileDir:發送的文件所在路徑 toUserName:發送的好友UserName mediaId:上傳文件以後返回的MediaId streamInfo:文件流信息,包含:fileReadStream文件流,filename文件名,extName文件擴展名
import ItChat4JS, { sendFile } from 'itchat4js';
const itChat4JsIns = new ItChat4JS();
const doFn = async () => {
await itChat4JsIns.login();
await sendFile('./file/txt.txt', 'filehelper');
};
doFn();
複製代碼
或者
import ItChat4JS, { sendFile } from 'itchat4js';
import fs from 'fs';
const itChat4JsIns = new ItChat4JS();
const doFn = async () => {
await itChat4JsIns.login();
const stream = fs.createReadStream('./file/txt.txt');
const streamInfo = {
fileReadStream: stream,
filename: 'txt.txt',
extName: '.txt'
};
sendFile(null, 'filehelper', null, streamInfo)
};
doFn()
複製代碼
異步方法,向指定好友發送圖片。通常須要兩步:一、上傳,二、發送消息
兩種方式發送:一、傳入文件路徑,自動上傳和發送。二、傳入stream信息,自動上傳和發送
fileDir:發送的文件所在路徑 toUserName:發送的好友UserName mediaId:上傳文件以後返回的MediaId streamInfo:文件流信息,包含:fileReadStream文件流,filename文件名,extName文件擴展名
import ItChat4JS, { sendImage } from 'itchat4js';
const itChat4JsIns = new ItChat4JS();
const doFn = async () => {
await itChat4JsIns.login();
await sendImage('./file/img.png', 'filehelper');
};
doFn();
複製代碼
或者
import ItChat4JS, { sendImage } from 'itchat4js';
import fs from 'fs';
const itChat4JsIns = new ItChat4JS();
const doFn = async () => {
await itChat4JsIns.login();
const stream = fs.createReadStream('./file/img.png');
const streamInfo = {
fileReadStream: stream,
//filename: 'img.png',
extName: '.png'
};
sendImage(null, 'filehelper', null, streamInfo)
};
doFn()
複製代碼
異步方法,向指定好友發送視頻。通常須要兩步:一、上傳,二、發送消息
兩種方式發送:一、傳入文件路徑,自動上傳和發送。二、傳入stream信息,自動上傳和發送
fileDir:發送的文件所在路徑 toUserName:發送的好友UserName mediaId:上傳文件以後返回的MediaId streamInfo:文件流信息,包含:fileReadStream文件流,filename文件名,extName文件擴展名
import ItChat4JS, { sendVideo } from 'itchat4js';
const itChat4JsIns = new ItChat4JS();
const doFn = async () => {
await itChat4JsIns.login();
await sendVideo('./file/video.mp4', 'filehelper');
};
doFn();
複製代碼
或者
import ItChat4JS, { sendVideo } from 'itchat4js';
import fs from 'fs';
const itChat4JsIns = new ItChat4JS();
const doFn = async () => {
await itChat4JsIns.login();
const stream = fs.createReadStream('./file/video.mp4');
const streamInfo = {
fileReadStream: stream,
//filename: 'video.mp4',
//extName: '.mp4'
};
sendVideo(null, 'filehelper', null, streamInfo)
};
doFn()
複製代碼
msg:文本內容 toUserName:發送的好友UserName
import ItChat4JS, { sendTextMsg } from 'itchat4js';
const itChat4JsIns = new ItChat4JS();
const doFn = async () => {
await itChat4JsIns.login();
await sendTextMsg('Hello File Helper', 'filehelper');
};
doFn();
複製代碼
import ItChat4JS, { revokeMsg } from 'itchat4js';
const itChat4JsIns = new ItChat4JS();
const doFn = async () => {
await itChat4JsIns.login();
const {MsgID, LocalID} = await sendTextMsg('Hello File Helper', 'filehelper');
revokeMsg(res.MsgID, username, res.LocalID);
};
doFn();
複製代碼
WeChatItChat4JS 基於itchat4js完成的一個關於微信號中轉消息的功能
itchat :優秀的、基於Python的微信我的號API,同時也是本項目的靈感之源。
WeixinBot: 網頁版微信API,包含終端版微信及微信機器人
感謝ItChat的開源,感謝ItChat做者
LittleCoder: 構架及維護Python2 Python3版本。
tempdban: 協議、構架及平常維護。
Chyroc: 完成初版本的Python3構架。
本項目長期更新、維護,功能不斷擴展與完善中,歡迎star。
項目使用過程當中遇到問題,或者有好的建議,歡迎隨時反饋。
任何問題或者建議均可以在Issue中提出來,也能夠添加做者微信號kobezsj進行討論。