Skip to content

Commit

Permalink
Add hot-import-bot example. Fixing wechaty#923
Browse files Browse the repository at this point in the history
  • Loading branch information
xinbenlv committed Oct 29, 2017
1 parent fa966db commit 568cf2b
Show file tree
Hide file tree
Showing 10 changed files with 158 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,7 @@ tags
*.bak
package-lock.json
.babel.json

.idea
example/.config
example/.pki
1 change: 1 addition & 0 deletions example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
7 changes: 7 additions & 0 deletions example/hot-import-bot/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
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)
53 changes: 53 additions & 0 deletions example/hot-import-bot/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* Wechaty - https://github.com/chatie/wechaty
*
* @copyright 2016-2017 Huan LI <[email protected]>
*
* 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)
})
21 changes: 21 additions & 0 deletions example/hot-import-bot/listener/friend.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Wechaty - https://github.com/chatie/wechaty
*
* @copyright 2016-2017 Huan LI <[email protected]>
*
* 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}`)
}
21 changes: 21 additions & 0 deletions example/hot-import-bot/listener/login.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Wechaty - https://github.com/chatie/wechaty
*
* @copyright 2016-2017 Huan LI <[email protected]>
*
* 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`)
}
22 changes: 22 additions & 0 deletions example/hot-import-bot/listener/message.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Wechaty - https://github.com/chatie/wechaty
*
* @copyright 2016-2017 Huan LI <[email protected]>
*
* 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}`);
}
23 changes: 23 additions & 0 deletions example/hot-import-bot/listener/scan.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Wechaty - https://github.com/chatie/wechaty
*
* @copyright 2016-2017 Huan LI <[email protected]>
*
* 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);
}
5 changes: 5 additions & 0 deletions example/hot-reload-bot/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
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
2 changes: 1 addition & 1 deletion src/wechaty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ export class Wechaty extends EventEmitter implements Sayable {
/**
* Quit the bot
*
* @deprecated
* @deprecated use stop() instead
* @returns {Promise<void>}
* @example
* await bot.quit()
Expand Down

0 comments on commit 568cf2b

Please sign in to comment.