Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
const { Wechaty } = require('wechaty')
Wechaty.instance() // Singleton
.on('scan', (qrcode, status) => console.log(`Scan QR Code to login: ${status}\nhttps://api.qrserver.com/v1/create-qr-code/?data=${encodeURIComponent(qrcode)}`))
.on('login', user => console.log(`User ${user} logined`))
.on('message', message => console.log(`Message: ${message}`))
.start()
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
const { Wechaty } = require('wechaty') // import Wechaty from 'wechaty'
Wechaty.instance() // Singleton
.on('scan', (qrcode, status) => console.log(`Scan QR Code to login: ${status}\nhttps://api.qrserver.com/v1/create-qr-code/?data=${encodeURIComponent(qrcode)}`))
.on('login', user => console.log(`User ${user} logined`))
.on('message', message => console.log(`Message: ${message}`))
.start()
console.info('This CI test was activitated from Master Branch.')
botList.push(
new Wechaty({ puppet: 'wechaty-puppet-padchat' }),
new Wechaty({ puppet: 'wechaty-puppet-padpro' }),
)
}
try {
for (const bot of botList) {
const future = new Promise(resolve => bot.once('scan', resolve))
await bot.start()
await future
await bot.stop()
console.info(`Puppet ${bot.puppet} v${bot.puppet.version()} smoke testing passed.`)
}
console.info(`Wechaty v${Wechaty.VERSION} smoke testing passed.`)
} catch (e) {
console.error(e)
// Error!
return 1
}
return 0
}
function getBotList (): Wechaty[] {
const botList = [
new Wechaty({ puppet: 'wechaty-puppet-mock' }),
new Wechaty({ puppet: 'wechaty-puppet-wechat4u' }),
// new Wechaty({ puppet: 'wechaty-puppet-puppeteer' }),
]
if (!isPR) {
botList.push(
new Wechaty({
puppet: 'wechaty-puppet-padplus',
// we use WECHATY_PUPPET_PADPLUS_TOKEN environment variable at here.
})
)
}
return botList
}
function getBotList (): Wechaty[] {
const botList = [
new Wechaty({ puppet: 'wechaty-puppet-mock' }),
new Wechaty({ puppet: 'wechaty-puppet-wechat4u' }),
// new Wechaty({ puppet: 'wechaty-puppet-puppeteer' }),
]
if (!isPR) {
botList.push(
new Wechaty({
puppet: 'wechaty-puppet-padplus',
// we use WECHATY_PUPPET_PADPLUS_TOKEN environment variable at here.
})
)
}
return botList
}
Wechaty,
Message,
} from 'wechaty'
// log.level = 'verbose'
// log.level = 'silly'
/**
*
* `7217d7bce18c4bcfbe04ba7bdfaf9c08` for Wechaty demo
*
*/
const APIAI_API_KEY = '7217d7bce18c4bcfbe04ba7bdfaf9c08'
const brainApiAi = ApiAi(APIAI_API_KEY)
const bot = Wechaty.instance({ profile: config.default.DEFAULT_PROFILE })
console.log(`
Welcome to api.AI Wechaty Bot.
Api.AI Doc: https://docs.api.ai/v16/docs/get-started
Notice: This bot will only active in the room which name contains 'wechaty'.
/* if (m.room() && /Wechaty/i.test(m.room().name())) { */
Loading... please wait for QrCode Image Url and then scan to login.
`)
bot
.on('scan', (qrcode, status) => {
QrcodeTerminal.generate(qrcode)
console.log(`${qrcode}\n[${status}] Scan QR Code in above url to login: `)
})
const {
groupMessage,
groupJoinMessage,
groupLeaveMessage,
groupTopicChange,
} = require('./feature/group.js');
const { singleMessage } = require('./feature/single.js');
const { isAutoChangeAvatar } = require('./config/config.js');
// 引入数据库链接文件
require('./tools/mongo.js');
// 实例化机器人对象
const bot = Wechaty.instance();
// 全局对象,保存当前登录的用户
let userSelf = null;
// 监听扫码登录方法
bot.on('scan', (qrCode, statusCode) => {
// 判断状态码
if (!/201|200/.test(statusCode)) {
QrcodeTerminal.generate(qrCode);
console.log('扫描二维码登录~~~~');
}
});
// 监听登录方法
bot.on('login', async (userContact) => {
userSelf = await contactInfo(userContact);
export default async function onMessage (message) {
try {
const room = message.room()
const sender = message.from()
const content = message.text()
const roomName = room ? `[${await room.topic()}] ` : ''
process.stdout.write(
`${roomName}<${sender.name()}>(${message.type()}:${message.typeSub()}): `)
if (message instanceof MediaMessage) {
saveMediaFile(message)
return
}
console.log(`${Misc.digestEmoji(message)}`)
// add an extra CR if too long
if (content.length > 80) console.log("")
const config = await hotImport('config.js')
// Hot import! Try to change the msgKW1&2 to 'ping' & 'pong'
// after the bot has already started!
if (content === config.msgKW1) {
await message.say(`${config.msgKW2}, thanks for ${config.msgKW1} me`)
log.info('Bot', `REPLY: ${config.msgKW2}`)
} else if (content === config.msgKW2) {
await sender.say('ok, ${config.msgKW2} me is welcome, too.')
} else if (/^hello/i.test(content)) {
return `How are you, ${sender.name()} from ${roomName}`
}
} catch (e) {
log.error('Bot', 'on(message) exception: %s' , e)
const { Wechaty, Room } = require('wechaty')
const qrTerm = require('qrcode-terminal')
const bot = new Wechaty()
bot.on('scan', (qrcode, status) => {
qrTerm.generate(qrcode, { small: true }) // show qrcode on console
const qrcodeImageUrl = [
'https://api.qrserver.com/v1/create-qr-code/?data=',
encodeURIComponent(qrcode),
'&size=220x220&margin=20',
].join('')
console.log(qrcodeImageUrl)
})
bot.on('login', user => {
console.log(`${user} login`)
})
// });
// return;
// }
if (Data.getWechatyStatus(userKey)) {
res.send({
success: false,
msg: "用户已登录"
});
return;
}
let wechatyCache = Data.getWechaty(userKey);
if (wechatyCache) {
console.log(`wechatyCache.options.name============================${wechatyCache.options.name}`);
wechatyCache.stop();
}
const wechaty = new Wechaty({
name: userKey
});
Data.pushWechaty(userKey, wechaty);
wechaty.on('scan', (qrcode, status) => {
Scan.onScan(userKey, qrcode, status, (url) => {
try {
res.send({
success: true,
msg: "请扫描二维码登录",
data: {
url: url
}
})
} catch (e) {
}