Skip to content

Instantly share code, notes, and snippets.

@suntong
Last active March 3, 2022 16:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save suntong/14a3427a72c7023ef1c5148bd1be6d9a to your computer and use it in GitHub Desktop.
Save suntong/14a3427a72c7023ef1c5148bd1be6d9a to your computer and use it in GitHub Desktop.
/**
* Wechaty - WeChat Bot SDK for Personal Account, Powered by TypeScript, Docker, and 💖
* - https://github.com/chatie/wechaty
*/
const {
Wechaty,
ScanStatus,
log,
} = require('wechaty')
// for pkg
require('./node_modules/in-gfw/lib/timezone.js')
function onScan (qrcode, status) {
if (status === ScanStatus.Waiting || status === ScanStatus.Timeout) {
require('qrcode-terminal').generate(qrcode, { small: true }) // show qrcode on console
const qrcodeImageUrl = [
'https://wechaty.js.org/qrcode/',
encodeURIComponent(qrcode),
].join('')
log.info('StarterBot', 'onScan: %s(%s) - %s', ScanStatus[status], status, qrcodeImageUrl)
} else {
log.info('StarterBot', 'onScan: %s(%s)', ScanStatus[status], status)
}
}
function onLogin (user) {
log.info('StarterBot', '%s login', user)
}
function onLogout (user) {
log.info('StarterBot', '%s logout', user)
}
async function onMessage (msg) {
const msgtype = msg.type()
log.info('Received','type(%s)\n %s', msgtype, msg.toString())
// log every incoming message on console
console.log(msg)
if (msg.text() === 'ding') {
await msg.say('dong')
}
if (msgtype == 14) {
const link = await msg.toUrlLink()
console.log(link)
//const r = await messageUrl(msg.id)
//console.log(r)
}
}
const bot = new Wechaty({
name: 'ding-dong-bot',
/**
* How to set Wechaty Puppet Provider:
*
* 1. Specify a `puppet` option when instantiating Wechaty. (like `{ puppet: 'wechaty-puppet-padlocal' }`, see below)
* 1. Set the `WECHATY_PUPPET` environment variable to the puppet NPM module name. (like `wechaty-puppet-padlocal`)
*
* You can use the following providers:
* - wechaty-puppet-puppeteer (no token required)
* - wechaty-puppet-padlocal (token required)
* - wechaty-puppet-service (token required, see: <https://wechaty.js.org/docs/puppet-services>)
* - etc. see: <https://github.com/wechaty/wechaty-puppet/wiki/Directory>
*/
// puppet: 'wechaty-puppet-puppeteer',
})
bot.on('scan', onScan)
bot.on('login', onLogin)
bot.on('logout', onLogout)
bot.on('message', onMessage)
bot.start()
.then(() => log.info('StarterBot', 'Starter Bot Started.'))
.catch(e => log.error('StarterBot', e))
async function messageUrl (messageId) {
console.log(`messageUrl(${messageId})`)
const rawPayload = await bot.messageRawPayload(messageId)
const payload = await bot.messagePayload(messageId)
if (payload.type !== bot.MessageType.Url) {
throw new Error('Can not get url from non url payload')
} else {
const appPayload = await bot.appMessageParser(rawPayload)
if (appPayload) {
return {
description: appPayload.des,
thumbnailUrl: appPayload.thumburl,
title: appPayload.title,
url: appPayload.url,
}
} else {
throw new Error('Can not parse url message payload')
}
}
}
{
"name": "wechaty-puppet-padlocal-demo",
"version": "0.1.2",
"description": "Demo for wechaty-puppet-padlocal",
"author": "haoda",
"license": "Apache-2.0",
"dependencies": {
"command-line-parser": "^0.1.10",
"cron": "^1.8.2",
"dotenv": "^16.0.0",
"file-box": "0.16.8",
"he": "^1.2.0",
"pkg": "^5.5.2",
"puppeteer": "^13.3.2",
"puppeteer-extra": "^3.2.3",
"puppeteer-extra-plugin-stealth": "^2.9.0",
"qrcode-terminal": "^0.12.0",
"tgfancy": "^0.13.0",
"wechaty": "^0.63.6",
"wechaty-puppet": "0.41.9",
"wechaty-puppet-padlocal": "^0.4.1"
},
"pkg": {
"scripts": "*.js",
"targets": [ "node14-linux" ],
"assets": [
"node_modules/wechaty-grpc/dist/proto/*",
"node_modules/wechaty-grpc/dist/proto/*/*",
"node_modules/better-sqlite3/build/Release/*",
"node_modules/wechaty-grpc/dist/generated/wechaty/puppet.swagger.json",
"node_modules/encoding/node_modules/iconv-lite/encodings/tables/*.json"
]
},
"bin": {
"dd": "./index.js"
},
"scripts": {
"demo": "ts-node main.ts"
}
}
@suntong
Copy link
Author

suntong commented Mar 3, 2022

for pkg, exe building

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment