Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
this.wechaty.on('message', async msg => {
log.verbose('WechatyAdapter', 'listen() wechaty.on(message) %s', msg)
const activity = await this.buildActivity(msg)
if (!activity) {
// should skip this message
log.verbose('WechatyAdapter', 'listen() wechaty.on(message) no activity')
return
}
try {
await this.runMiddleware(
new TurnContext(this, activity),
logic,
)
} catch (err) {
throw err
}
this.wechaty.on('message', async msg => {
log.verbose('WechatyAdapter', 'listen() wechaty.on(message) %s', msg)
const activity = await this.buildActivity(msg)
if (!activity) {
// should skip this message
log.verbose('WechatyAdapter', 'listen() wechaty.on(message) no activity')
return
}
try {
await this.runMiddleware(
new TurnContext(this, activity),
logic,
)
} catch (err) {
throw err
}
})
public async listen (
logic: (context: TurnContext) => Promise,
): Promise<() => Promise> {
log.verbose('WechatyAdapter', 'listen()')
await this.wechaty.start()
this.wechaty.on('message', async msg => {
log.verbose('WechatyAdapter', 'listen() wechaty.on(message) %s', msg)
const activity = await this.buildActivity(msg)
if (!activity) {
// should skip this message
log.verbose('WechatyAdapter', 'listen() wechaty.on(message) no activity')
return
}
try {
await this.runMiddleware(
new TurnContext(this, activity),
if (token) {
log.info('Wechaty', 'TOKEN: %s', token)
bot = Wechaty.instance({ profile: token })
const ioClient = new IoClient({
token,
wechaty: bot,
})
ioClient.start().catch(e => {
log.error('Wechaty', 'IoClient.init() exception: %s', e)
bot.emit('error', e)
})
} else {
log.verbose('Wechaty', 'TOKEN: N/A')
bot = Wechaty.instance()
}
bot
.on('scan', (qrcode, status) => {
qrTerm.generate(qrcode, { small: true })
console.log(`${status}: ${qrcode} - Scan QR Code of the url to login:`)
})
.on('logout' , user => log.info('Bot', `${user.name()} logouted`))
.on('error' , e => log.info('Bot', 'error: %s', e))
.on('login', async function(user) {
const msg = `${user.name()} logined`
log.info('Bot', msg)
await this.say(msg)
room.on('join', function(inviteeList, inviter) {
log.verbose('Bot', 'Room EVENT: join - "%s", "%s"',
inviteeList.map(c => c.name()).join(', '),
inviter.name(),
)
console.log('room.on(join) id:', this.id)
checkRoomJoin.call(this, room, inviteeList, inviter)
})
log.info('Bot', 'createDingRoom("%s")', contact)
try {
const helperContact = await getHelperContact()
if (!helperContact) {
log.warn('Bot', 'getHelperContact() found nobody')
await contact.say(`You don't have a friend called "${HELPER_CONTACT_NAME}",
because create a new room at least need 3 contacts, please set [HELPER_CONTACT_NAME] in the code first!`)
return
}
log.info('Bot', 'getHelperContact() ok. got: "%s"', helperContact.name())
const contactList = [contact, helperContact]
log.verbose('Bot', 'contactList: "%s"', contactList.join(','))
await contact.say(`There isn't ding room. I'm trying to create a room with "${helperContact.name()}" and you`)
const room = await bot.Room.create(contactList, 'ding')
log.info('Bot', 'createDingRoom() new ding room created: "%s"', room)
await room.topic('ding - created')
await room.say('ding - created')
return room
} catch (e) {
log.error('Bot', 'getHelperContact() exception:', e.stack)
throw e
}
}
msg: Message,
): Promise> {
log.verbose('WechatyAdapter', 'buildActivity(%s)', msg)
const from = msg.from()
if (!from) {
throw new Error('WechatyAdapter processMessage() discard message without a from contact: ' + msg)
}
if (msg.self()
|| msg.room()
|| (from && from.type() !== this.wechaty.Contact.Type.Personal)
|| (msg.type() !== this.wechaty.Message.Type.Text)
) {
log.verbose('WechatyAdapter', 'buildActivity(%s) message from self or room or not text, return null', msg)
return null
}
const botUser = this.wechaty.userSelf()
const room = msg.room()
const reference = {
bot: {
id : botUser.id,
name : botUser.name(),
},
channelId: 'wechaty',
conversation: {
id : (room && room.id) || 'conversation',
isGroup : !!room,
name : (room && await room.topic()) || 'Conversation',
.on('logout', user => log.verbose('WechatyAdapter', `${user.name()} logouted`))
.on('login', user => {