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进行讨论。

相关文章
相关标签/搜索