# egg-duoxing-bot

> Bot plugin of duoxing chat.

Latest version **1.0.0-alpha-1** (published 2022-01-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install egg-duoxing-bot
pnpm add egg-duoxing-bot
yarn add egg-duoxing-bot
bun add egg-duoxing-bot
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0-alpha-1 |
| Published | 2022-01-27 |
| First published | 2022-01-27 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=8.0.0 |
| Dependencies | 4 |
| Unpacked size | 21.7 KB |
| Known vulnerabilities | 0 (+23 in 1 direct dependencies) |
| Install scripts | no |
| Maintainers | colin_liu |
| Keywords | egg, eggPlugin, egg-plugin |

## Links

- npm: https://www.npmjs.com/package/egg-duoxing-bot
- Repository: https://github.com/eggjs/egg-egg-duoxing-bot
- Homepage: https://github.com/eggjs/egg-egg-duoxing-bot#readme
- Issues: https://github.com/eggjs/egg/issues
- npm.io page: https://npm.io/package/egg-duoxing-bot

## Dependencies (4)

- [egg](https://npm.io/package/egg.md) ^2.16.0
- [axios](https://npm.io/package/axios.md) ^0.24.0
- [dayjs](https://npm.io/package/dayjs.md) ^1.10.5
- [socket.io-client](https://npm.io/package/socket.io-client.md) ^4.4.0

## Recent versions

- 1.0.0-alpha-1 (latest) — 2022-01-27
- 1.0.0-alpha-3 (alpha) — 2022-01-29
- 1.0.0-alpha-2 — 2022-01-28

## README

# egg-egg-duoxing-bot

多星机器人

## socket 操作设计说明

Agent 端：

1. 初始化时，执行登录操作，并将 authToken 下发给所有 worker 端
2. 维护 authToken，快过期时刷新并同步给所有 worker 端
3. 监听其它 ipc 消息，执行发 socket 的操作
4. 当有 socket 回来时，分发给随机一个 worker 处理

Worker 端：

1. 监听 agent 发过来的消息，调用对应的 listener
2. 当需要发 socket 消息时，通过 ipc 调用通知 agent 端操作

## Install

```bash
$ npm i egg-egg-duoxing-bot --save
```

## Usage

### 发消息、调用接口

```js
await app.duoxingClient.sendMsgToPerson('hi, this is send by bot', 'm3659N');
await app.duoxingClient.sendMsgToGroup('hi, this is send by bot', '73f9d78c-d5ce-17f2-bfe1-e1d30eb6a5f0');
```

### 收消息并做出响应

在 listener 目录中，继承 BaseListener 类，可实现监听指定的 action 的消息：

```js
class HelloListener extends BaseListener {

    /**
     * 配置
     */
    static get config() {
        return {
            // 监听的 action 列表
            listenActions: ['userMessage'],
            // 开关
            disable: false,
        };
    }

    /**
     * 处理 action 的回调函数
     */
    async handle(actionId, message) {
        console.log('[HelloListener]', `handle actionId${actionId}, message: ${message}`);
        const {fromUserId, fromUsername, toGroupId, messageContentType, messageContent} = message.data.params;
        // 过滤自己的发言
        if (fromUserId === this.duoxingClient.duoxingConfig.userId) {
            return;
        }
        if (actionId === 'userMessage') {
            await this.duoxingClient.sendMsgToPerson('hello world!', fromUserId);
        }
    }

}
```

当然你也可以监听其它事件，比如群组消息、上下线消息等等。

如果你的机器人只需要关心群组和个人消息，我们已经为你封装了一个类，即 BaseMessageListener：

```js
class EchoListener extends BaseMessageListener {

    /**
     * 配置
     */
    static get config() {
        return {
            // 监听的 action 列表
            listenActions: super.config.listenActions,
            // 开关
            disable: false,
        };
    }

    /**
     * 回复用户消息，当收到 text 消息回复
     */
    replyUser(messageContentType, messageContent, fromUserId, fromUsername) {
        if (messageContentType !== 'text') {
            return `你好呀，[${fromUsername}]，暂无法处理 ${messageContentType} 类型的消息`;
        }
        return `你好呀，[${fromUsername}]，你刚才发的消息是【${messageContent}】，机器人更多功能尽请期待~`;
    }

    /**
     * 回复群组消息，当收到 text 消息，并包含 /hi 时做回复
     */
    replyGroup(messageContentType, messageContent, fromUserId, fromUsername) {
        if (messageContentType !== 'text') {
            return;
        }
        if (messageContent.includes('/hi')) {
            return `你好呀，[${fromUsername}]，你刚才发的消息是【${messageContent}】，机器人更多功能尽请期待~`;
        }
    }

}
```

## Configuration

```js
// {app_root}/config/config.default.js
exports.duoxingBot = {
  server: 'http://local1.duoxing.org:8082',
  userId: '10001',
  password: '123456',
};
```

see [config/config.default.js](config/config.default.js) for more detail.

## Example

<!-- example here -->

## Questions & Suggestions

Please open an issue [here](https://github.com/eggjs/egg/issues).

## License

[MIT](LICENSE)

---
_Source: https://npm.io/package/egg-duoxing-bot · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
