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
- fix #70 ,it maybe cause by designing the ConfigSetting as an interface, work around it putting the _puppetinstace in to global — committed to JasLin/wechaty by JasLin 8 years ago
- Merge pull request #76 from JasLin/master about issue #70 : prepare to test this pr on cloud9 — committed to wechaty/wechaty by huan 8 years ago
- issue #70 use related module filepath to specific file inner wechaty — committed to wechaty/wechaty by huan 8 years ago
- #70 temporary for users — committed to wechaty/wechaty by huan 8 years ago
- clean(code): remove redundancy code from issue #70 — committed to wechaty/wechaty by huan 7 years ago
- add PuppetMock for unit tests & new puppet quickstart (#69 #70 #237 #400 #1016 #1095) — committed to wechaty/wechaty by huan 6 years ago
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:
ts-nodeto run wechaty inside wechaty source directory, anddistdirectory with compiled javascript source code. (if you runnpm run build)then the module system will load
src/config.tsmodule twice, then we will have two instances ofConfig, and the_puppetInstancewill 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.tsthis code will log when
Configmodule is imported.2. run
npm run buildto get adistdirectory full of javascript3. run our code by ts-node
Solution
Will dig it deeper later. now the best way to avoid this is:
ts-node, thenrm -fr distfirstnode example/zixia.jsinsidedistdirectory.Thank you @JasLin, Cheers!