ItChat is a very good open source wechat personal number interface, using Python language development, provides a simple and easy to use API, can be very convenient to extend the personal wechat signal, automatic reply, wechat hang-up robot, etc.. This project is named ItChat4JS because it is mainly inspired by the ItChat project.

The installation

You can install ItChat4JS with this command:

npm install itchat4js
Copy the code

Getting Started

With ItChat4JS, if you want to send a message to the File Transfer Assistant, just do this:

import ItChat4JS, { sendTextMsg } from 'itchat4js';

const itChat4JsIns = new ItChat4JS();

const doFn = async() = > {await itChat4JsIns.login();
    await sendTextMsg('Hello FileHelper'.'filehelper');
};

doFn();

Copy the code

If you want to reply to a text message sent to yourself, just do this:

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

Copy the code

Classification of sources

import { EMIT_NAME } from 'itchat4js';
console.log('Classification of sources', EMIT_NAME)
Copy the code

FRIEND: messages from wechat friends CHAT_ROOM: messages from group chats MASSIVE_PLATFORM: messages from public accounts, subscription accounts

Message type classification

import { MESSAGE_TYPE } from 'itchat4js';
console.log('Message Type Classification', MESSAGE_TYPE)
Copy the code

-Serena: TEXT message MAP, location message CARD, NOTE SHARING PICTURE RECORDING VOICE ATTACHMENT VIDEO FRIENDS Add a friend application. SYSTEM: indicates a SYSTEM message

Registration of various types of messages

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();Copy the code

Common methods and use

Instance methods

1. Login async itchat4jsins.login ()

Receiving: Indicates whether to automatically receive messages. The default value is false

import ItChat4JS from 'itchat4js';

const itChat4JsIns = new ItChat4JS();

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

doFn();

Copy the code

Listen (emitName, msgType, callback)

EmitName: message source, see EMIT_NAME msgType: message type, MESSAGE_TYPE. Can be string or Array, undefined or null callback: the actual operation of processing a message

Callback parameters: msgInfo, toUserInfo msgInfo: message information after processing, including: text: text content Type: message information filename: Filename async download(path= “, filename= “, toStream=false) : download asynchronous downloads. You can specify the download path and filename. Filename will be automatically generated and will not be saved after downloading. It will be returned as stream. The default value is false (currently not supported).

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();
Copy the code

Async verifyFriend(userName, status = 2, verifyContent = “”, autoUpdate = true)

UserName: indicates the operation type. The default value is 2. VerifyContent: added and authenticated content autoUpdate: whether to update local friend information. The default value is true

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();
Copy the code

4. Obtain friend information through UserName, NickName or RemarkName. GetContactInfoByName (name)

Name: UserName or NickName

import ItChat4JS from 'itchat4js';

const itChat4JsIns = new ItChat4JS();

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

doFn()
Copy the code

5. Set async setAlias(userName, alias = “”)

UserName: userName, NickName or RemarkName Alias: Remarks

import ItChat4JS from 'itchat4js';

const itChat4JsIns = new ItChat4JS();

const doFn = async() = > {await itChat4JsIns.login();
    const info = itChat4JsIns.getContactInfoByName('xxx');
    itChat4JsIns.setAlias(info.UserName, 'Remarks name');
};

doFn()
Copy the code

6. Set group, top async setPinned(userName, isPinned = true)

UserName: userName, NickName or RemarkName isPinned: whether the pin isPinned to the top

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()
Copy the code

Create group chat async createChatRoom(memberList, topic = “”)

MemberList: user list ([{UserName: ‘XXX}]) Topic: group name

import ItChat4JS from 'itchat4js';

const itChat4JsIns = new ItChat4JS();

const doFn = async() = > {await itChat4JsIns.login();
    const userArr = itChat4JsIns.getContactInfoByName(['AAA'.'BBB']);
    itChat4JsIns.createChatRoom(userArr, 'Group chat Name');
};

doFn()
Copy the code

