2 Star 1 Fork 0

Wechaty / wechaty-getting-started

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
ding-dong-bot.ts 2.06 KB
一键复制 编辑 原始数据 按行查看 历史
/**
* Wechaty - Conversational RPA SDK for Chatbot Makers.
* - https://github.com/wechaty/wechaty
*/
import {
Contact,
Message,
ScanStatus,
Wechaty,
log,
} from 'wechaty'
import { generate } from 'qrcode-terminal'
require('dotenv').config()
function onScan (qrcode: string, status: ScanStatus) {
if (status === ScanStatus.Waiting || status === ScanStatus.Timeout) {
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: 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 = 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-wechat (web protocol, no token required)
* - wechaty-puppet-whatsapp (web protocol, no token required)
* - wechaty-puppet-padlocal (pad protocol, token required)
* - wechaty-puppet-service (token required, see: <https://wechaty.js.org/docs/puppet-services>)
* - etc. see: <https://wechaty.js.org/docs/puppet-providers/>
*/
// puppet: 'wechaty-puppet-whatsapp'
})
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))
TypeScript
1
https://gitee.com/wechaty/wechaty-getting-started.git
git@gitee.com:wechaty/wechaty-getting-started.git
wechaty
wechaty-getting-started
wechaty-getting-started
master

搜索帮助