Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#! /usr/bin/env node
const {
Wechaty,
config,
} = require('wechaty')
const { Message } = require('wechaty')
const { MessageType } = require('wechaty-puppet')
const { PuppetPadplus } = require('wechaty-puppet-padplus')
const qrTerm = require('qrcode-terminal')
const brolog = require('brolog')
const log = new brolog.Brolog()
const token = process.env['WECHATY_PADPLUS_TOKEN']
const puppet = new PuppetPadplus({
token,
})
const bot = new Wechaty({
puppet,
profile : config.default.DEFAULT_PROFILE,
name : 'myPadplusBot',
})
bot
.on('scan', onScan)
.on('login', onLogin)
.on('message', onMessage)
.on('logout', onLogout)
.on('error', onError)
.start()
.catch(async e => {
console.error('Bot start() fail:', e)
await bot.stop()
process.exit(-1)
})
/**
* Define Event Handler Functions for:
* `scan`, `login`, `logout`, `error`, and `message`
*/
function onScan (qrcode, status) {
//qrTerm.generate(qrcode)
qrTerm.generate(qrcode, { small: true })
// Generate a QR Code online via http://goqr.me/api/doc/create-qr-code/
const qrcodeImageUrl = [
'https://api.qrserver.com/v1/create-qr-code/?data=',
encodeURIComponent(qrcode),
].join('')
console.log(`[${status}] ${qrcodeImageUrl}\nScan QR Code above to log in: `)
}
function onLogin (user) {
console.log(`${user.name()} login`)
bot.say('Wechaty login').catch(console.error)
}
function onLogout (user) {
console.log(`${user.name()} logouted`)
}
function onError (e) {
console.error('Bot error:', e)
/*
if (bot.logonoff()) {
bot.say('Wechaty error: ' + e.message).catch(console.error)
}
*/
}
/**
* The most important handler is for:
* dealing with Messages.
*/
async function onMessage (msg) {
if (msg.age() > 60) {
console.log('Message discarded because its TOO OLD(than 1 minute)')
return
}
// log every incoming message on console
// console.log(msg.toString())
const msgtype = msg.type()
log.info('Received','type(%s)\n %s', msgtype, msg)
if ( msg.type() !== Message.Type.Text
|| !/^(ding|ping|bing|code)$/i.test(msg.text())
/*&& !msg.self()*/
) {
//console.log('Message discarded because it does not match ding/ping/bing/code')
return
}
await msg.say('dong')
console.log('REPLY: dong')
}
/**
* Output the Welcome Message
*/
const welcome = `
Please wait... preparing for login in...
`
console.log(welcome)
import { Wechaty } from 'wechaty'
import { PuppetPadplus } from 'wechaty-puppet-padplus'
import QrcodeTerminal from 'qrcode-terminal'
const token = process.env['WECHATY_PADPLUS_TOKEN']
const puppet = new PuppetPadplus({
token,
})
const name = 'my-bot-name'
const bot = new Wechaty({
puppet,
name, // generate xxxx.memory-card.json and save login data for the next login
})
bot
.on('scan', (qrcode) => {
QrcodeTerminal.generate(qrcode, {
small: true
})
})
.on('message', msg => {
console.log(`msg : ${msg}`)
})
.start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.