8. Set group chat name async setChatRoomName(chatRoomUserName, name) {

ChatRoomUserName: group UserName Name: group name

import ItChat4JS from 'itchat4js';

const itChat4JsIns = new ItChat4JS();

const doFn = async() = > {await itChat4JsIns.login();
    const info = itChat4JsIns.getContactInfoByName('XXX');
    itChat4JsIns.setChatRoomName(info.UserName, 'Group chat Name');
};

doFn()
Copy the code

Async deleteMemberFromChatRoom(chatRoomUserName, memberList) {

Always return Ret: 1 failed to delete group members.

ChatRoomUserName: group UserName memberList: user list ([{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()
Copy the code

10. Add group member async addMemberIntoChatRoom(chatRoomUserName, memberList, useInvitation = false) {

ChatRoomUserName: group UserName memberList: list of users ([{UserName: ‘XXX}]) useInvitation: Whether to send an invitation

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()
Copy the code

A static method

Async sendFile(fileDir, toUserName, mediaId, streamInfo = {})

Asynchronous method that sends a file to a specified friend. Generally, there are two steps: 1, upload, 2, send a message

Two ways to send: 1, the path of the incoming file, automatic upload and send. 2, incoming stream information, automatically upload and send

ToUserName: Sent friend UserName mediaId: mediaId streamInfo: file flow information returned after a file is uploaded, including: FileReadStream file flow, filename filename, extName file extension

import ItChat4JS, { sendFile } from 'itchat4js';

const itChat4JsIns = new ItChat4JS();

const doFn = async() = > {await itChat4JsIns.login();
    await sendFile('./file/txt.txt'.'filehelper');
};

doFn();

Copy the code

or

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

Copy the code

Async sendImage(fileDir, toUserName, mediaId, streamInfo = {})

Asynchronous method that sends a picture to a specified friend. Generally, there are two steps: 1, upload, 2, send a message

Two ways to send: 1, the path of the incoming file, automatic upload and send. 2, incoming stream information, automatically upload and send

ToUserName: Sent friend UserName mediaId: mediaId streamInfo: file flow information returned after a file is uploaded, including: FileReadStream file flow, filename filename, extName file extension

import ItChat4JS, { sendImage } from 'itchat4js';

const itChat4JsIns = new ItChat4JS();

const doFn = async() = > {await itChat4JsIns.login();
    await sendImage('./file/img.png'.'filehelper');
};

doFn();

Copy the code

or

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

Copy the code

Async sendVideo(fileDir, toUserName, mediaId, streamInfo = {})

Asynchronous method that sends a video to a specified friend. Generally, there are two steps: 1, upload, 2, send a message

Two ways to send: 1, the path of the incoming file, automatic upload and send. 2, incoming stream information, automatically upload and send

ToUserName: Sent friend UserName mediaId: mediaId streamInfo: file flow information returned after a file is uploaded, including: FileReadStream file flow, filename filename, extName file extension

import ItChat4JS, { sendVideo } from 'itchat4js';

const itChat4JsIns = new ItChat4JS();

const doFn = async() = > {await itChat4JsIns.login();
    await sendVideo('./file/video.mp4'.'filehelper');
};

doFn();

Copy the code

or

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

Copy the code

4. Send text async sendTextMsg(MSG, toUserName)

MSG: text content toUserName: sent friend UserName

import ItChat4JS, { sendTextMsg } from 'itchat4js';

const itChat4JsIns = new ItChat4JS();

const doFn = async() = > {await itChat4JsIns.login();
    await sendTextMsg('Hello File Helper'.'filehelper');
};

doFn();

Copy the code

4. Revoke sent message 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();
Copy the code

Demo

WeChatItChat4JS WeChatItChat4JS is a function for transferring messages on wechat based on itchat4JS

Similar projects

Itchat: An excellent Python-based API for wechat personal accounts and the inspiration for this project.

WeixinBot: Webpage version oF wechat API, including terminal version of wechat and wechat robot

Thank you:

Thanks to ItChat open source, thanks to ItChat authors

LittleCoder: Architecture and maintenance Python2 Python3.

Tempdban: Protocol, architecture and routine maintenance.

Chyroc: Complete the first version of Python3 architecture.

Questions and Suggestions

This project has been updated and maintained for a long time, and its functions are constantly expanding and improving. Welcome star.

If you have any problems or good suggestions, please feel free to feedback.

Any questions or suggestions can be put forward in the Issue, or you can add the author’s wechat id Kobezsj for discussion.