# PUPPET-MINI

[![NPM Version](https://badge.fury.io/js/wechaty-puppet-mini.svg)](https://badge.fury.io/js/wechaty-puppet-mini)
[![npm (tag)](https://img.shields.io/npm/v/wechaty-puppet-mini/next.svg)](https://www.npmjs.com/package/wechaty-puppet-mini?activeTab=versions)
[![NPM](https://github.com/leochen-g/wechaty-puppet-mini/workflows/NPM/badge.svg)](https://github.com/leochen-g/wechaty-puppet-mini/actions?query=workflow%3ANPM)

[![Powered by Wechaty](https://img.shields.io/badge/Powered%20By-Wechaty-brightgreen.svg)](https://github.com/wechaty/wechaty)
[![TypeScript](https://img.shields.io/badge/%3C%2F%3E-TypeScript-blue.svg)](https://www.typescriptlang.org/)

## 运行方法

### 安装依赖

~~~shell
npm install @juzi/wechaty
npm install wechaty-puppet-mini
~~~

### 编写代码

~~~typescript
import { WechatyBuilder } from '@juzi/wechaty'

WechatyBuilder.build()  // get a Wechaty instance
  .on('message',       message => console.log(`Message: ${message}`))
  .start()
~~~

> Learn more for building your first Wechaty bot at <https://github.com/wechaty/>, <https://github.com/wechaty/getting-started>

## 消息种类支持

| 消息类型 | 从属(根据接口返回) | api                    | 接收 | 发送 |
| -------- | ------------------ | ---------------------- | ---- | ---- |
| 文本     | `text`             | `message.text`         | ✅    | ✅    |
| 图片     | `image`            | `message.toImage()`    | ✅    | ✅    |
| 视频     | `video`            | `message.toFilebox()`  | ✅    | ✅   |
| 文件     | `other`            | `message.toFilebox()`  | ✅    | ✅    |

## 使用示例

### 1. 可配置参数

- `token`、`chatbotId`、 `mqName`和`mqPassword`可以通过环境变量或者此处传入。
- `port`可以指定 koa 服务监听的端口和路由前缀，默认是8077。

~~~ts
new PuppetMini({
  token: 'mini_1325g3v4vs4xvu7snst',
  chatbotId: 'juzi_1qaz@WSX',
  mqName: 'kbl',
  mqPassword: '1qaz@WSX',
})
~~~

### 2. 创建实例

~~~ts
const bot = WechatyBuilder.build({
  puppet: new PuppetMini({
    token: 'mini_1325g3v4vs4xvu7snst',
    chatbotId: 'juzi_1qaz@WSX',
    mqName: 'kbl',
    mqPassword: '1qaz@WSX',
  }),
})  // get a Wechaty instance
  .on('login', (user: any) => log.info(`User ${user} logged in`))
  .on('message', async (message: Message) => {
    log.info(`Message: ${message}`)
  })

await bot.start()

const contact = await bot.Contact.find({ id: 'xxxxxxxxxxx' })
~~~

### 3. 文本消息

~~~ts
await contact.say('This is a simple text message.')
~~~

### 4. 图片消息

~~~ts
contact.say(FileBox.fromFile('C:\\Users\\Desktop\\1.png'))
~~~

## Wechaty

[Wechaty](https://wechaty.js.org) 是一个开源聊天机器人框架SDK，具有高度封装、高可用的特性，支持NodeJs, Python, Go 和Java 等多语言版本。在过去的5年中，服务了数万名开发者，收获了 Github 的 9600 Star。同时配置了完整的DevOps体系并持续按照Apache 的方式管理技术社区。

## 项目技术栈

1. TypeScript programming language
2. Git
3. REST Api

## 相关链接

- [Wechaty](https://wechaty.js.org/v/zh/)
- [Koa](https://koa.bootcss.com/)
- [TypeScripts中文手册](https://www.tslang.cn/docs/handbook/basic-types.html)
