Description
Enter verify code to login to wecom token
Recently, some wecom account requires a verify-code to login to desktop client. It looks like this:
To make these accounts usable, we implemented verify-code event and verify-code entering method in @juzi/wechaty ASAP. You should note that currently we can only ensure the experience of node version wechaty. We don't have enough resources to push to iterate for other versions. To make full use of your token, we recommend you to use node version of wechaty. ( @juzi/wechaty has only node version provided. )
Dependencies
@juzi/[email protected]
@juzi/[email protected]
@juzi/[email protected]
Types
export enum VerifyCodeStatus {
UNKNOWN = 0,
WAITING = 1, // Waiting for input
EXPIRED = 2, // Not implemented yet, now you should judge from the error message of verify-code enter method
}
export enum VerifyCodeScene {
UNKNOWN = 0,
LOGIN = 1,
}
type WechatyEventListenerVerifyCode = (
id: string, // verify-code id. When logging in, it is the same with the key of the qr code
message: string, // message, currently fixed to '请输入企业微信手机端展示的验证码,以继续登录'
scene: PUPPET.types.VerifyCodeScene, // scene, now the only scene is LOGIN
status: PUPPET.types.VerifyCodeStatus // scene, now the only status implemented is WAITING
) => void | Promise<void>
// listen to the event
bot.on('verify-code', async (id: string, message: string, scene: types.VerifyCodeScene, status: types.VerifyCodeStatus) => {
// code here
})
Methods
async enterVerifyCode (
id: string,
code: string,
): Promise<void> // enter verify-code
async cancelVerifyCode (
id: string,
): Promise<void> // cancel verify-code ( will cause the qrcode to refresh )
async refreshQrCode (): Promise<void> // force qrcode to refresh
Notice
When the verify-code
event is pushed, the scan
event will continue to be pushed ( status
4 Confirmed
)。bot developer should check the id
of verify-code
event, and check it with the qrcode
of scan
event. qrcode
is string like https://wx.work.weixin.qq.com/cgi-bin/crtx_auth?key=${qrcodeKey}&wx=1
. The qrcodeKey
and the id
of verify-code
event should be the same. If not, it means a new qrcode has been pushed and you should scan again.
Example
Entering verify-code
const store = {}
bot.on('scan', (qrcode: string, status: types.ScanStatus) => {
if (status === types.ScanStatus.Waiting) {
// New qrcode
store.qrcodeKey= getQrcodeKey(qrcode)
}
}).on('verify-code', async (id: string, message: string, scene: types.VerifyCodeScene, status: types.VerifyCodeStatus) => {
if (status === types.VerifyCodeStatus.WAITING && scene === types.VerifyCodeScene.LOGIN && id === store.qrcodeKey) {
const verifyCode = await getVerifyCode() // get verify-code from some async methods, e.g. console input
try {
await bot.enterVerifyCode(id, verifyCode) // if no error was caught, it means the verify-code is correct, and the login event should be expected
return
} catch (e) {
console.log(e.message)
// if there is error, please handle according to the message. Currently you can input 3 times.
// message keyword: 验证码错误输入错误,请重新输入
// message keyword: 验证码错误次数超过阈值,请重新扫码
// currently no ```EXPIRED``` event will be pushed, you should decided from the error message.
}
}
})
Refresh Qrcode
await bot.cancelVerifyCode(store.qrcodeKey) // cancel the verify-code according to the key. You'll need to scan again, instead of just refresh the verify-code
await bot.refreshQrCode() // force refresh qrcode
Misc
Please note that the corresponding doc are subjected to change without editing this issue. Please follow the Feishu Doc. Please also expect our TSS (Token Self Service) platform, where you can buy and renew token, and also get latest doc. Coming soon.
Working Example
Please checkout this codespace
Demo Code
Please checkouot this repo workpro-getting-started.