Migrating Wechaty v0.14 to v0.18 Guide - From Puppeteer To Padchat

Wechaty v0.16 更新迁移指南
本文档详细介绍了如何将基于 Wechaty 的应用从 Web 解决方案迁移到 iPad 解决方案,包括代码迁移步骤、环境变量设置及 TypeScript 代码检查等关键信息。

Wechaty has been updated to version 0.16(BETA) these days and begin to support all kinds of wechat solutions including web, ipad, ios, etc. My product based on wechaty has to migration from wechaty solution based on web to ipad solution.

This blog introduces how to porting code from wechaty puppeteer to padchat.

code

1. Puppeteer VS Padchat

Puppet System means to connect Wechaty API to any kinds of Puppets, including Web, iPad, Android, Windows Hook and ios. Different implements of these puppets are totally different, without the puppet, using one API to bridge all implement is very difficult, so we had Puppet System.

Puppet

  • Puppeteer: a solution based on Web Wechat
  • Padchat: a solution based on Ipad Wechat

You can find more in this blog: Wechaty New Version 0.16(BETA, with superpower) Released

2. How to run puppet-padchat

Wechaty will start with puppeteer(web solution) by default, if you want to change to padchat, you need to set the environmental variable.

See more in wechaty wiki: How to run a new wecahty-puppet-padchat

Start with Github repo

1. Pull the latest code
git pullrm -rf package-lock.json
rm -rf node_modules/
npm install
2. Get Token

Participate in our alpha test here: Wechaty v0.17 Padchat Testing: Win32/iPad/Android/iOS/API Puppets Support are coming! and Get WECHATY_PUPPET_PADCHAT_TOKEN

3. Set environment variable and run

Remeber to set WECHATY_PUPPET=padchat to change from puppeteer to padchat

WECHATY_PUPPET_PADCHAT_TOKEN=your padchat token WECHATY_PUPPET=padchat  node examples/ding-dong-bot.js

NPM

1. Install
npminstall wechaty@next
2. Get token

Participate in our alpha test here: Wechaty v0.17 Padchat Testing: Win32/iPad/Android/iOS/API Puppets Support are coming! and Get WECHATY_PUPPET_PADCHAT_TOKEN

3. Set environment variable & run

Remeber to set WECHATY_PUPPET=padchat to change from puppeteer to padchat

WECHATY_PUPPET_PADCHAT_TOKEN=your padchat token WECHATY_PUPPET=padchat   node examples/ding-dong-bot.js

Docker

1. Install Docker
docker pull zixia/wechaty:latest
2. Get Token for Docker

Participate in our alpha test here: Wechaty v0.17 Padchat Testing: Win32/iPad/Android/iOS/API Puppets Support are coming! and Get WECHATY_PUPPET_PADCHAT_TOKEN

3. Set environment variable and Run
  • Remeber to set WECHATY_PUPPET=padchat to change from puppeteer to padchat
  • Remove wechaty in node_module if exist.
docker run-t -i  -e WECHATY_PUPPET="padchat" -e WECHATY_PUPPET_PADCHAT_TOKEN="your token"  --volume="$(pwd)":/bot --name=wechaty zixia/wechaty:latest examples/ding-dong-bot.ts

3. Check Code

Wechaty is written by typescript, all with strong typing. Since to change all wechaty code, I change my logic product to strong typing.

We need tools to help us check our code errors. I use TSLint to help me to check typescript code and use VS CODE as my IDE.

Why we need to check code

Maybe you think JavaScript is so flexible so it needs to be checked, but typescript has been able to check out a lot of problems when compiling. Why do we still need to check the code?

This is because TypeScript focuses on the type of match, not the code style. When there are more and more people on our team, the same logic may be totally different in different people:

  • Four spaces indentation or two spaces indentation?
  • Should we disable var?
  • Should all the interface name begin with I?
  • Should it be mandatory to use === instead of ==?
  • Should we need a semicolon?

TypeScript will not concern these problems, but these affect the efficiency of multi-person collaboration when developing, and how easy the code to read and maintain.

This blog explain why we need to check code in typescript.

In one word, though code errors are more important than the uniform code style when a project becomes bigger and more and more developers join in, the code style constraints are still very important.

Using TSLint

TSLint is an extensible static analysis tool that checks TypeScript code for readability, maintainability, and functionality errors. It is widely supported across modern editors & build systems and can be customized with your own lint rules, configurations, and formatters. Learn more in TSLint Website

1. Install TSLint
npminstall --save-dev tslint
npm install tslint-config-standard
2. Config File

Create config file tslint.json

I suggest to refer wechaty config about tslint and tsconfig

