Skip to content

Commit

Permalink
Merge pull request #66 from atorber/main
Browse files Browse the repository at this point in the history
v1.14.3 修复接收视频消息bug及增加wechaty运行示例
  • Loading branch information
leochen-g authored Sep 21, 2023
2 parents d52db4a + 464024e commit 9e9f5ff
Show file tree
Hide file tree
Showing 7 changed files with 246 additions and 5 deletions.
1 change: 1 addition & 0 deletions examples/file/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
text
Binary file added examples/media/test.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/media/test.mp4
Binary file not shown.
87 changes: 87 additions & 0 deletions examples/media/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# WECHATY-PUPPET-WECHAT4U [![NPM](https:/wechaty/wechaty-puppet-wechat4u/actions/workflows/npm.yml/badge.svg)](https:/wechaty/wechaty-puppet-wechat4u/actions/workflows/npm.yml)

[![Powered by Wechaty](https://img.shields.io/badge/Powered%20By-Wechaty-blue.svg)](https:/chatie/wechaty)
[![NPM Version](https://badge.fury.io/js/wechaty-puppet-wechat4u.svg)](https://badge.fury.io/js/wechaty-puppet-wechat4u)
[![npm (tag)](https://img.shields.io/npm/v/wechaty-puppet-wechat4u/next.svg)](https://www.npmjs.com/package/wechaty-puppet-wechat4u?activeTab=versions)
[![TypeScript](https://img.shields.io/badge/%3C%2F%3E-TypeScript-blue.svg)](https://www.typescriptlang.org/)
[![ES Modules](https://img.shields.io/badge/ES-Modules-brightgreen)](https:/Chatie/tsconfig/issues/16)

![wechaty puppet wechat4u](https://wechaty.github.io/puppet-wechat4u/images/wechat4u-logo.png)

Wechat4u Puppet for Wechaty

See: [New Puppet - Plan to support `WECHATY_HEAD=WECHAT4U` #69](https:/Chatie/wechaty/issues/69)

## ABOUT WECHAT4U

[Wechat4U](https:/nodeWechat/wechat4u) is an excellent wechat bot framework that supports both Node.js & Browser, with rich features and an active community of experienced contributors.

## KNOWN LIMITATIONS

~~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>~~
~~1. Web API can not create room and invite members to room since 2018.~~

UOS has support

更新 - 2023/02/10
目前使用 1.13.14 大多数使用者可能出现微信被官方封禁提醒,从已知收集的封禁情况,暂未有可解决方案。

猜测可能与近期ChatGPT结合本仓库实现个性化机器人导致相关封禁,请合理,谨慎使用本仓库。

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:/lijiarui/wechaty-puppet-padchat).

Learn more about the Puppet at [Wechaty wiki: Puppet](https:/Chatie/wechaty/wiki/Puppet)

## HISTORY

### v1.14.0 (April 21, 2023)

1.Stable version

2.Fix Contact isFriend

### v1.13.8 (Nov 22, 2022)

1.Fix Contact update

### v1.13.1 (Nov 18, 2022)

1.Support uos login

2.Refactor the code to support more event

### master v1.0 (Oct 30, 2021)

Release 1.0 of Wechaty Puppet for Wechat4u

### v0.20 (Sep 14, 2021)

1. ES Modules support

### v0.18 (Feb 20, 2021)

Fix `wechaty-puppet` dependencies

### v0.0.1 (Jun 30, 2018)

Init version

## MAINTAINERS

- [Leo Chen](https://wechaty.js.org/contributors/leochen-g/), [Project Admin](https:/wechaty/puppet-wechat4u/pull/42#issuecomment-1324436596)
- [Huan](https://wechaty.js.org/contributors/huan/), Author of Puppet WeChat4U

## AUTHOR

[Huan LI](http://linkedin.com/in/zixia) \<[email protected]\>

<!-- markdownlint-disable MD033 -->
<a href="https://stackexchange.com/users/265499">
<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">
</a>

## COPYRIGHT & LICENSE

- Code & Docs © 2018 Huan LI \<[email protected]\>
- Code released under the Apache-2.0 License
- Docs released under Creative Commons
148 changes: 148 additions & 0 deletions examples/ripe-wechaty.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/**
* Wechaty - Conversational RPA SDK for Chatbot Makers.
* - https:/wechaty/wechaty
*/
import {
Contact,
Message,
ScanStatus,
WechatyBuilder,
log,
types,
} from 'wechaty'
import { FileBox } from 'file-box'

import { PuppetWechat4u } from '../src/puppet-wechat4u.js'
import qrcodeTerminal from 'qrcode-terminal'
import fs from 'fs'

function onScan (qrcode: string, status: ScanStatus) {
if (qrcode) {
const qrcodeImageUrl = [
'https://wechaty.js.org/qrcode/',
encodeURIComponent(qrcode),
].join('')
console.info('StarterBot', 'onScan: %s(%s) - %s', status, qrcodeImageUrl)

qrcodeTerminal.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: Contact) {
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: Contact) {
log.info('StarterBot', '%s logout', user)
}

async function onMessage (msg: Message) {
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 = FileBox.fromFile(newpath)
await msg.say(fileBox)
}

/**
* 发送图片
*/
if (msg.text() === 'jpg') {
const newpath = 'https:/wechaty/wechaty/blob/main/docs/images/bot-qr-code.png'
const fileBox = FileBox.fromUrl(newpath)
await msg.say(fileBox)
}

/**
* 发送表情
*/
if (msg.text() === 'gif') {
const newpath = basepath + 'test.gif'
const fileBox = FileBox.fromFile(newpath)
await msg.say(fileBox)
}

/**
* 发送视频
*/
if (msg.text() === 'mp4') {
const newpath = basepath + 'test.mp4'
const fileBox = FileBox.fromFile(newpath)
await msg.say(fileBox)
}

try {
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) {
const file = await msg.toFileBox() // Save the media message as a FileBox
const filePath = 'examples/file/' + file.name
file.toFile(filePath)
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.appendFileSync(logPath, JSON.stringify(logData, null, 2) + '\n')
log.info(`Logged message data to ${logPath}`)
}
} catch (e) {
console.error(`Error handling message: ${e}`)
}

}

const puppet = new PuppetWechat4u()
const bot = 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(',')
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(',')
log.info(`Room ${await room.topic()} lost member ${nameList}, the remover is: ${remover}`)
})
bot.on('room-topic', async (room, topic, oldTopic, changer) => {
log.info(`Room ${await room.topic()} topic changed from ${oldTopic} to ${topic} by ${changer.name()}`)
})
bot.on('room-invite', async roomInvitation => {
log.info(JSON.stringify(roomInvitation))
try {
log.info('received room-invite event.')
await roomInvitation.accept()
} catch (e) {
console.error(e)
}
})

bot.start()
.then(() => {
return log.info('StarterBot', 'Starter Bot Started.')
})
.catch(console.error)
13 changes: 9 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wechaty-puppet-wechat4u",
"version": "1.14.2",
"version": "1.14.5",
"description": "Wechat4u Puppet for Wechaty",
"type": "module",
"exports": {
Expand All @@ -24,6 +24,8 @@
"lint:ts": "tsc --isolatedModules --noEmit",
"lint:es": "eslint \"src/**/*.ts\" \"tests/**/*.spec.ts\" --ignore-pattern tests/fixtures/",
"start": "cross-env NODE_OPTIONS=\"--no-warnings --loader=ts-node/esm\" node examples/ding-dong-bot.ts",
"start:ripe": "cross-env WECHATY_LOG=verbose NODE_OPTIONS=\"--no-warnings --loader=ts-node/esm\" node examples/ripe-wechaty.ts",
"start:ripe:nolog": "cross-env NODE_OPTIONS=\"--no-warnings --loader=ts-node/esm\" node examples/ripe-wechaty.ts",
"test": "npm run lint && npm run test:unit",
"test:pack": "bash -x scripts/npm-pack-testing.sh",
"test:unit": "cross-env NODE_OPTIONS=\"--no-warnings --loader=ts-node/esm\" tap \"src/**/*.spec.ts\" \"tests/**/*.spec.ts\""
Expand Down Expand Up @@ -53,12 +55,15 @@
"@chatie/semver": "^0.4.7",
"@chatie/tsconfig": "^4.6.2",
"@types/promise-retry": "^1.1.3",
"@types/qrcode-terminal": "^0.12.0",
"@types/xml2js": "^0.4.11",
"memory-card": "^1.0.3"
"memory-card": "^1.0.3",
"qrcode-terminal": "^0.12.0",
"wechaty": "^1.20.2"
},
"peerDependencies": {
"wechaty-puppet": "^1.18.3",
"@swc/core":"1.3.78"
"@swc/core": "1.3.78",
"wechaty-puppet": "^1.18.3"
},
"homepage": "https:/wechaty/wechaty-puppet-wechat4u#readme",
"dependencies": {
Expand Down
2 changes: 1 addition & 1 deletion src/puppet-wechat4u.ts
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@ export class PuppetWechat4u extends PUPPET.Puppet {
* 视频消息
*/
// console.log('视频消息,保存到本地')
return FileBox.fromStream(
return FileBox.fromBuffer(
(await this.wechat4u.getVideo(rawPayload.MsgId)).data,
`message-${id}-video.mp4`,
)
Expand Down

0 comments on commit 9e9f5ff

Please sign in to comment.