提交 876cba72 编写于 作者: Z Zainan Victor Zhou 提交者: Huan (李卓桓)

Add hot-import-bot example. Fixing #923 (#935)

上级 f17303b9
......@@ -67,3 +67,7 @@ tags
*.bak
package-lock.json
.babel.json
.idea
example/.config
example/.pki
......@@ -12,6 +12,7 @@
| room-bot.ts | Manage Chat Room |
| speech-to-text-bot.ts | Convert Voice Message to Text |
| tuling123-bot.ts | Answer Any Question |
| hot-import-bot | Use hot-import for updating code without restarting program |
Learn more about Wechaty from:
1. API Document: <https://github.com/Chatie/wechaty/wiki/API>
......
This directory is an example of how to use `hot-import`
feature introduced in [this commit](https://github.com/Chatie/wechaty/commit/c47715b4470e7ade9a2590fd3e66985dd7977622).
The hot-import is based on an npm package [hot-import](https://www.npmjs.com/package/hot-import)
Not to be confused by the directory `../hot-reload-bot` which is a hand written
hot reload approach proposed by [Gcaufy](https://github.com/Gcaufy)
/**
* Wechaty - https://github.com/chatie/wechaty
*
* @copyright 2016-2017 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 */
const finis = require('finis')
/**
* Change `import { ... } from '../'`
* to `import { ... } from 'wechaty'`
* when you are running with Docker or NPM instead of Git Source.
*/
import {
config,
Wechaty,
log,
} from '../';
const bot = Wechaty.instance({profile: config.default.DEFAULT_PROFILE})
bot
.on('login', 'listener/login')
.on('friend', 'listener/friend')
.on('scan', 'listener/scan')
.on('message', 'listener/message')
bot.start()
.catch(e => {
log.error('Bot', 'init() fail: %s', e)
bot.quit()
process.exit(-1)
})
finis((code, signal) => {
const exitMsg = `Wechaty exit ${code} because of ${signal} `
console.log(exitMsg)
bot.say(exitMsg)
})
/**
* Wechaty - https://github.com/chatie/wechaty
*
* @copyright 2016-2017 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.
*
*/
export default async function (contact, request) {
console.log(`contact = ${contact}`)
}
/**
* Wechaty - https://github.com/chatie/wechaty
*
* @copyright 2016-2017 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.
*
*/
export default function (user) {
console.log(`${user} login`)
}
/**
* Wechaty - https://github.com/chatie/wechaty
*
* @copyright 2016-2017 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.
*
*/
export default function (message) {
console.log(`message = ${message}`);
}
/**
* Wechaty - https://github.com/chatie/wechaty
*
* @copyright 2016-2017 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.
*
*/
export default function (url, code) {
const loginUrl = url.replace('qrcode', 'l');
require('qrcode-terminal').generate(loginUrl);
console.log(url);
}
Hand written hot-reload approach proposed by
[Gcaufy](https://github.com/Gcaufy), not to be confused by
the approach supported by npm package
[hot-import](https://www.npmjs.com/package/hot-import),
see `../hot-import-bot` for example
......@@ -445,7 +445,7 @@ export class Wechaty extends EventEmitter implements Sayable {
/**
* Quit the bot
*
* @deprecated
* @deprecated use stop() instead
* @returns {Promise<void>}
* @example
* await bot.quit()
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册