Skip to content

Commit 04bcc9b

Browse files
atorberchoogoo
andauthored
send at msg fixed (#205)
* 3.9.2.23 init (#180) * 3.9.2.23 initt * Update init-agent-script.js * 3.9.2.23 adapter * 3.9.2.23 init (#182) * 3.9.2.23 initt * Update init-agent-script.js * 3.9.2.23 adapter * 适配3.9.2.23 * Delete agent-script-3.9.2.23-new.js * 适配3.9.2.23 (#185) * 3.9.2.23 initt * Update init-agent-script.js * 3.9.2.23 adapter * 适配3.9.2.23 * Delete agent-script-3.9.2.23-new.js * 适配3.9.2.23 --------- Co-authored-by: LuChao <[email protected]> * add 1.3.0 illustrate * Update README.md * Update README.md * add demo * 1.13.1 * 1.13.1 * 1.13.2 dev * 增加扫码登录、检测登录状态 * 增加登入登出事件 * 增加ts改造init * 1.13.2 * 1.13.2 * 1.13.2 * 1.13.2 * 1.13.2 1. 修复获取群成员昵称乱码 * Update .npmignore * Update .gitignore * 1.13.2删除agent的ts文件 * 1.13.4 * 1.13.5 * Update init-agent-script.ts * 1.13.5 * 1.13.6 --------- Co-authored-by: choogoo <[email protected]>
1 parent 724138a commit 04bcc9b

21 files changed

+3208
-3020
lines changed

.eslintrc.cjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
const rules = {
2+
"no-shadow": "off"
23
}
34

45
module.exports = {
56
extends: '@chatie',
7+
"ignorePatterns": ["src/init-agent-script.ts", "src/init-agent-script.js"],
68
rules,
79
}

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,6 @@ typings/
6565
t/
6666
t.*
6767
cache/*
68+
# src/init-agent-script.ts
69+
examples/file/*
70+
examples/media/*

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
docs/
2+
src/init-agent-script.ts

README.md

Lines changed: 42 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,21 @@ wechaty-puppet-xp is a local puppet for Wechaty:
2626
## GETTING STARTED
2727

2828
- STEP 1: Install wechat client in your Windows computer.
29-
- STEP 2: Login the wechat client on the computer.
30-
- STEP 3: Getting Started with TypeScript/JavaScript (RECOMMENDED).
3129

32-
## QUICK START
30+
> 1.13.0+ is the latest version, only support WeChat v3.9.2.23. Note to use the npm package that matches the WeChat version.
3331
34-
> 1.12.0+ is the latest version, only support WeChat v3.6.0.18, 1.11.14 is the last version base WeChat v3.3.0.115. Note to use the npm package that matches the WeChat version.
32+
- STEP 2: Login the wechat client on the computer.
33+
- STEP 3: Getting Started with TypeScript/JavaScript (RECOMMENDED).
3534

3635
```sh
3736
git clone https://github.com/wechaty/wechaty-puppet-xp.git
3837
cd wechaty-puppet-xp
38+
39+
# 安装依赖
3940
npm install
41+
42+
# 启动程序
43+
npm start
4044
#
4145
# Do not forget to install WeChat with requried version and login.
4246
#
@@ -48,46 +52,51 @@ npm install
4852
| `npm run start:ripe` | [examples/ripe-wechaty.ts](examples/ripe-wechaty.ts) | Wechaty ding/dong |
4953
| `npm run start:raw` | [examples/raw-sidecar.ts](examples/raw-sidecar.ts) | Sidecar ding/dong |
5054

55+
## RUNNING WHITH NPM
56+
57+
puppet-xp also have already released the installation package on NPM. Running with NPM and more examples can be referred to [wechaty-puppet-xp-getting-started](https://github.com/atorber/wechaty-puppet-xp-getting-started).
58+
5159
## PUPPET COMPARISON
5260

5361
XP is a young puppet,it keeps growing and improving.
5462

55-
Puppet|xp👍|
56-
:---|:---|
57-
支持账号|个人微信|
63+
版本|3.3.0.115|3.6.0.18|3.9.2.23|
64+
:---|:---|:---|:---|
5865
**<消息>**|
59-
接收文本|✅
60-
接收图片|✅
61-
接收文件|✅
62-
接收动图|✅
63-
接收表情|✅
64-
接收小程序卡片|✅
65-
接收联系人卡片|✅
66-
接收位置卡片|✅
67-
发送文本|✅
68-
发送图片|✅
69-
发送文件|✅
70-
发送动图|✅(以文件形式发送)
66+
接收文本|✅|✅|✅
67+
接收图片|✅|✅|✅
68+
接收文件|✅|✅|✅
69+
接收动图|✅|✅|✅
70+
接收表情|✅|✅|✅
71+
接收小程序卡片|✅|✅|✅
72+
接收联系人卡片|✅|✅|✅
73+
接收位置卡片|✅|✅|✅
74+
发送文本|✅|✅|✅
75+
发送图片|✅|✅|✅
76+
发送文件|✅|✅|✅
77+
发送动图|✅|✅|✅
7178
**<群组>**|
72-
@群成员|✅
73-
群列表|✅
74-
群成员列表|✅
75-
群详情|✅
76-
进群提示|✅
79+
@群成员|✅|✅|✅
80+
群列表|✅|✅|✅
81+
群成员列表|✅|✅|✅
82+
群详情|✅|✅|✅
83+
进群提示|✅|✅|✅
7784
**<联系人>**|
78-
好友列表|✅
79-
好友详情|✅
85+
好友列表|✅|✅|✅
86+
好友详情|✅|✅|✅
8087
**<其他>**|
81-
登录事件|✅
82-
依赖协议|Windows
88+
登录事件|✅|✅|✅
89+
扫码登录|||✅
8390

8491
## VERSION SUPPORT
8592

86-
puppet-xp|wechat|
87-
|:---|:---|
88-
|1.11.14|[WeChat-v3.3.0.115](https://github.com/wechaty/wechaty-puppet-xp/releases/download/v0.5/WeChatSetup-v3.3.0.115.exe)|
89-
|1.12.7|[WeChat-v3.6.0.18](https://github.com/tom-snow/wechat-windows-versions/releases/download/v3.6.0.18/WeChatSetup-3.6.0.18.exe)|
90-
|1.3.x|[WeChat-v3.9.2.23](https://github.com/tom-snow/wechat-windows-versions/releases/download/v3.9.2.23/WeChatSetup-3.9.2.23.exe)|
93+
Note: You need to install an NPM version that matches your WeChat client version.
94+
95+
puppet-xp|wechat|npm install|
96+
|:---|:---|:---|
97+
|1.3.x|[WeChat-v3.9.2.23](https://github.com/tom-snow/wechat-windows-versions/releases/download/v3.9.2.23/WeChatSetup-3.9.2.23.exe)|npm i wechaty-puppet-xp@next|
98+
|1.12.7|[WeChat-v3.6.0.18](https://github.com/tom-snow/wechat-windows-versions/releases/download/v3.6.0.18/WeChatSetup-3.6.0.18.exe)|npm i [email protected]|
99+
|1.11.14|[WeChat-v3.3.0.115](https://github.com/wechaty/wechaty-puppet-xp/releases/download/v0.5/WeChatSetup-v3.3.0.115.exe)|npm i [email protected]|
91100

92101
## HISTORY
93102

examples/demo.ts

Lines changed: 265 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,265 @@
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

Comments
 (0)