wechaty: Cannot create an instance of the abstract class 'FriendRequest

i am trying to test FriendRequest in ``` example/ding-dong-bot.js`` , error occurs like that:

example/ding-dong-bot.ts (49,19): Cannot create an instance of the abstract class 'FriendRequest'. (2511)

code: example/ding-dong-bot.js :

/**
 *
 * Wechaty - Wechat for Bot
 *
 * Connecting ChatBots
 * https://github.com/wechaty/wechaty
 *
 */

/* tslint:disable:variable-name */
const QrcodeTerminal = require('qrcode-terminal')

import {
  Wechaty
  , Config
  , log
  , FriendRequest
} from '../'

const welcome = `
| __        __        _           _
| \\ \\      / /__  ___| |__   __ _| |_ _   _
|  \\ \\ /\\ / / _ \\/ __| '_ \\ / _\` | __| | | |
|   \\ V  V /  __/ (__| | | | (_| | |_| |_| |
|    \\_/\\_/ \\___|\\___|_| |_|\\__,_|\\__|\\__, |
|                                     |___/

=============== Powered by Wechaty ===============
-------- https://github.com/zixia/wechaty --------

I'm a bot, my super power is talk in Wechat.

If you send me a 'ding', I will reply you a 'dong'!
__________________________________________________

Hope you like it, and you are very welcome to
upgrade me for more super powers!

Please wait... I'm trying to login in...

`

console.log(welcome)
const bot = Wechaty.instance({ profile: Config.DEFAULT_PROFILE })

bot
.on('login'	  , user => {
  log.info('Bot', `${user.name()} logined`)
  const request = new FriendRequest()
  console.log('loging....................................')
  console.log(request)
})
.on('logout'	, user => log.info('Bot', `${user.name()} logouted`))
.on('error'   , e => log.info('Bot', 'error: %s', e))
.on('scan', (url, code) => {
  if (!/201|200/.test(String(code))) {
    let loginUrl = url.replace(/\/qrcode\//, '/l/')
    QrcodeTerminal.generate(loginUrl)
  }
  console.log(`${url}\n[${code}] Scan QR Code in above url to login: `)
})
.on('message', m => {
  try {
    const room = m.room()
    console.log((room ? '[' + room.topic() + ']' : '')
                + '<' + m.from().name() + '>'
                + ':' + m.toStringDigest()
    )

    if (/^(ding|ping|bing)$/i.test(m.content()) && !m.self()) {
      m.say('dong')
      log.info('Bot', 'REPLY: dong')
    }
  } catch (e) {
    log.error('Bot', 'on(message) exception: %s' , e)
  }
})

bot.init()
.catch(e => {
  log.error('Bot', 'init() fail: %s', e)
  bot.quit()
  process.exit(-1)
})

function logToFile(data) {
  require('fs').appendFile('message.log', data + '\n\n#############################\n\n', err => {
    if (err) { log.error('LogToFile: %s', err) }
  })
}
if (typeof logToFile === 'fasdfsd') {
  console.log('disable linting warning')
}

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 28 (28 by maintainers)

Commits related to this issue

Most upvoted comments

ah, PR closes this by mistake…

after collaborating with @JasLin on Cloud9 IDE, finally, we had caught the bug together, haha!

TL;DR

this problem will occur if match the following two conditions:

  1. you use ts-node to run wechaty inside wechaty source directory, and
  2. you had a dist directory with compiled javascript source code. (if you run npm run build)

then the module system will load src/config.ts module twice, then we will have two instances of Config, and the _puppetInstance will be unpredictable, because we do not know which one we are using.

Details

1. let’s put those two lines in the beginning of src/config.ts

console.log('\nconfig.ts imported!!!!!!!!!!!!!!' + __dirname + '\n')
console.log(new Error().stack )

this code will log when Config module is imported.

2. run npm run build to get a dist directory full of javascript

$ npm run build

3. run our code by ts-node

$ npm run ts-node example/zixia.ts

> wechaty@0.5.1 ts-node /home/ubuntu/workspace
> ts-node "example/zixia.ts"


config.ts imported!!!!!!!!!!!!!!/home/ubuntu/workspace/dist/src

Error
    at Object.<anonymous> (/home/ubuntu/workspace/src/config.ts:8:13)
    at Module._compile (module.js:573:32)
    at Object.Module._extensions..js (module.js:582:10)
    at Module.load (module.js:490:32)
    at tryModuleLoad (module.js:449:12)
    at Function.Module._load (module.js:441:3)
    at Module.require (module.js:500:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/home/ubuntu/workspace/index.ts:1:1)
    at Module._compile (module.js:573:32)

config.ts imported!!!!!!!!!!!!!!/home/ubuntu/workspace/src

Error
    at Object.<anonymous> (/home/ubuntu/workspace/src/config.ts:8:13)
    at Module._compile (module.js:573:32)
    at Module.m._compile (/home/ubuntu/workspace/node_modules/ts-node/src/index.ts:406:23)
    at Module._extensions..js (module.js:582:10)
    at Object.require.extensions.(anonymous function) [as .ts] (/home/ubuntu/workspace/node_modules/ts-node/src/index.ts:409:12)
    at Module.load (module.js:490:32)
    at tryModuleLoad (module.js:449:12)
    at Function.Module._load (module.js:441:3)
    at Module.require (module.js:500:17)
    at require (internal/module.js:20:19)

Solution

Will dig it deeper later. now the best way to avoid this is:

  1. if use ts-node, then rm -fr dist first
  2. or run node example/zixia.js inside dist directory.

Thank you @JasLin, Cheers!