|
| 1 | +/** |
| 2 | + * Wechaty - Conversational RPA SDK for Chatbot Makers. |
| 3 | + * - https://github.com/wechaty/wechaty |
| 4 | + */ |
| 5 | +import { |
| 6 | + Contact, |
| 7 | + Message, |
| 8 | + ScanStatus, |
| 9 | + WechatyBuilder, |
| 10 | + log, |
| 11 | + types, |
| 12 | +} from 'wechaty' |
| 13 | +import { FileBox } from 'file-box' |
| 14 | + |
| 15 | +import { PuppetWechat4u } from '../src/puppet-wechat4u.js' |
| 16 | +import qrcodeTerminal from 'qrcode-terminal' |
| 17 | +import timersPromise from 'timers/promises' |
| 18 | +import fs from 'fs' |
| 19 | + |
| 20 | +function onScan (qrcode: string, status: ScanStatus) { |
| 21 | + if (qrcode) { |
| 22 | + const qrcodeImageUrl = [ |
| 23 | + 'https://wechaty.js.org/qrcode/', |
| 24 | + encodeURIComponent(qrcode), |
| 25 | + ].join('') |
| 26 | + console.info('StarterBot', 'onScan: %s(%s) - %s', status, qrcodeImageUrl) |
| 27 | + |
| 28 | + qrcodeTerminal.generate(qrcode, { small: true }) // show qrcode on console |
| 29 | + console.info(`[${status}] ${qrcode}\nScan QR Code above to log in: `) |
| 30 | + } else { |
| 31 | + console.info(`[${status}]`) |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +async function onLogin (user: Contact) { |
| 36 | + log.info('StarterBot', '%s login', user) |
| 37 | + const roomList = await bot.Room.findAll() |
| 38 | + console.info(roomList.length) |
| 39 | + const contactList = await bot.Contact.findAll() |
| 40 | + console.info(contactList.length) |
| 41 | +} |
| 42 | + |
| 43 | +function onLogout (user: Contact) { |
| 44 | + log.info('StarterBot', '%s logout', user) |
| 45 | +} |
| 46 | + |
| 47 | +async function onMessage (msg: Message) { |
| 48 | + log.info('StarterBot', msg.toString()) |
| 49 | + if (msg.text() === 'ding') { |
| 50 | + await msg.say('dong') |
| 51 | + } |
| 52 | + |
| 53 | + const basepath = 'examples/media/' |
| 54 | + /** |
| 55 | + * 发送文件 |
| 56 | + */ |
| 57 | + if (msg.text() === 'txt') { |
| 58 | + const newpath = basepath + 'test.txt' |
| 59 | + const fileBox = FileBox.fromFile(newpath) |
| 60 | + await msg.say(fileBox) |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * 发送图片 |
| 65 | + */ |
| 66 | + if (msg.text() === 'jpg') { |
| 67 | + const newpath = 'https://github.com/wechaty/wechaty/blob/main/docs/images/bot-qr-code.png' |
| 68 | + const fileBox = FileBox.fromUrl(newpath) |
| 69 | + await msg.say(fileBox) |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * 发送表情 |
| 74 | + */ |
| 75 | + if (msg.text() === 'gif') { |
| 76 | + const newpath = basepath + 'test.gif' |
| 77 | + const fileBox = FileBox.fromFile(newpath) |
| 78 | + await msg.say(fileBox) |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * 发送视频 |
| 83 | + */ |
| 84 | + if (msg.text() === 'mp4') { |
| 85 | + const newpath = basepath + 'test.mp4' |
| 86 | + const fileBox = FileBox.fromFile(newpath) |
| 87 | + await msg.say(fileBox) |
| 88 | + } |
| 89 | + |
| 90 | + try { |
| 91 | + if (msg.type() === types.Message.Image || msg.type() === types.Message.Attachment || msg.type() === types.Message.Video || msg.type() === types.Message.Audio || msg.type() === types.Message.Emoticon) { |
| 92 | + const file = await msg.toFileBox() // Save the media message as a FileBox |
| 93 | + const filePath = 'examples/file/' + file.name |
| 94 | + file.toFile(filePath) |
| 95 | + log.info(`Saved file: ${filePath}`) |
| 96 | + } else { |
| 97 | + // Log other non-text messages |
| 98 | + const logData = { |
| 99 | + date: new Date(), |
| 100 | + from: msg.talker().name(), |
| 101 | + text: msg.text(), |
| 102 | + type: msg.type(), |
| 103 | + } |
| 104 | + const logPath = 'examples/log/message.log' |
| 105 | + fs.appendFileSync(logPath, JSON.stringify(logData, null, 2) + '\n') |
| 106 | + log.info(`Logged message data to ${logPath}`) |
| 107 | + } |
| 108 | + } catch (e) { |
| 109 | + console.error(`Error handling message: ${e}`) |
| 110 | + } |
| 111 | + |
| 112 | +} |
| 113 | + |
| 114 | +const puppet = new PuppetWechat4u() |
| 115 | +const bot = WechatyBuilder.build({ |
| 116 | + name: 'ding-dong-bot', |
| 117 | + puppet, |
| 118 | +}) |
| 119 | + |
| 120 | +bot.on('scan', onScan) |
| 121 | +bot.on('login', onLogin) |
| 122 | +bot.on('logout', onLogout) |
| 123 | +bot.on('message', onMessage) |
| 124 | +bot.on('room-join', async (room, inviteeList, inviter) => { |
| 125 | + const nameList = inviteeList.map(c => c.name()).join(',') |
| 126 | + log.info(`Room ${await room.topic()} got new member ${nameList}, invited by ${inviter}`) |
| 127 | +}) |
| 128 | +bot.on('room-leave', async (room, leaverList, remover) => { |
| 129 | + const nameList = leaverList.map(c => c.name()).join(',') |
| 130 | + log.info(`Room ${await room.topic()} lost member ${nameList}, the remover is: ${remover}`) |
| 131 | +}) |
| 132 | +bot.on('room-topic', async (room, topic, oldTopic, changer) => { |
| 133 | + log.info(`Room ${await room.topic()} topic changed from ${oldTopic} to ${topic} by ${changer.name()}`) |
| 134 | +}) |
| 135 | +bot.on('room-invite', async roomInvitation => { |
| 136 | + log.info(JSON.stringify(roomInvitation)) |
| 137 | + try { |
| 138 | + log.info('received room-invite event.') |
| 139 | + await roomInvitation.accept() |
| 140 | + } catch (e) { |
| 141 | + console.error(e) |
| 142 | + } |
| 143 | +}) |
| 144 | + |
| 145 | +bot.start() |
| 146 | + .then(() => { |
| 147 | + return log.info('StarterBot', 'Starter Bot Started.') |
| 148 | + }) |
| 149 | + .catch(console.error) |
0 commit comments