|
| 1 | +#!/usr/bin/env -S node --no-warnings --loader ts-node/esm |
| 2 | +/** |
| 3 | + * wechaty-puppet-xp示例代码,可以作为模版编写自己的业务逻辑. |
| 4 | + * |
| 5 | +**/ |
| 6 | +import 'dotenv/config.js' |
| 7 | + |
| 8 | +import { |
| 9 | + Contact, |
| 10 | + Message, |
| 11 | + ScanStatus, |
| 12 | + WechatyBuilder, |
| 13 | + log, |
| 14 | + types, |
| 15 | +} from 'wechaty' |
| 16 | + |
| 17 | +import qrcodeTerminal from 'qrcode-terminal' |
| 18 | +import { FileBox } from 'file-box' |
| 19 | +import { PuppetXp } from '../src/puppet-xp.js' |
| 20 | + |
| 21 | +const onScan = (qrcode: string, status: ScanStatus) => { |
| 22 | + if (status === ScanStatus.Waiting || status === ScanStatus.Timeout) { |
| 23 | + const qrcodeImageUrl = [ |
| 24 | + 'https://wechaty.js.org/qrcode/', |
| 25 | + encodeURIComponent(qrcode), |
| 26 | + ].join('') |
| 27 | + log.info('onScan: %s(%s) - %s', ScanStatus[status], status, qrcodeImageUrl) |
| 28 | + |
| 29 | + qrcodeTerminal.generate(qrcode, { small: true }) // show qrcode on console |
| 30 | + |
| 31 | + } else { |
| 32 | + log.info('onScan: %s(%s)', ScanStatus[status], status) |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +const onLogin = async (user: Contact) => { |
| 37 | + log.info('onLogin', '%s login', user) |
| 38 | + // 登录成功后调用bot |
| 39 | + main() |
| 40 | +} |
| 41 | + |
| 42 | +const onLogout = (user: Contact) => { |
| 43 | + log.info('onLogout', '%s logout', user) |
| 44 | +} |
| 45 | + |
| 46 | +const onMessage = async (msg: Message) => { |
| 47 | + log.info('onMessage', JSON.stringify(msg)) |
| 48 | + // Message doc : https://wechaty.js.org/docs/api/message#messageage--number |
| 49 | + |
| 50 | + const talker = msg.talker() // 发消息人 |
| 51 | + const listener = msg.listener() // 接收消息人 |
| 52 | + const room = msg.room() // 是否是群消息 |
| 53 | + const text = msg.text() // 消息内容 |
| 54 | + const type = msg.type() // 消息类型 |
| 55 | + const self = msg.self() // 是否自己发给自己的消息 |
| 56 | + |
| 57 | + log.info('talker', JSON.stringify(talker)) |
| 58 | + log.info('listener', listener||'undefined') |
| 59 | + log.info('room', room || 'undefined') |
| 60 | + log.info('text', text) |
| 61 | + log.info('type', type) |
| 62 | + log.info('self', self?'true':'false') |
| 63 | + |
| 64 | + try { |
| 65 | + switch (text) { |
| 66 | + case 'ding': // 接收到的消息是ding,回复dong |
| 67 | + await msg.say('dong') |
| 68 | + break |
| 69 | + case 'send text': // 接收到的消息是send text,发送文本消息 |
| 70 | + await msg.say('this is a test text') |
| 71 | + break |
| 72 | + case 'send image': // 接收到的消息是send image,发送图片 |
| 73 | + const image = FileBox.fromUrl('https://wechaty.js.org/assets/logo.png') |
| 74 | + await msg.say(image) |
| 75 | + break |
| 76 | + case 'send file': // 接收到的消息是send file,发送文件 |
| 77 | + const fileBox = FileBox.fromUrl('https://wechaty.js.org/assets/logo.png') |
| 78 | + await msg.say(fileBox) |
| 79 | + break |
| 80 | + case 'send video': // 接收到的消息是send video,发送视频 |
| 81 | + const video = FileBox.fromUrl('http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4') |
| 82 | + await msg.say(video) |
| 83 | + break |
| 84 | + case 'send audio': // 接收到的消息是send audio,发送音频 |
| 85 | + const audio = FileBox.fromUrl('http://www.zhongguoyinhang.com/upload/2018-11/201811161154314128.mp3') |
| 86 | + await msg.say(audio) |
| 87 | + break |
| 88 | + case 'send emotion': // 接收到的消息是send emotion,发送表情 |
| 89 | + const emotion = FileBox.fromUrl('https://res.wx.qq.com/mpres/htmledition/images/icon/emotion/0.gif') |
| 90 | + await msg.say(emotion) |
| 91 | + break |
| 92 | + default: |
| 93 | + break |
| 94 | + } |
| 95 | + } catch (e) { |
| 96 | + console.log('回复消息失败...', e) |
| 97 | + } |
| 98 | + |
| 99 | + try{ |
| 100 | + switch (type) { |
| 101 | + case types.Message.Text: // 接收到的消息是文本 |
| 102 | + log.info('接收到的消息是文本') |
| 103 | + log.info('消息内容:', text) |
| 104 | + break |
| 105 | + case types.Message.Image: // 接收到的消息是图片 |
| 106 | + log.info('接收到的消息是图片') |
| 107 | + const image = await msg.toImage().thumbnail() // Save the media message as a FileBox |
| 108 | + const filePath = 'examples/file/' + image.name |
| 109 | + try { |
| 110 | + image.toFile(filePath, true) |
| 111 | + log.info(`Saved file: ${filePath}`) |
| 112 | + } catch (e) { |
| 113 | + log.error('保存文件错误:', e) |
| 114 | + } |
| 115 | + break |
| 116 | + case types.Message.Attachment: // 接收到的消息是附件 |
| 117 | + log.info('接收到的消息是附件') |
| 118 | + break |
| 119 | + case types.Message.Video: // 接收到的消息是视频 |
| 120 | + log.info('接收到的消息是视频') |
| 121 | + break |
| 122 | + case types.Message.Audio: // 接收到的消息是音频 |
| 123 | + log.info('接收到的消息是音频') |
| 124 | + break |
| 125 | + case types.Message.Emoticon: // 接收到的消息是表情 |
| 126 | + log.info('接收到的消息是表情') |
| 127 | + break |
| 128 | + case types.Message.Url: // 接收到的消息是链接 |
| 129 | + log.info('接收到的消息是链接') |
| 130 | + break |
| 131 | + case types.Message.MiniProgram: // 接收到的消息是小程序 |
| 132 | + log.info('接收到的消息是小程序') |
| 133 | + break |
| 134 | + case types.Message.Transfer: // 接收到的消息是转账 |
| 135 | + log.info('接收到的消息是转账') |
| 136 | + break |
| 137 | + case types.Message.RedEnvelope: // 接收到的消息是红包 |
| 138 | + log.info('接收到的消息是红包') |
| 139 | + break |
| 140 | + case types.Message.Recalled: // 接收到的消息是撤回的消息 |
| 141 | + log.info('接收到的消息是撤回的消息') |
| 142 | + break |
| 143 | + default: |
| 144 | + break |
| 145 | + } |
| 146 | + }catch(e){ |
| 147 | + console.log('处理消息失败...', e) |
| 148 | + } |
| 149 | + // 关键词回复,同时也是发送消息的方法 |
| 150 | +} |
| 151 | + |
| 152 | +// const bot = WechatyBuilder.build({ |
| 153 | +// name: 'ding-dong-bot', |
| 154 | +// puppet: 'wechaty-puppet-xp', |
| 155 | +// puppetOptions: { |
| 156 | +// version: '3.9.2.23', |
| 157 | +// } |
| 158 | +// }) |
| 159 | + |
| 160 | +const puppet = new PuppetXp({wechatVersion:'0.0.0.0'}) |
| 161 | +const bot = WechatyBuilder.build({ |
| 162 | + name: 'ding-dong-bot', |
| 163 | + puppet, |
| 164 | +}) |
| 165 | + |
| 166 | +bot.on('scan', onScan) |
| 167 | +bot.on('login', onLogin) |
| 168 | +bot.on('logout', onLogout) |
| 169 | +bot.on('message', onMessage) |
| 170 | + |
| 171 | +bot.start() |
| 172 | + .then(async () => { |
| 173 | + log.info('StarterBot', 'Starter Bot Started.') |
| 174 | + }) |
| 175 | + .catch(e => log.error('StarterBot', e)) |
| 176 | + |
| 177 | +const main = async () => { |
| 178 | + |
| 179 | + // 获取当前登录微信信息 |
| 180 | + try { |
| 181 | + const self = bot.currentUser |
| 182 | + log.info('当前登录账号信息:', self) |
| 183 | + } catch (e) { |
| 184 | + log.error('get user failed', e) |
| 185 | + } |
| 186 | + |
| 187 | + // 通过微信号搜索联系人 |
| 188 | + try { |
| 189 | + const contactById = await bot.Contact.find({ |
| 190 | + id: 'filehelper' |
| 191 | + }) |
| 192 | + log.info('微信号查找联系人:', contactById) |
| 193 | + // 向联系人发送消息 |
| 194 | + contactById?.say('向指定好友微信号发送消息') |
| 195 | + } catch (e) { |
| 196 | + log.error('contactByWeixin', e) |
| 197 | + } |
| 198 | + |
| 199 | + // 通过昵称搜索联系人 |
| 200 | + try { |
| 201 | + const contactByName = await bot.Contact.find({ |
| 202 | + name: '文件传输助手' |
| 203 | + }) |
| 204 | + log.info('昵称查找联系人:', contactByName) |
| 205 | + // 向联系人发送消息 |
| 206 | + contactByName?.say('向指定好友昵称发送消息') |
| 207 | + } catch (e) { |
| 208 | + log.error('contactByName', e) |
| 209 | + } |
| 210 | + |
| 211 | + // 通过备注搜索联系人 |
| 212 | + try { |
| 213 | + const contactByAlias = await bot.Contact.find({ |
| 214 | + alias: '超哥' |
| 215 | + }) |
| 216 | + log.info('备注名称查找联系人:', contactByAlias || '没有找到联系人') |
| 217 | + // 向联系人发送消息 |
| 218 | + contactByAlias?.say('向指定好友备注好友发送消息') |
| 219 | + } catch (e) { |
| 220 | + log.error('contactByAlias', e) |
| 221 | + } |
| 222 | + |
| 223 | + try { |
| 224 | + // 通过群ID搜索群 |
| 225 | + const roomById = await bot.Room.find({ |
| 226 | + id: '21341182572@chatroom' |
| 227 | + }) |
| 228 | + log.info('群ID查找群:', roomById) |
| 229 | + // 向群里发送消息 |
| 230 | + roomById?.say('向指定群ID发送消息') |
| 231 | + } catch (e) { |
| 232 | + log.error('roomById', e) |
| 233 | + } |
| 234 | + |
| 235 | + try { |
| 236 | + // 通过群名称搜索群 |
| 237 | + const roomByName = await bot.Room.find({ |
| 238 | + topic: '大师是群主' |
| 239 | + }) |
| 240 | + log.info('群名称查找群:', roomByName || '没有找到群') |
| 241 | + // 向群里发送消息 |
| 242 | + roomByName?.say('向指定群名称发送消息') |
| 243 | + } catch (e) { |
| 244 | + console.log('roomByName', e) |
| 245 | + } |
| 246 | + |
| 247 | + try { |
| 248 | + // 获取所有联系人 |
| 249 | + const contactList = await bot.Contact.findAll() |
| 250 | + // log.info('获取联系人列表:', contactList) |
| 251 | + log.info('联系人数量:', contactList.length) |
| 252 | + } catch (e) { |
| 253 | + log.error('contactList', e) |
| 254 | + } |
| 255 | + |
| 256 | + try { |
| 257 | + // 获取所有群 |
| 258 | + const roomList = await bot.Room.findAll() |
| 259 | + // log.info('获取群列表:', roomList) |
| 260 | + log.info('群数量:', roomList.length) |
| 261 | + } catch (e) { |
| 262 | + log.error('roomList', e) |
| 263 | + } |
| 264 | + |
| 265 | +} |
0 commit comments