Skip to content

Commit e01ec9c

Browse files
committed
v1.14.3 修复接收视频消息bug及增加wechaty运行示例
1 parent fae3593 commit e01ec9c

File tree

7 files changed

+245
-4
lines changed

7 files changed

+245
-4
lines changed

examples/file/test.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
text

examples/media/test.gif

52.1 KB
Loading

examples/media/test.mp4

345 KB
Binary file not shown.

examples/media/test.txt

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# WECHATY-PUPPET-WECHAT4U [![NPM](https://github.com/wechaty/wechaty-puppet-wechat4u/actions/workflows/npm.yml/badge.svg)](https://github.com/wechaty/wechaty-puppet-wechat4u/actions/workflows/npm.yml)
2+
3+
[![Powered by Wechaty](https://img.shields.io/badge/Powered%20By-Wechaty-blue.svg)](https://github.com/chatie/wechaty)
4+
[![NPM Version](https://badge.fury.io/js/wechaty-puppet-wechat4u.svg)](https://badge.fury.io/js/wechaty-puppet-wechat4u)
5+
[![npm (tag)](https://img.shields.io/npm/v/wechaty-puppet-wechat4u/next.svg)](https://www.npmjs.com/package/wechaty-puppet-wechat4u?activeTab=versions)
6+
[![TypeScript](https://img.shields.io/badge/%3C%2F%3E-TypeScript-blue.svg)](https://www.typescriptlang.org/)
7+
[![ES Modules](https://img.shields.io/badge/ES-Modules-brightgreen)](https://github.com/Chatie/tsconfig/issues/16)
8+
9+
![wechaty puppet wechat4u](https://wechaty.github.io/puppet-wechat4u/images/wechat4u-logo.png)
10+
11+
Wechat4u Puppet for Wechaty
12+
13+
See: [New Puppet - Plan to support `WECHATY_HEAD=WECHAT4U` #69](https://github.com/Chatie/wechaty/issues/69)
14+
15+
## ABOUT WECHAT4U
16+
17+
[Wechat4U](https://github.com/nodeWechat/wechat4u) is an excellent wechat bot framework that supports both Node.js & Browser, with rich features and an active community of experienced contributors.
18+
19+
## KNOWN LIMITATIONS
20+
21+
~~1. WeChat Account that registered after 2017 mignt not be able to login Web Wechat, so it can not use PuppetPuppeteer with Wechaty. Please make sure your WeChat Account can be able to login by visiting <https://wx.qq.com>~~
22+
~~1. Web API can not create room and invite members to room since 2018.~~
23+
24+
UOS has support
25+
26+
更新 - 2023/02/10
27+
目前使用 1.13.14 大多数使用者可能出现微信被官方封禁提醒,从已知收集的封禁情况,暂未有可解决方案。
28+
29+
猜测可能与近期ChatGPT结合本仓库实现个性化机器人导致相关封禁,请合理,谨慎使用本仓库。
30+
31+
If you want to break the above limitations, please consider to use a Wechaty Puppet other than using Web API, like [wechaty-puppet-padchat](https://github.com/lijiarui/wechaty-puppet-padchat).
32+
33+
Learn more about the Puppet at [Wechaty wiki: Puppet](https://github.com/Chatie/wechaty/wiki/Puppet)
34+
35+
## HISTORY
36+
37+
### v1.14.0 (April 21, 2023)
38+
39+
1.Stable version
40+
41+
2.Fix Contact isFriend
42+
43+
### v1.13.8 (Nov 22, 2022)
44+
45+
1.Fix Contact update
46+
47+
### v1.13.1 (Nov 18, 2022)
48+
49+
1.Support uos login
50+
51+
2.Refactor the code to support more event
52+
53+
### master v1.0 (Oct 30, 2021)
54+
55+
Release 1.0 of Wechaty Puppet for Wechat4u
56+
57+
### v0.20 (Sep 14, 2021)
58+
59+
1. ES Modules support
60+
61+
### v0.18 (Feb 20, 2021)
62+
63+
Fix `wechaty-puppet` dependencies
64+
65+
### v0.0.1 (Jun 30, 2018)
66+
67+
Init version
68+
69+
## MAINTAINERS
70+
71+
- [Leo Chen](https://wechaty.js.org/contributors/leochen-g/), [Project Admin](https://github.com/wechaty/puppet-wechat4u/pull/42#issuecomment-1324436596)
72+
- [Huan](https://wechaty.js.org/contributors/huan/), Author of Puppet WeChat4U
73+
74+
## AUTHOR
75+
76+
[Huan LI](http://linkedin.com/in/zixia) \<[email protected]\>
77+
78+
<!-- markdownlint-disable MD033 -->
79+
<a href="https://stackexchange.com/users/265499">
80+
<img src="https://stackexchange.com/users/flair/265499.png" width="208" height="58" alt="profile for zixia on Stack Exchange, a network of free, community-driven Q&amp;A sites" title="profile for zixia on Stack Exchange, a network of free, community-driven Q&amp;A sites">
81+
</a>
82+
83+
## COPYRIGHT & LICENSE
84+
85+
- Code & Docs © 2018 Huan LI \<[email protected]\>
86+
- Code released under the Apache-2.0 License
87+
- Docs released under Creative Commons

examples/ripe-wechaty.ts

+149
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
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)

package.json

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "wechaty-puppet-wechat4u",
3-
"version": "1.14.2",
3+
"version": "1.14.3",
44
"description": "Wechat4u Puppet for Wechaty",
55
"type": "module",
66
"exports": {
@@ -24,6 +24,8 @@
2424
"lint:ts": "tsc --isolatedModules --noEmit",
2525
"lint:es": "eslint \"src/**/*.ts\" \"tests/**/*.spec.ts\" --ignore-pattern tests/fixtures/",
2626
"start": "cross-env NODE_OPTIONS=\"--no-warnings --loader=ts-node/esm\" node examples/ding-dong-bot.ts",
27+
"start:ripe": "cross-env WECHATY_LOG=verbose NODE_OPTIONS=\"--no-warnings --loader=ts-node/esm\" node examples/ripe-wechaty.ts",
28+
"start:ripe:nolog": "cross-env NODE_OPTIONS=\"--no-warnings --loader=ts-node/esm\" node examples/ripe-wechaty.ts",
2729
"test": "npm run lint && npm run test:unit",
2830
"test:pack": "bash -x scripts/npm-pack-testing.sh",
2931
"test:unit": "cross-env NODE_OPTIONS=\"--no-warnings --loader=ts-node/esm\" tap \"src/**/*.spec.ts\" \"tests/**/*.spec.ts\""
@@ -54,11 +56,13 @@
5456
"@chatie/tsconfig": "^4.6.2",
5557
"@types/promise-retry": "^1.1.3",
5658
"@types/xml2js": "^0.4.11",
59+
"wechaty": "^1.20.2",
60+
"qrcode-terminal": "^0.12.0",
5761
"memory-card": "^1.0.3"
5862
},
5963
"peerDependencies": {
60-
"wechaty-puppet": "^1.18.3",
61-
"@swc/core":"1.3.78"
64+
"@swc/core": "1.3.78",
65+
"wechaty-puppet": "^1.18.3"
6266
},
6367
"homepage": "https://github.com/wechaty/wechaty-puppet-wechat4u#readme",
6468
"dependencies": {

src/puppet-wechat4u.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,7 @@ export class PuppetWechat4u extends PUPPET.Puppet {
696696
* 视频消息
697697
*/
698698
// console.log('视频消息,保存到本地')
699-
return FileBox.fromStream(
699+
return FileBox.fromBuffer(
700700
(await this.wechat4u.getVideo(rawPayload.MsgId)).data,
701701
`message-${id}-video.mp4`,
702702
)

0 commit comments

Comments
 (0)