Here are some tips about wehcaty config:

  • “strict” : true Enable all strict type checking options. Enabling –strict enables –noImplicitAny, –noImplicitThis, –alwaysStrict, –strictNullChecks, –strictFunctionTypes and –strictPropertyInitialization
  • “noEmitOnError” : true Do not emit outputs if any errors were reported.
  • “noUnusedLocals” : true Report errors on unused locals.
  • “noImplicitReturns” : true Report error when not all code paths in function return a value.
  • “noFallthroughCasesInSwitch” : true Report errors for fallthrough cases in switch statement.
  • “strictNullChecks” : true In strict null checking mode, the null and undefined values are not in the domain of every type and are only assignable to themselves and any (the one exception being that undefined is also assignable to void).
  • “noImplicitAny” : true Raise error on expressions and declarations with an implied any type. Related issue: ts-node 7.0 breaking change: Skip files by default
  • “no-floating-promises”: true Check the floating promise。Related issue: Prevent the Floating Promise in the Async/Await Code
  • “noUnusedParameters” : true Report errors on unused parameters.。
  • “noImplicitThis” : true Raise error on this expression with an implied any type.

see more in official website

3. Add tslint script for package.json
{
    "scripts": {
        "lint": "tslint --project . src/**/*.ts src/**/*.tsx",
    }
}

-- project . require all tslint to use tsconfig.json configuration of the current directory to get the information.

Then run npm run lint will check the whole code.

4. Add TSLint to VSCode

Search tslint and install a plugin for vscode. It is enabled by default.

5. Sweet Tips

I also recommend another plugin in vscode: editorconfig: EditorConfig helps developers define and maintain consistent coding styles between different editors and IDEs. see more: Official EditorConfig Website

You can find wechaty editorconfig in .editorconfig

4. BREAKING CHANGES

After upgrade wechaty, see more inblog. Although we are trying to minimize API changes, there are some breaking changes. I really suggest you reading CHANGE LOG. Also, I list some important changes here:

bot.init() change to bot.start()

bot.quit() change to bot.stop()

FriendRequest change to Friendship

FriendRequest class refactored.

Before (v0.14 or below)
wechaty.on('friend', (contact, request) => {
  if (!request) {
    // this is a friend request confirmation event
  } else {
    // this is a friend request
  }
})

After (v0.16 or above)
wechaty.on('friendship', request => {
  switch (request.type()) {
    case FriendRequest.Type.RECEIVE:
      // this is a friend request request
      break
    case FriendRequest.Type.CONFIRM:
      // this is a friend request confirmation
      break
  }
})

Related link:

Message.content() change to Message.text()

From v0.16, Message.content() will be deprecated. From v0.18, Message.content() will be removed.

Related Link:

Remove MediaMessage class

From v0.16, MediaMessage will be deprecated. From v0.18, MediaMessage will be removed.

Use Message instead.

Migration Example

