Skip to content

Commit

Permalink
chore: handle message
Browse files Browse the repository at this point in the history
  • Loading branch information
shfshanyue committed Mar 23, 2023
1 parent 367b682 commit df042e1
Show file tree
Hide file tree
Showing 9 changed files with 5,157 additions and 4,950 deletions.
15 changes: 14 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,25 @@

> Wechaty is a Bot SDK for Wechat Individual Account which can help you create a bot in 6 lines of javascript, with cross-platform support including Linux, Windows, MacOS, and Docker.
## 目录结构

``` bash
$ tree -L 1
.
├── Readme.md
├── event/ # 关于 wechaty 的事件处理程序
├── message/
├── schedule
├── config.ts
└── index.ts
```

## 步骤

1. 开启一个微信机器人,使用将要作为机器人的微信扫码进行登录

``` bash
$ yarn start
$ npm start
```

2. 与机器人对话,机器人默认原样回复
Expand Down
2 changes: 2 additions & 0 deletions config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ export default {

// 自动同意添加好友的口令
acceptText: //,

logger: true
}
34 changes: 17 additions & 17 deletions event/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@ import { Message } from 'wechaty'
import { Message as MessageType } from 'wechaty-puppet/types'
import { routes } from '../message'

async function defaultFilter(msg: Message) {
return msg.type() === MessageType.Text && !msg.room()
}

export async function handleMessage(msg: Message) {
if (msg.type() === MessageType.Text) {
if (
!msg.room() ||
((await msg.mentionSelf()) &&
msg.room()?.owner()?.name()?.includes('山月'))
) {
const self = msg.listener()
const text = msg.text().replace('@' + self?.name(), '') || ''
const route = routes.find((route) => {
const keyword = route.keyword
if (typeof keyword === 'string') {
return text.includes(keyword)
}
return keyword.test(text)
})
const data = await route.handle(text)
await msg.say(data)
const self = msg.listener()
const text = msg.text().replace('@' + self?.name(), '') || ''
const route = routes.find((route) => {
const keyword = route.keyword
if (typeof keyword === 'string') {
return text.includes(keyword)
}
return keyword.test(text)
})
const filter = await (route.filter || defaultFilter)(msg)
if (!filter || !route) {
return
}
const data = await route.handle(text)
await msg.say(data)
}
2 changes: 1 addition & 1 deletion message/echo.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export function echo (text: string) {
export function handle (text: string) {
return text
}
2 changes: 1 addition & 1 deletion message/fund.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function getFunds () {
})
}

export async function topFund (): Promise<string> {
export async function handle (): Promise<string> {
const funds = await getFunds()
return funds.map((fund: string[]) => `${fund[0]}: ${fund[1]}`).join('\n')
}
21 changes: 14 additions & 7 deletions message/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import { Sayable } from 'wechaty';
import { echo } from './echo';
import { Message, Sayable } from 'wechaty'
import * as echo from './echo'
import * as fund from './fund'

type Route = {
keyword: string | RegExp;
handle: ((text: string) => Sayable) | ((text: string) => Promise<Sayable>);
handle: ((text: string) => Sayable) | ((text: string) => Promise<Sayable>)
keyword: string | RegExp
filter?: (msg: Message) => boolean
}

export const routes: Route[] = [
{ keyword: '', handle: echo },
{ keyword: '基金', handle: fund.topFund },
]
{
keyword: '/ping',
handle() {
return 'pong'
},
},
{ keyword: '基金', handle: fund.handle },
{ keyword: '', handle: echo.handle },
]
Loading

0 comments on commit df042e1

Please sign in to comment.