ItChat4JS -- 用NodeJs擴展我的微信號的能力

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();

複製代碼

經常使用的方法及使用

實例方法

1.登陸 async itChat4JsIns.login()

異步方法,用戶掃碼登陸,receiving:是否自動接收消息,默認false微信

import ItChat4JS from 'itchat4js';

const itChat4JsIns = new ItChat4JS();

const doFn = async () => {
    await itChat4JsIns.login();
};

doFn();

複製代碼

2.監聽消息 listen(emitName, msgType, callback)

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();
複製代碼

3.好友添加及驗證 async verifyFriend(userName, status = 2, verifyContent = '', autoUpdate = true)

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();
複製代碼

4.經過UserName,NickName或者RemarkName獲取好友信息 getContactInfoByName(name)

name:UserName或者NickName

import ItChat4JS from 'itchat4js';

const itChat4JsIns = new ItChat4JS();

const doFn = async () => {
    await itChat4JsIns.login();
    console.log(itChat4JsIns.getContactInfoByName('xxx'));
};

doFn()
複製代碼

5.設置好友備註 async setAlias(userName, alias = '')

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()
複製代碼

6.設置羣組,好友置頂 async setPinned(userName, isPinned = true)

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()
複製代碼

7.建立羣聊 async createChatRoom(memberList, topic = '')

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()
複製代碼

8.設置羣聊名稱 async setChatRoomName(chatRoomUserName, name) {

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()
複製代碼

9.刪除羣成員 async deleteMemberFromChatRoom(chatRoomUserName, memberList) {

Tip:刪除羣成員老是返回Ret:1不成功(網頁版也不成功)

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()
複製代碼

10.添加羣成員 async addMemberIntoChatRoom(chatRoomUserName, memberList, useInvitation = false) {

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()
複製代碼

靜態方法

1.發送文件 async sendFile(fileDir, toUserName, mediaId, streamInfo = {})

異步方法,向指定好友發送文件。通常須要兩步:一、上傳,二、發送消息

兩種方式發送:一、傳入文件路徑,自動上傳和發送。二、傳入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()

複製代碼

2.發送圖片 async sendImage(fileDir, toUserName, mediaId, streamInfo = {})

異步方法,向指定好友發送圖片。通常須要兩步:一、上傳,二、發送消息

兩種方式發送:一、傳入文件路徑,自動上傳和發送。二、傳入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()

複製代碼

3.發送視頻 async sendVideo(fileDir, toUserName, mediaId, streamInfo = {})

異步方法,向指定好友發送視頻。通常須要兩步:一、上傳,二、發送消息

兩種方式發送:一、傳入文件路徑,自動上傳和發送。二、傳入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()

複製代碼

4.發送文本 async sendTextMsg(msg, toUserName)

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();

複製代碼

4.撤回發送的消息 async revokeMsg(msgId, toUserName, localId)

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();
複製代碼

Demo

WeChatItChat4JS 基於itchat4js完成的一個關於微信號中轉消息的功能

相似項目

itchat :優秀的、基於Python的微信我的號API,同時也是本項目的靈感之源。

WeixinBot: 網頁版微信API,包含終端版微信及微信機器人

致謝:

感謝ItChat的開源,感謝ItChat做者

LittleCoder: 構架及維護Python2 Python3版本。

tempdban: 協議、構架及平常維護。

Chyroc: 完成初版本的Python3構架。

問題和建議

本項目長期更新、維護,功能不斷擴展與完善中,歡迎star。

項目使用過程當中遇到問題,或者有好的建議,歡迎隨時反饋。

任何問題或者建議均可以在Issue中提出來,也能夠添加做者微信號kobezsj進行討論。

相關文章
相關標籤/搜索