- bot.say(new MediaMessage('/image.png')
+ bot.say(new Message('/image.png')

Related issue

A useful tip to send the image

FileBox, FileBox is a virtual container for packing a file data into it for future readers, and easily transport between servers with the least payload, no mater than where it is (local path, remote URL, or cloud storage).

const fileBox = FileBox.fromStream(
fs.createReadStream(BOT_QR_CODE_IMAGE_FILE),
BOT_QR_CODE_IMAGE_FILE,
)

Wechaty self() change to Wechaty.userSelf()

Related issue:

Contact.personal() and Contact.official() change to Contact.type()

Contact.personal() Before
const isPersonal = contact.personal()
const isOfficial = contact.official()
Contact.personal() sAfter
/**
   * Return the type of the Contact
   *
   * @returns ContactType - Contact.Type.PERSONAL for personal account, Contact.Type.OFFICIAL for official account
   * @example
   * const isOfficial = contact.type() === Contact.Type.OFFICIAL
   */
  const type = Contact.type()

Related issue

Room.add() return from Promise <boolean> to Promise<void>

Related issue:

Room.topic() change from Sycn to Async

Room.topic() Before
const topic = room.topic()
Room.topic() After
const topic = await room.topic()

Related issue:

Room.alias(contact) change from Sycn to Async

Room.alias(contact) Before
const alias = room.alias(contact)
Room.alias(contact) After
const alias = await room.alias(contact)

Related issue:

Room.memberList() change from Sycn to Async

Room.memberList() Before
const memberList = room.memberList()
Room.memberList() After
const memberList = await room.memberList()

Related Issue:

Room.member() from sync to async

Room.member() Before
const contact = room.member('Huan')
Room.member() After
- const contact = room.member('Huan')
+ const contact = await room.member('Huan')

Related Issue:

Room.has(contact) change from Sycn to Async

WARNING: This change will let us make more mistakes:

if (room.has(contact)) {
  console.error('here will always be executed because Promise === true')
}
Room.has(contact) Before
const exist = room.has(contact)
Room.has(contact) After
const exist = await room.has(contact)

Related Link:

Message.mention() change from Sync to Async

BREAKING CHANGE: v0.16 Message.mention() change from sync to async

Message.mention() Before
const mentionList = message.mention()
Message.mention() After
- const mentionList = message.mention()
+ const mentionList = await message.mention()

Related issue:

scan Event args will become different

The good news is: the old code seems will run without problem, because it will just do nothing:

const loginUrl = url.replace(/\/qrcode\//, '/l/')

scan Event Before

https://github.com/wechaty/wechaty/blob/860e85ec776ac20e92751ec4b67e0d539ef40a16/examples/ding-dong-bot.ts#L74-L77

scan Event After

https://github.com/wechaty/wechaty/blob/07008dff17ccc46b347ba28b85af167984573ea0/examples/ding-dong-bot.ts#L74-L76

Notice that we deleted the const loginUrl = url.replace(/\/qrcode\//, '/l/')

Related Issues:

Class cannot be instantiated directly

Error Message
  1. Error: the class cannot be instantiated directly!
  2. Error: must not use the global Message/Contact/Room. use a cloned child via clone class instead

Currently, the Contact, FriendRequest, Message, and Room classes will not be able to instantiate directly, because they must attach with a Puppet.

They need to be cloneClass()-ed first, then attach the puppet, and at last they will be ready for use by:

  1. wechaty.Contact, or
  2. puppet.Contact, etc.
~Do Not~
import { Room } from 'wechaty'
const room = await Room.create(...)

The above code will throw an error.

Do
- import { Room } from 'wechaty'
+ import { Wechaty } from 'wechaty'

- const room = await Room.create(...)
+ const wechaty = new Wechaty()
+ const room = await wechaty.Room.create(...)

So does Contact, FriendRequest, and Message.

Related Link

  1. Wechaty Multi-Instance Support #518
  2. NPM clone-class
  3. https://github.com/huan/node-clone-class/issues/5

Related issue

Message.ext() return ‘.ext’ instead of ‘ext’ before

According to the ext() methods behavior in Node/Python/C# etc, the ext() always return the filename extension that including the dot(.).

So the following BREAKING CHANGE was made in v0.15:

const ext = message.ext()
// assume the filename is `test.txt`
Before (v0.14 or earlieer)

assert(ext === 'txt')

After (v0.16 or later)

assert(ext === '.txt')

Related Link

Hmmm… That’s all and wish you can have a good coding experience, thanks!

## 软件功能详细介绍 1. **文本片段管理**:可以添加、编辑、删除常用文本片段,方便快速调用 2. **分组管理**:支持创建多个分组,不同类型的文本片段可以分类存储 3. **热键绑定**:为每个文本片段绑定自定义热键,实现一键粘贴 4. **窗口置顶**:支持窗口置顶功能,方便在其他应用程序上直接使用 5. **自动隐藏**:可以设置自动隐藏,减少桌面占用空间 6. **数据持久化**:所有配置和文本片段会自动保存,下次启动时自动加载 ## 软件使用技巧说明 1. **快速添加文本**:在文本输入框中输入内容后,点击"添加内容"按钮即可快速添加 2. **批量管理**:可以同时编辑多个文本片段,提高管理效率 3. **热键冲突处理**:如果设置的热键与系统或其他软件冲突,会自动提示 4. **分组切换**:使用分组按钮可以快速切换不同类别的文本片段 5. **文本格式化**:支持在文本片段中使用换行符和制表符等格式 ## 软件操作方法指南 1. **启动软件**:双击"大飞哥软件自习室——快捷粘贴工具.exe"文件即可启动 2. **添加文本片段**: - 在主界面的文本输入框中输入要保存的内容 - 点击"添加内容"按钮 - 在弹出的对话框中设置热键和分组 - 点击"确定"保存 3. **使用热键粘贴**: - 确保软件处于运行状态 - 在需要粘贴的位置按下设置的热键 - 文本片段会自动粘贴到当前位置 4. **编辑文本片段**: - 选中要编辑的文本片段 - 点击"编辑"按钮 - 修改内容或热键设置 - 点击"确定"保存修改 5. **删除文本片段**: - 选中要删除的文本片段 - 点击"删除"按钮 - 在确认对话框中点击"确定"即可删除
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值