Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

send at msg fixed #205

Merged
merged 31 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
0863b88
3.9.2.23 init (#180)
choogoo Jul 11, 2023
ad0e0e3
3.9.2.23 init (#182)
choogoo Jul 11, 2023
3faaa0b
适配3.9.2.23 (#185)
choogoo Sep 21, 2023
865ef7f
add 1.3.0 illustrate
atorber Sep 21, 2023
7bfb22c
Update README.md
atorber Sep 21, 2023
e2fe791
Update README.md
atorber Sep 21, 2023
5ae42d1
Merge pull request #2 from atorber/3.9.2.23
atorber Sep 21, 2023
eae4d90
add demo
atorber Sep 21, 2023
240252a
1.13.1
atorber Nov 14, 2023
6d3f020
Merge branch 'main' into 3.9.2.23
atorber Nov 14, 2023
b741379
1.13.1
atorber Nov 14, 2023
60c8f7d
Merge branch '3.9.2.23' of https://github.com/atorber/puppet-xp into …
atorber Nov 14, 2023
ad155b2
1.13.2 dev
atorber Nov 19, 2023
d7d72c7
Merge pull request #3 from atorber/3.9.2.23
atorber Nov 20, 2023
0d6c8ec
增加扫码登录、检测登录状态
atorber Nov 22, 2023
5e8a1ef
增加登入登出事件
atorber Nov 23, 2023
6a0afa8
增加ts改造init
atorber Dec 29, 2023
4b7892d
1.13.2
atorber Jan 1, 2024
2788998
1.13.2
atorber Jan 2, 2024
d397db3
1.13.2
atorber Jan 5, 2024
a68a3ee
Merge branch 'main' into 3.9.2.23
atorber Jan 5, 2024
a9b060f
1.13.2
atorber Jan 9, 2024
cc171f4
1.13.2
atorber Jan 9, 2024
7f8d6f1
Update .npmignore
atorber Jan 9, 2024
c1575cd
Update .gitignore
atorber Jan 9, 2024
60333db
1.13.2删除agent的ts文件
atorber Jan 10, 2024
77d2075
1.13.4
atorber Jan 10, 2024
d1fb18b
1.13.5
atorber Jan 12, 2024
8243c3b
Update init-agent-script.ts
atorber Jan 12, 2024
e9f16c5
1.13.5
atorber Jan 13, 2024
e5c6981
1.13.6
atorber Jan 19, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const rules = {
"no-shadow": "off"
}

module.exports = {
extends: '@chatie',
"ignorePatterns": ["src/init-agent-script.ts", "src/init-agent-script.js"],
rules,
}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,6 @@ typings/
t/
t.*
cache/*
# src/init-agent-script.ts
examples/file/*
examples/media/*
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
docs/
src/init-agent-script.ts
75 changes: 42 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,21 @@ wechaty-puppet-xp is a local puppet for Wechaty:
## GETTING STARTED

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

## QUICK START
> 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.

> 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.
- STEP 2: Login the wechat client on the computer.
- STEP 3: Getting Started with TypeScript/JavaScript (RECOMMENDED).

```sh
git clone https://github.com/wechaty/wechaty-puppet-xp.git
cd wechaty-puppet-xp

# 安装依赖
npm install

# 启动程序
npm start
#
# Do not forget to install WeChat with requried version and login.
#
Expand All @@ -48,46 +52,51 @@ npm install
| `npm run start:ripe` | [examples/ripe-wechaty.ts](examples/ripe-wechaty.ts) | Wechaty ding/dong |
| `npm run start:raw` | [examples/raw-sidecar.ts](examples/raw-sidecar.ts) | Sidecar ding/dong |

## RUNNING WHITH NPM

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).

## PUPPET COMPARISON

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

Puppet|xp👍|
:---|:---|
支持账号|个人微信|
版本|3.3.0.115|3.6.0.18|3.9.2.23|
:---|:---|:---|:---|
**<消息>**|
接收文本|✅
接收图片|✅
接收文件|✅
接收动图|✅
接收表情|✅
接收小程序卡片|✅
接收联系人卡片|✅
接收位置卡片|✅
发送文本|✅
发送图片|✅
发送文件|✅
发送动图|✅(以文件形式发送)
接收文本|✅|✅|✅
接收图片|✅|✅|✅
接收文件|✅|✅|✅
接收动图|✅|✅|✅
接收表情|✅|✅|✅
接收小程序卡片|✅|✅|✅
接收联系人卡片|✅|✅|✅
接收位置卡片|✅|✅|✅
发送文本|✅|✅|✅
发送图片|✅|✅|✅
发送文件|✅|✅|✅
发送动图|✅|✅|✅
**<群组>**|
@群成员|✅
群列表|✅
群成员列表|✅
群详情|✅
进群提示|✅
@群成员|✅|✅|✅
群列表|✅|✅|✅
群成员列表|✅|✅|✅
群详情|✅|✅|✅
进群提示|✅|✅|✅
**<联系人>**|
好友列表|✅
好友详情|✅
好友列表|✅|✅|✅
好友详情|✅|✅|✅
**<其他>**|
登录事件|✅
依赖协议|Windows
登录事件|✅|✅|✅
扫码登录|||✅

## VERSION SUPPORT

puppet-xp|wechat|
|:---|:---|
|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)|
|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)|
|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)|
Note: You need to install an NPM version that matches your WeChat client version.

puppet-xp|wechat|npm install|
|:---|:---|:---|
|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|
|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]|
|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]|

## HISTORY

Expand Down
265 changes: 265 additions & 0 deletions examples/demo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,265 @@
#!/usr/bin/env -S node --no-warnings --loader ts-node/esm
/**
* wechaty-puppet-xp示例代码,可以作为模版编写自己的业务逻辑.
*
**/
import 'dotenv/config.js'

import {
Contact,
Message,
ScanStatus,
WechatyBuilder,
log,
types,
} from 'wechaty'

import qrcodeTerminal from 'qrcode-terminal'
import { FileBox } from 'file-box'
import { PuppetXp } from '../src/puppet-xp.js'

const onScan = (qrcode: string, status: ScanStatus) => {
if (status === ScanStatus.Waiting || status === ScanStatus.Timeout) {
const qrcodeImageUrl = [
'https://wechaty.js.org/qrcode/',
encodeURIComponent(qrcode),
].join('')
log.info('onScan: %s(%s) - %s', ScanStatus[status], status, qrcodeImageUrl)

qrcodeTerminal.generate(qrcode, { small: true }) // show qrcode on console

} else {
log.info('onScan: %s(%s)', ScanStatus[status], status)
}
}

const onLogin = async (user: Contact) => {
log.info('onLogin', '%s login', user)
// 登录成功后调用bot
main()
}

const onLogout = (user: Contact) => {
log.info('onLogout', '%s logout', user)
}

const onMessage = async (msg: Message) => {
log.info('onMessage', JSON.stringify(msg))
// Message doc : https://wechaty.js.org/docs/api/message#messageage--number

const talker = msg.talker() // 发消息人
const listener = msg.listener() // 接收消息人
const room = msg.room() // 是否是群消息
const text = msg.text() // 消息内容
const type = msg.type() // 消息类型
const self = msg.self() // 是否自己发给自己的消息

log.info('talker', JSON.stringify(talker))
log.info('listener', listener||'undefined')
log.info('room', room || 'undefined')
log.info('text', text)
log.info('type', type)
log.info('self', self?'true':'false')

try {
switch (text) {
case 'ding': // 接收到的消息是ding,回复dong
await msg.say('dong')
break
case 'send text': // 接收到的消息是send text,发送文本消息
await msg.say('this is a test text')
break
case 'send image': // 接收到的消息是send image,发送图片
const image = FileBox.fromUrl('https://wechaty.js.org/assets/logo.png')
await msg.say(image)
break
case 'send file': // 接收到的消息是send file,发送文件
const fileBox = FileBox.fromUrl('https://wechaty.js.org/assets/logo.png')
await msg.say(fileBox)
break
case 'send video': // 接收到的消息是send video,发送视频
const video = FileBox.fromUrl('http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4')
await msg.say(video)
break
case 'send audio': // 接收到的消息是send audio,发送音频
const audio = FileBox.fromUrl('http://www.zhongguoyinhang.com/upload/2018-11/201811161154314128.mp3')
await msg.say(audio)
break
case 'send emotion': // 接收到的消息是send emotion,发送表情
const emotion = FileBox.fromUrl('https://res.wx.qq.com/mpres/htmledition/images/icon/emotion/0.gif')
await msg.say(emotion)
break
default:
break
}
} catch (e) {
console.log('回复消息失败...', e)
}

try{
switch (type) {
case types.Message.Text: // 接收到的消息是文本
log.info('接收到的消息是文本')
log.info('消息内容:', text)
break
case types.Message.Image: // 接收到的消息是图片
log.info('接收到的消息是图片')
const image = await msg.toImage().thumbnail() // Save the media message as a FileBox
const filePath = 'examples/file/' + image.name
try {
image.toFile(filePath, true)
log.info(`Saved file: ${filePath}`)
} catch (e) {
log.error('保存文件错误:', e)
}
break
case types.Message.Attachment: // 接收到的消息是附件
log.info('接收到的消息是附件')
break
case types.Message.Video: // 接收到的消息是视频
log.info('接收到的消息是视频')
break
case types.Message.Audio: // 接收到的消息是音频
log.info('接收到的消息是音频')
break
case types.Message.Emoticon: // 接收到的消息是表情
log.info('接收到的消息是表情')
break
case types.Message.Url: // 接收到的消息是链接
log.info('接收到的消息是链接')
break
case types.Message.MiniProgram: // 接收到的消息是小程序
log.info('接收到的消息是小程序')
break
case types.Message.Transfer: // 接收到的消息是转账
log.info('接收到的消息是转账')
break
case types.Message.RedEnvelope: // 接收到的消息是红包
log.info('接收到的消息是红包')
break
case types.Message.Recalled: // 接收到的消息是撤回的消息
log.info('接收到的消息是撤回的消息')
break
default:
break
}
}catch(e){
console.log('处理消息失败...', e)
}
// 关键词回复,同时也是发送消息的方法
}

// const bot = WechatyBuilder.build({
// name: 'ding-dong-bot',
// puppet: 'wechaty-puppet-xp',
// puppetOptions: {
// version: '3.9.2.23',
// }
// })

const puppet = new PuppetXp({wechatVersion:'0.0.0.0'})
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.start()
.then(async () => {
log.info('StarterBot', 'Starter Bot Started.')
})
.catch(e => log.error('StarterBot', e))

const main = async () => {

// 获取当前登录微信信息
try {
const self = bot.currentUser
log.info('当前登录账号信息:', self)
} catch (e) {
log.error('get user failed', e)
}

// 通过微信号搜索联系人
try {
const contactById = await bot.Contact.find({
id: 'filehelper'
})
log.info('微信号查找联系人:', contactById)
// 向联系人发送消息
contactById?.say('向指定好友微信号发送消息')
} catch (e) {
log.error('contactByWeixin', e)
}

// 通过昵称搜索联系人
try {
const contactByName = await bot.Contact.find({
name: '文件传输助手'
})
log.info('昵称查找联系人:', contactByName)
// 向联系人发送消息
contactByName?.say('向指定好友昵称发送消息')
} catch (e) {
log.error('contactByName', e)
}

// 通过备注搜索联系人
try {
const contactByAlias = await bot.Contact.find({
alias: '超哥'
})
log.info('备注名称查找联系人:', contactByAlias || '没有找到联系人')
// 向联系人发送消息
contactByAlias?.say('向指定好友备注好友发送消息')
} catch (e) {
log.error('contactByAlias', e)
}

try {
// 通过群ID搜索群
const roomById = await bot.Room.find({
id: '21341182572@chatroom'
})
log.info('群ID查找群:', roomById)
// 向群里发送消息
roomById?.say('向指定群ID发送消息')
} catch (e) {
log.error('roomById', e)
}

try {
// 通过群名称搜索群
const roomByName = await bot.Room.find({
topic: '大师是群主'
})
log.info('群名称查找群:', roomByName || '没有找到群')
// 向群里发送消息
roomByName?.say('向指定群名称发送消息')
} catch (e) {
console.log('roomByName', e)
}

try {
// 获取所有联系人
const contactList = await bot.Contact.findAll()
// log.info('获取联系人列表:', contactList)
log.info('联系人数量:', contactList.length)
} catch (e) {
log.error('contactList', e)
}

try {
// 获取所有群
const roomList = await bot.Room.findAll()
// log.info('获取群列表:', roomList)
log.info('群数量:', roomList.length)
} catch (e) {
log.error('roomList', e)
}

}
Loading
Loading