Giter Club home page Giter Club logo

yochat's Introduction

yochat

yochat 是一个微信机器人的Node.js版本的实现,集成了Web版微信的所有接口,功能,简单的几行命令就可以定义一个微信小机器人,当然,你也可以基于该代码自己扩展丰富的功能.

入门

定义简单的机器人:

const Wechat = require('yochat')

// 全局配置
Wechat.config({
    autoLogin: true, // 是否保存cookie 以便自动登录
    openBrowser: false, // 是否在浏览器中打开二维码链接 (默认在terminal中显示)
    cookie: 'cookie string' // 自定义设置cookie字符串
})

// 初始化程序
Wechat.run(async () => {
    // 获取联系人列表
    let memberList = await Wechat.getContact()
    // 获取账户信息
    let ownerInfo = await Wechat.getOwnerInfo()
    // 给指定用户发送消息(这里测试自己给自己发)
    var ownUserName = ownerInfo.User.UserName;
    let sendStatus = await Wechat.sendMsg(ownUserName, ownUserName, `hello word! 现在的时间是:${new Date()}`)
    if (sendStatus) {
        console.log('消息发送成功!')
    } 

    // 自动回复消息
    Wechat.listener.on('message', async data => {
        // 群聊消息
        if (data.type === 'Group') {

            var groupName = data.fromUser.NickName;
            var masterUserName = data.toUser.NickName;

            if (data.msg.indexOf('exit') > -1) {
                console.log('发送 "exit" 退出群聊')
                Wechat.deleteMemberFromChatroom(data.fromUser.UserName, data.toUser.UserName)
            }

            if (data.msg.indexOf('rename') > -1) {
                console.log('发送 "rename" 修改群聊名称')
                Wechat.renameChatroom(data.fromUser.UserName, new Date().getTime())
            }
            console.log(`收到群聊消息: ${masterUserName}(${groupName}): ${data.msg}`)
        } 
        // 普通消息
        else {
            console.log(`收到来自: ${data.fromUser.NickName} 的消息: ${data.msg}`)
        }

        // 自动回复
        let sendStatus = await Wechat.sendMsg(ownUserName, data.fromUser.UserName, `自动回复: ${data.msg}`)
        if (sendStatus) {
            console.log('消息发送成功!')
        }
    })
})

全局配置:

Wechat.config({
    autoLogin: true, // 是否保存cookie 以便自动登录
    openBrowser: false, // 是否在浏览器中打开二维码链接 (默认在terminal中显示)
})

效果演示:

image

yochat's People

Contributors

w88975 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.