mirror of
https://github.com/wechaty/getting-started.git
synced 2026-10-16 21:12:46 +08:00
5f86ab282b
* fix: ding-dong requires uos * 1.18.8 * fix: gcloud tutorial system dependencies:libatk-bridge2.0 * 1.18.9 * Update ding-dong-bot.ts --------- Co-authored-by: Huan Li <zixia@zixia.net>
95 lines
2.7 KiB
JavaScript
Executable File
95 lines
2.7 KiB
JavaScript
Executable File
#!/usr/bin/env -S node --no-warnings --loader ts-node/esm
|
|
/**
|
|
* Wechaty - Conversational RPA SDK for Chatbot Makers.
|
|
* - https://github.com/wechaty/wechaty
|
|
*/
|
|
// https://stackoverflow.com/a/42817956/1123955
|
|
// https://github.com/motdotla/dotenv/issues/89#issuecomment-587753552
|
|
import 'dotenv/config.js'
|
|
|
|
import {
|
|
Contact,
|
|
Message,
|
|
ScanStatus,
|
|
WechatyBuilder,
|
|
log,
|
|
} from 'wechaty'
|
|
|
|
import qrcodeTerminal from 'qrcode-terminal'
|
|
|
|
function onScan (qrcode: string, status: ScanStatus) {
|
|
if (status === ScanStatus.Waiting || status === ScanStatus.Timeout) {
|
|
const qrcodeImageUrl = [
|
|
'https://wechaty.js.org/qrcode/',
|
|
encodeURIComponent(qrcode),
|
|
].join('')
|
|
log.info('StarterBot', 'onScan: %s(%s) - %s', ScanStatus[status], status, qrcodeImageUrl)
|
|
|
|
qrcodeTerminal.generate(qrcode, { small: true }) // show qrcode on console
|
|
|
|
} else {
|
|
log.info('StarterBot', 'onScan: %s(%s)', ScanStatus[status], status)
|
|
}
|
|
}
|
|
|
|
function onLogin (user: Contact) {
|
|
log.info('StarterBot', '%s login', user)
|
|
}
|
|
|
|
function onLogout (user: Contact) {
|
|
log.info('StarterBot', '%s logout', user)
|
|
}
|
|
|
|
async function onMessage (msg: Message) {
|
|
log.info('StarterBot', msg.toString())
|
|
|
|
if (msg.text() === 'ding') {
|
|
await msg.say('dong')
|
|
}
|
|
}
|
|
|
|
const bot = WechatyBuilder.build({
|
|
name: 'ding-dong-bot',
|
|
/**
|
|
* You can specific `puppet` and `puppetOptions` here with hard coding:
|
|
*
|
|
puppet: 'wechaty-puppet-wechat',
|
|
puppetOptions: {
|
|
uos: true,
|
|
},
|
|
*/
|
|
/**
|
|
* How to set Wechaty Puppet Provider:
|
|
*
|
|
* 1. Specify a `puppet` option when instantiating Wechaty. (like `{ puppet: 'wechaty-puppet-whatsapp' }`, see below)
|
|
* 1. Set the `WECHATY_PUPPET` environment variable to the puppet NPM module name. (like `wechaty-puppet-whatsapp`)
|
|
*
|
|
* You can use the following providers locally:
|
|
* - wechaty-puppet-wechat (web protocol, no token required)
|
|
* - wechaty-puppet-whatsapp (web protocol, no token required)
|
|
* - wechaty-puppet-padlocal (pad protocol, token required)
|
|
* - etc. see: <https://wechaty.js.org/docs/puppet-providers/>
|
|
*/
|
|
// puppet: 'wechaty-puppet-whatsapp'
|
|
|
|
/**
|
|
* You can use wechaty puppet provider 'wechaty-puppet-service'
|
|
* which can connect to remote Wechaty Puppet Services
|
|
* for using more powerful protocol.
|
|
* Learn more about services (and TOKEN) from https://wechaty.js.org/docs/puppet-services/
|
|
*/
|
|
// puppet: 'wechaty-puppet-service'
|
|
// puppetOptions: {
|
|
// token: 'xxx',
|
|
// }
|
|
})
|
|
|
|
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))
|