UNPKG

wechaty-puppet-wechat4u

Version:
136 lines 4.97 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); /** * Wechaty - Conversational RPA SDK for Chatbot Makers. * - https://github.com/wechaty/wechaty */ const wechaty_1 = require("wechaty"); const file_box_1 = require("file-box"); const puppet_wechat4u_js_1 = require("../src/puppet-wechat4u.js"); const qrcode_terminal_1 = __importDefault(require("qrcode-terminal")); const fs_1 = __importDefault(require("fs")); function onScan(qrcode, status) { if (qrcode) { const qrcodeImageUrl = [ 'https://wechaty.js.org/qrcode/', encodeURIComponent(qrcode), ].join(''); console.info('StarterBot', 'onScan: %s(%s) - %s', status, qrcodeImageUrl); qrcode_terminal_1.default.generate(qrcode, { small: true }); // show qrcode on console console.info(`[${status}] ${qrcode}\nScan QR Code above to log in: `); } else { console.info(`[${status}]`); } } async function onLogin(user) { wechaty_1.log.info('StarterBot', '%s login', user); const roomList = await bot.Room.findAll(); console.info(roomList.length); const contactList = await bot.Contact.findAll(); console.info(contactList.length); } function onLogout(user) { wechaty_1.log.info('StarterBot', '%s logout', user); } async function onMessage(msg) { wechaty_1.log.info('StarterBot', msg.toString()); if (msg.text() === 'ding') { await msg.say('dong'); } const basepath = 'examples/media/'; /** * 发送文件 */ if (msg.text() === 'txt') { const newpath = basepath + 'test.txt'; const fileBox = file_box_1.FileBox.fromFile(newpath); await msg.say(fileBox); } /** * 发送图片 */ if (msg.text() === 'jpg') { const newpath = 'https://github.com/wechaty/wechaty/blob/main/docs/images/bot-qr-code.png'; const fileBox = file_box_1.FileBox.fromUrl(newpath); await msg.say(fileBox); } /** * 发送表情 */ if (msg.text() === 'gif') { const newpath = basepath + 'test.gif'; const fileBox = file_box_1.FileBox.fromFile(newpath); await msg.say(fileBox); } /** * 发送视频 */ if (msg.text() === 'mp4') { const newpath = basepath + 'test.mp4'; const fileBox = file_box_1.FileBox.fromFile(newpath); await msg.say(fileBox); } try { if (msg.type() === wechaty_1.types.Message.Image || msg.type() === wechaty_1.types.Message.Attachment || msg.type() === wechaty_1.types.Message.Video || msg.type() === wechaty_1.types.Message.Audio || msg.type() === wechaty_1.types.Message.Emoticon) { const file = await msg.toFileBox(); // Save the media message as a FileBox const filePath = 'examples/file/' + file.name; file.toFile(filePath); wechaty_1.log.info(`Saved file: ${filePath}`); } else { // Log other non-text messages const logData = { date: new Date(), from: msg.talker().name(), text: msg.text(), type: msg.type(), }; const logPath = 'examples/log/message.log'; fs_1.default.appendFileSync(logPath, JSON.stringify(logData, null, 2) + '\n'); wechaty_1.log.info(`Logged message data to ${logPath}`); } } catch (e) { console.error(`Error handling message: ${e}`); } } const puppet = new puppet_wechat4u_js_1.PuppetWechat4u(); const bot = wechaty_1.WechatyBuilder.build({ name: 'ding-dong-bot', puppet, }); bot.on('scan', onScan); bot.on('login', onLogin); bot.on('logout', onLogout); bot.on('message', onMessage); bot.on('room-join', async (room, inviteeList, inviter) => { const nameList = inviteeList.map(c => c.name()).join(','); wechaty_1.log.info(`Room ${await room.topic()} got new member ${nameList}, invited by ${inviter}`); }); bot.on('room-leave', async (room, leaverList, remover) => { const nameList = leaverList.map(c => c.name()).join(','); wechaty_1.log.info(`Room ${await room.topic()} lost member ${nameList}, the remover is: ${remover}`); }); bot.on('room-topic', async (room, topic, oldTopic, changer) => { wechaty_1.log.info(`Room ${await room.topic()} topic changed from ${oldTopic} to ${topic} by ${changer.name()}`); }); bot.on('room-invite', async (roomInvitation) => { wechaty_1.log.info(JSON.stringify(roomInvitation)); try { wechaty_1.log.info('received room-invite event.'); await roomInvitation.accept(); } catch (e) { console.error(e); } }); bot.start() .then(() => { return wechaty_1.log.info('StarterBot', 'Starter Bot Started.'); }) .catch(console.error); //# sourceMappingURL=ripe-wechaty.js.map