Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function onScan (qrcode: string, status: ScanStatus) {
generate(qrcode) // show qrcode on console
const qrcodeImageUrl = [
'https://api.qrserver.com/v1/create-qr-code/?data=',
encodeURIComponent(qrcode),
].join('')
log.info('StarterBot', '%s(%s) - %s', ScanStatus[status], status, qrcodeImageUrl)
}
function doExit(code: number): void {
log.info('Bot', 'doExit(%d)', code)
if (killChrome) {
killChrome('SIGINT')
}
process.exit(code)
}
async function manageDingRoom() {
log.info('Bot', 'manageDingRoom()')
/**
* Find Room
*/
try {
const room = await bot.Room.find({ topic: /^ding/i })
if (!room) {
log.warn('Bot', 'there is no room topic ding(yet)')
return
}
log.info('Bot', 'start monitor "ding" room join/leave/topic event')
/**
* Event: Join
*/
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)
})
/**
* Event: Leave
*/
const contact = contactList[i]
/**
* Save avatar to file like: "1-name.jpg"
*/
const file = await contact.avatar()
const name = file.name
await file.toFile(name, true)
log.info('Bot', 'Contact: "%s" with avatar file: "%s"',
contact.name(),
name,
)
if (i > MAX) {
log.info('Bot', 'Contacts too many, I only show you the first %d ... ', MAX)
break
}
}
const SLEEP = 7
log.info('Bot', 'I will re-dump contact weixin id & names after %d second... ', SLEEP)
setTimeout(main, SLEEP * 1000)
}
log.info('Bot', `personal ${i}: ${contact.name()} : ${contact.id}`)
}
}
const MAX = 17
for (let i = 0; i < contactList.length; i++ ) {
const contact = contactList[i]
/**
* Save avatar to file like: "1-name.jpg"
*/
const file = await contact.avatar()
const name = file.name
await file.toFile(name, true)
log.info('Bot', 'Contact: "%s" with avatar file: "%s"',
contact.name(),
name,
)
if (i > MAX) {
log.info('Bot', 'Contacts too many, I only show you the first %d ... ', MAX)
break
}
}
const SLEEP = 7
log.info('Bot', 'I will re-dump contact weixin id & names after %d second... ', SLEEP)
setTimeout(main, SLEEP * 1000)
}
async function getOutRoom(contact, room) {
log.info('Bot', 'getOutRoom("%s", "%s")', contact, room)
try {
await room.say('You said "ding" in my room, I will remove you out.')
await room.del(contact)
} catch (e) {
log.error('Bot', 'getOutRoom() exception: ' + e.stack)
}
}
async function createDingRoom(contact) {
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
.then(() => log.info('StarterBot', 'Starter Bot Started.'))
.catch(e => log.error('StarterBot', e))
function onLogin (user: Contact) {
log.info('StarterBot', '%s login', user)
}
async function main() {
const contactList = await bot.Contact.findAll()
log.info('Bot', '#######################')
log.info('Bot', 'Contact number: %d\n', contactList.length)
/**
* official contacts list
*/
for (let i = 0; i < contactList.length; i++) {
const contact = contactList[i]
if (contact.type() === Contact.Type.Official) {
log.info('Bot', `official ${i}: ${contact}`)
}
}
/**
* personal contact list
*/