diff --git a/.gitignore b/.gitignore index 8fa552330..fbc3c9331 100644 --- a/.gitignore +++ b/.gitignore @@ -67,3 +67,7 @@ tags *.bak package-lock.json .babel.json + +.idea +example/.config +example/.pki diff --git a/example/README.md b/example/README.md index c4d51fbd2..6b5f3bc0a 100644 --- a/example/README.md +++ b/example/README.md @@ -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: diff --git a/example/hot-import-bot/Readme.md b/example/hot-import-bot/Readme.md new file mode 100644 index 000000000..671ca807c --- /dev/null +++ b/example/hot-import-bot/Readme.md @@ -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) diff --git a/example/hot-import-bot/index.ts b/example/hot-import-bot/index.ts new file mode 100644 index 000000000..f096512a9 --- /dev/null +++ b/example/hot-import-bot/index.ts @@ -0,0 +1,53 @@ +/** + * Wechaty - https://github.com/chatie/wechaty + * + * @copyright 2016-2017 Huan LI + * + * 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) +}) diff --git a/example/hot-import-bot/listener/friend.ts b/example/hot-import-bot/listener/friend.ts new file mode 100644 index 000000000..cd053bffa --- /dev/null +++ b/example/hot-import-bot/listener/friend.ts @@ -0,0 +1,21 @@ +/** + * Wechaty - https://github.com/chatie/wechaty + * + * @copyright 2016-2017 Huan LI + * + * 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}`) +} diff --git a/example/hot-import-bot/listener/login.ts b/example/hot-import-bot/listener/login.ts new file mode 100644 index 000000000..c34251a9a --- /dev/null +++ b/example/hot-import-bot/listener/login.ts @@ -0,0 +1,21 @@ +/** + * Wechaty - https://github.com/chatie/wechaty + * + * @copyright 2016-2017 Huan LI + * + * 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`) +} diff --git a/example/hot-import-bot/listener/message.ts b/example/hot-import-bot/listener/message.ts new file mode 100644 index 000000000..a8b48260b --- /dev/null +++ b/example/hot-import-bot/listener/message.ts @@ -0,0 +1,22 @@ +/** + * Wechaty - https://github.com/chatie/wechaty + * + * @copyright 2016-2017 Huan LI + * + * 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}`); +} diff --git a/example/hot-import-bot/listener/scan.ts b/example/hot-import-bot/listener/scan.ts new file mode 100644 index 000000000..cc504043e --- /dev/null +++ b/example/hot-import-bot/listener/scan.ts @@ -0,0 +1,23 @@ +/** + * Wechaty - https://github.com/chatie/wechaty + * + * @copyright 2016-2017 Huan LI + * + * 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); +} diff --git a/example/hot-reload-bot/Readme.md b/example/hot-reload-bot/Readme.md new file mode 100644 index 000000000..a5345ce17 --- /dev/null +++ b/example/hot-reload-bot/Readme.md @@ -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 diff --git a/src/wechaty.ts b/src/wechaty.ts index 329c87b03..caef2e8a7 100644 --- a/src/wechaty.ts +++ b/src/wechaty.ts @@ -444,7 +444,7 @@ export class Wechaty extends EventEmitter implements Sayable { /** * Quit the bot * - * @deprecated + * @deprecated use stop() instead * @returns {Promise} * @example * await bot.quit()