Wechaty
NewsBlogDevelopersDocs
中文版
中文版
  • 介绍
  • 快速开始
  • API 文档
    • Wechaty
    • Message
    • Contact
    • ContactSelf
    • Room
    • RoomInvitation
    • Friendship
  • 示例代码
  • Puppet
  • 常见问题
  • 最佳实践
  • 参与贡献
  • 了解更多
    • 视频教程
    • 进阶
    • 测试
    • 目录结构
    • ReleaseLog
    • ChangeLog
    • Awesome Wechaty
    • Contributors
    • 关于token的所有问题
Powered by GitBook
On this page
  • 1. 规则
  • 2. 使用docker 运行
  • 2.1 使用 JavaScript 运行
  • 2.2 使用TypeScript 运行
  • 3. 机器人启动方法
  • 4. 为方法调用设置合理间隔
  • 5. 日志说明
  • 6. 使用热加载
  • 7. 推荐版本
  • 8. 环境要求
  • 9. 相关拓展包说明
  • 10. 代码示例

Was this helpful?

最佳实践

Previous常见问题Next参与贡献

Last updated 5 years ago

Was this helpful?

1. 规则

  • 使用TypeScript 作为开发语言,TypeScript 是强类型的JavaScript,统一typings,增强系统的可维护性。更多请查看issue 讨论:, .

  • 使用TSLint 作为代码检查工具,为什么需要?。wechaty 的tslint 配置说明请参考。

  • 使用 作为编译器。

  • 文件的命名规则是小写,用- 连接所有的内容而不是空格。比如2017-10-06-wechat-pc-impactor 而不是 2017-10-06-WeChat PC Impactor

  • 变量命名为小驼峰写法。如 userName 而不是 user_name。

2. 使用docker 运行

最新版的 Wechaty Docker Image (v0.22)预装了所有的 puppet ,包括 puppeteer 和 padchat 等代码里面预设的所有.

Wechaty Docker 同时支持JavaScript 和TypeScript。因为我们使用 ts-node 运行代码,所以你不需要编译,只需要将拓展名改为.ts即可使用TypeScript。

2.1 使用 JavaScript 运行

# for JavaScript
docker run -ti --rm --volume="$(pwd)":/bot zixia/wechaty mybot.js

2.2 使用TypeScript 运行

# for TypeScript
docker run -ti --rm --volume="$(pwd)":/bot zixia/wechaty mybot.ts

3. 机器人启动方法

import { PuppetPadchat } from 'wechaty-puppet-padchat'
const puppet = new PuppetPadchat()
const bot = new Wechaty({ 
    puppet,
    name: 'your-bot-name'
})

进一步说明:在启动的时候设置wechaty 的 name(旧版本叫profile) 来存储机器人的登陆信息,登陆后会自动生成一个*.memory-card.json 的文件,这样再次运行的时候,就不需要扫码就可以直接登陆机器人了。

4. 为方法调用设置合理间隔

我们也建议开发者在调用wechaty 方法的时候,也自行设置间隔,数值建议如下:

  • 发送消息:1s

  • 修改备注:10s

  • 添加好友: 5min

  • 自动通过好友请求:1min

  • 持续更新中。。。

5. 日志说明

  • silent

  • error

  • warn

  • info

  • verbose

  • silly

有以下两种方式来设置LOG 的级别:

  1. 可以通过设置环境变量 `WECHATY_LOG` 的方式设置LOG的级别

  2. 在代码中设置:

import { log } from 'wechaty'
log.level('silly') 
// 'silent' | 'error' | 'warn' | 'info' | 'verbose' | 'silly'

6. 使用热加载

docker运行的bot文件如何debug?像nodemon那样?

7. 推荐版本

如何理解Wechaty 及相关Puppet 的版本号

简单回答:

次要版本号是偶数数字是生产版本。

详细回答:

数字的规则:

  1. 偶数版本,如0.8,0.12,是用于生产环境的

  2. 奇数版本,如0.11,0.13,是发布的开发版本

偶数版本 例子: (用于生产环境)

  • 0.16.1

  • 0.16.2

  • 1.0.1

  • 1.0.2

技术版本 例子: (用于开发环境)

  • 0.15.1

  • 0.15.2

  • 1.1.1

  • 1.1.2

同时,只要代码通过了Travis CI 的自动化测试,我们会发布所有版本的NPM包。

8. 环境要求

  • Node.js 版本高于 10

9. 相关拓展包说明

10. 代码示例

了解更多:.

为了防止微信封号,wechaty 内置了队列,详细可见:​

wechaty 使用了 作为日志工具,默认打印的级别是`verbose`, brolog 共有的级别从高到低分别为:

wechaty 提供了hot-import 模块,参考:​

Wechaty 根据 的规则制定版本号,并使用次要版本号来发布的版本是生产版本还是开发版本。

参考 和 , 当语义版本的次要版本号是技术的时候,意味着它是开发分支,建议不要上生产环境。

如果想了解更多:​

同时支持, , 和 多个平台。windows 用户如果安装失败,请参考这篇博客:​

:Memory Card is an Easy to Use Key/Value Store, with Swagger API Backend & Serialization Support.

: Brolog is Logger for Angular in Browser like Npmlog.

: Easy to Use ReactiveX Queue that Supports Delay/DelayExector/Throttle/Debounce Features Powered by RxJS.

:Pack a File into Box for easy move/transfer between servers no matter of where it is.(local, remote url, or cloud storage)

: Hot Module Replacement(HMR) for Node.js

: FlashStore is a Key-Value persistent storage with easy to use ES6 Map-like API(both Async and Sync support), powered by LevelDB and TypeScript.

: State Switch is a Monitor/Guard for Managing Your Async Operations.

: An Timer used to Detect and Recover from Malfunctions

: Hook node exit with your callback, get exit code and signal name from parameters

: Pure C++ Node.js LevelDB binding serving as the back-end to LevelUP

Wiki:Docker
rx-queue
brolog
https://github.com/Chatie/wechaty-getting-started/tree/master/examples/professional/hot-import-bot
http://semver.org/
wechaty issue #905
wechaty issue 1158
How to Understand the Wechaty Semantic Versioning?
Linux
Windows
Darwin(OSX/Mac)
Docker
在Windows10下安装Wechaty
memory-card
brolog
rx-queue
file-box
hot-import
flash-store
state-switch
watchdog
finis
leveldown
示例代码
#1066
#1064
推荐阅读这个了解详情
这篇博客
VSCode
Docker Pulls
Docker Layers
GitHub - wechaty/docker-getting-started: Getting Started Template for Docker UsersGitHub
Logo