Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use "wechaty-puppet-padchat" cannot login successfully #1600

Closed
tianshunjian opened this issue Sep 19, 2018 · 1 comment
Closed

use "wechaty-puppet-padchat" cannot login successfully #1600

tianshunjian opened this issue Sep 19, 2018 · 1 comment

Comments

@tianshunjian
Copy link

tianshunjian commented Sep 19, 2018

I clone the "wechaty-getting-started", and modify the init code of Wechaty in "examples/starter-bot.js". Then in the console, I run "npm install" and "npm start". However, after I scan the qrcode, I got nothing in the console. After a while, I got status -106. And I found issue #176, and it mentions "puppet-padchat-patch" can solve the -106 issue. "puppet-padchat-patch" can show me a new qrcode, but I cannot login after I scan the new qrcode.

My codes as below.

const { Wechaty } = require('wechaty')
const { PadchatPatch } = require('puppet-padchat-patch')

const TOKEN = 'myToken'; // the token I applied
const initialName = 'test';

const puppet = 'wechaty-puppet-padchat'
const puppetOptions = {
    token: TOKEN
}

const bot = new Wechaty({
    initialName,
    puppet,
    puppetOptions,
});
bot.on('scan',    onScan)
bot.on('login',   onLogin)
bot.on('logout',  onLogout)
bot.on('message', onMessage)
bot.start()
.then(() => console.log('Starter Bot Started.'))
.catch(e => console.error(e))

function onScan (qrcode, status) {
  const qrcodeImageUrl = [
    'https://api.qrserver.com/v1/create-qr-code/?data=',
    encodeURIComponent(qrcode),
  ].join('')
  console.log(qrcodeImageUrl)
}

function onLogin (user) {
  console.log(`${user} login`)
}

function onLogout(user) {
  console.log(`${user} logout`)
}

async function onMessage (msg) {
  console.log(msg.toString())
}

/**
 * Hook the -106 failure status here, try to get auth data from separate server
 */
process.on('unhandledRejection', (error) => {
    const { message } = error
    if (message && message.indexOf('unknown status: -106') !== -1) {
        const wxid = message.split(' ').slice(-1)[0]
        const padchatPatch = new PadchatPatch(TOKEN, initialName, wxid)
        padchatPatch
            .on('scan', (qrcode, status) => {
                // You need to let the user scan the qrcode again here
                // generate(qrcode, { small: true })
                const qrcodeImageUrl = [
                    'https://api.qrserver.com/v1/create-qr-code/?data=',
                    encodeURIComponent(qrcode),
                ].join('')
                console.log(`[${status}] ${qrcodeImageUrl}\nScan QR Code above to log in: `)
            })
            .on('finish', async () => {
                // restart your application here
                bot.start()
            })
            .start()
        return
    }
})
@huan
Copy link
Member

huan commented Sep 19, 2018

Please move this issue to wechaty-puppet-padchat because it's related to a specific puppet provider instead of Wechaty itself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants