提交 97268d2e 编写于 作者: ruiruibupt's avatar ruiruibupt

Merge branch 'master' of github.com:Chatie/wechaty into bug-compatible-createroom

[![Wechaty](https://chatie.io/wechaty/images/wechaty-logo-en.png)](https://github.com/chatie/wechaty)
# WECHATY
[![Wechaty](https://chatie.io/wechaty/images/wechaty-logo-en.png)](https://github.com/chatie/wechaty)
## CONNECTING CHATBOTS
Wechaty is a Bot SDK for Wechat **Personal** Account which can help you create a bot in 6 lines of javascript, with cross-platform support include [Linux](https://travis-ci.com/chatie/wechaty), [Windows](https://ci.appveyor.com/project/chatie/wechaty), [Darwin(OSX/Mac)](https://travis-ci.com/chatie/wechaty) and [Docker](https://app.shippable.com/github/Chatie/wechaty).
......@@ -37,14 +38,16 @@ See more at [Wiki:VoiceOfDeveloper](https://github.com/Chatie/wechaty/wiki/Voice
## The World's Shortest ChatBot Code: 6 lines of JavaScript
```javascript
const { Wechaty } = require('wechaty') // import { Wechaty } from 'wechaty'
Wechaty.instance() // Singleton
.on('scan', (url, code) => console.log(`Scan QR Code to login: ${code}\n${url}`))
.on('login', user => console.log(`User ${user} logined`))
.on('message', message => console.log(`Message: ${message}`))
Wechaty.instance() // Global Instance
.on('scan', (qrcode, status) => console.log(`Scan QR Code to login: ${status}\n${qrcode}`))
.on('login', user => console.log(`User ${user} logined`))
.on('message', message => console.log(`Message: ${message}`))
.start()
```
> **Notice: Wechaty requires Node.js version >= 8.5**
This bot can log all messages to the console.
......@@ -93,12 +96,14 @@ Get to know more about Wechaty Docker at [Wiki:Docker](https://github.com/chatie
```shell
$ docker run -ti --rm --volume="$(pwd)":/bot zixia/wechaty mybot.js # for JavaScript
...
```
1. Run TypeScript
```shell
$ docker run -ti --rm --volume="$(pwd)":/bot zixia/wechaty mybot.ts # for TypeScript
...
```
#### NPM
......@@ -257,9 +262,3 @@ At last, It's built for my personal study purpose of Automatically Testing.
[downloads-image]: http://img.shields.io/npm/dm/wechaty.svg?style=flat-square
[downloads-url]: https://npmjs.org/package/wechaty
## NOTES
* github.com/chatie-oos: Open Open Source for Chatie Community.
* wechaty-puppet Plugin Design: strong support wechaty.
* list all donation sponsors on README, and create a wechat group for sponsors.
......@@ -14,8 +14,10 @@
| tuling123-bot.ts | Answer Any Question |
| hot-import-bot | Use hot-import for updating code without restarting program |
| blessed-twins-bot/ | Multi-Instance Twins Bot Powered by Blessed |
| busy-bot.ts | Enter Auto Response Mode when you are BUSY |
Learn more about Wechaty from:
1. API Document: <https://github.com/Chatie/wechaty/wiki/API>
1. Chatie Blog: <https://blog.chatie.io>
......
#!/usr/bin/env node
/**
* Wechaty - https://github.com/chatie/wechaty
*
* @copyright 2016-2018 Huan LI <zixia@zixia.net>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
/* tslint:disable:variable-name */
import { generate } from 'qrcode-terminal'
import {
Wechaty,
log,
} from '../src/'
console.log(`
=============== Powered by Wechaty ===============
-------- https://github.com/Chatie/wechaty --------
I'm the BUSY BOT, I can do auto response message for you when you are BUSY.
Send command to FileHelper to:
1. '#busy' - set busy mode ON
2. '#busy I'm busy' - set busy mode ON and set a Auto Reply Message
3. '#free' - set busy mode OFF
4. '#status' - check the current Busy Mode and Auto Reply Message.
Loading... please wait for QrCode Image Url and then scan to login.
`)
const bot = Wechaty.instance()
bot
.on('scan', (qrcode, status) => {
generate(qrcode, { small: true })
console.log(`${status}\n[${qrcode}] Scan QR Code of the url to login:`)
})
.on('logout' , user => log.info('Bot', `${user.name()} logouted`))
.on('error' , e => log.info('Bot', 'error: %s', e))
.on('login', async function(this, user) {
const msg = `${user.name()} logined`
log.info('Bot', msg)
await this.say(msg)
})
/**
* Global Event: message
*/
let busyIndicator = false
let busyAnnouncement = `Automatic Reply: I cannot read your message because I'm busy now, will talk to you when I get back.`
bot.on('message', async function(msg) {
log.info('Bot', '(message) %s', msg)
const filehelper = bot.Contact.load('filehelper')
const sender = msg.from()
const receiver = msg.to()
const text = msg.text()
const room = msg.room()
// if (msg.age() > 60) {
// log.info('Bot', 'on(message) skip age(%d) > 60 seconds: %s', msg.age(), msg)
// return
// }
if (!sender || !receiver) {
return
}
if (receiver.id === 'filehelper') {
if (text === '#status') {
filehelper.say('in busy mode: ' + busyIndicator)
filehelper.say('auto reply: ' + busyAnnouncement)
} else if (text === '#free') {
busyIndicator = false
filehelper.say('auto reply stopped.')
} else if (/^#busy/i.test(text)) {
busyIndicator = true
filehelper.say('in busy mode: ' + 'ON')
const matches = text.match(/^#busy (.+)$/i)
if (!matches || !matches[1]) {
filehelper.say('auto reply message: "' + busyAnnouncement + '"')
} else {
busyAnnouncement = matches[1]
filehelper.say('set auto reply to: "' + busyAnnouncement + '"')
}
}
return
}
if (sender.type() !== bot.Contact.Type.Personal) {
return
}
if (!busyIndicator) {
return // free
}
if (msg.self()) {
return
}
/**
* 1. Send busy anoncement to contact
*/
if (!room) {
msg.say(busyAnnouncement)
return
}
/**
* 2. If there's someone mentioned me in a room,
* then send busy annoncement to room and mention the contact who mentioned me.
*/
const contactList = await msg.mention()
const contactIdList = contactList.map(c => c.id)
if (contactIdList.includes(this.userSelf().id)) {
msg.say(busyAnnouncement, sender)
}
})
bot.start()
.catch(e => console.error(e))
{
"name": "wechaty",
"version": "0.15.172",
"version": "0.15.173",
"description": "Wechat for Bot(Personal Account)",
"main": "dist/src/index.js",
"typings": "dist/src/index.d.ts",
......
......@@ -455,9 +455,6 @@ export class PuppetPadchat extends Puppet {
/**
* Update Room Payload to new Topic
*/
// const updateRoomPayload = await this.roomPayload(roomId)
// updateRoomPayload.topic = newTopic
// this.cacheRoomPayload.set(roomId, updateRoomPayload)
await this.roomPayloadDirty(roomId)
this.emit('room-topic', roomId, newTopic, oldTopic, changerId)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册