# hapi-wechat

> wechat api for hapi

Latest version **1.0.9** (published 2018-11-21) · MIT license · 0 weekly downloads

## Install

```sh
npm install hapi-wechat
pnpm add hapi-wechat
yarn add hapi-wechat
bun add hapi-wechat
```

## 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.9 |
| Published | 2018-11-21 |
| First published | 2018-11-21 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 9 |
| Unpacked size | 192.7 KB |
| Known vulnerabilities | 0 (+5 in 4 direct dependencies) |
| Install scripts | no |
| GitHub stars | 5 |
| Author | dhso |
| Maintainers | dhso |
| Keywords | hapi, wechat, wechat api |

## Links

- npm: https://www.npmjs.com/package/hapi-wechat
- Repository: https://github.com/dhso/hapi-wechat
- Issues: https://github.com/dhso/hapi-wechat/issues
- npm.io page: https://npm.io/package/hapi-wechat

## Dependencies (9)

- [ejs](https://npm.io/package/ejs.md) ^2.6.1
- [joi](https://npm.io/package/joi.md) ^13.1.1
- [glob](https://npm.io/package/glob.md) ^5.0.3
- [httpx](https://npm.io/package/httpx.md) ^2.1.1
- [colors](https://npm.io/package/colors.md) ^1.3.0
- [xml2js](https://npm.io/package/xml2js.md) ^0.4.19
- [cast-array](https://npm.io/package/cast-array.md) ^1.0.0
- [formstream](https://npm.io/package/formstream.md) >=0.0.8
- [json-bigint](https://npm.io/package/json-bigint.md) ^0.2.3

## Recent versions

- 1.0.9 (latest) — 2018-11-21

## README

# hapi-wechat
Wechat plugin for hapi

# co-wechat-api
```
version: 3.8.1

https://github.com/node-webot/co-wechat-api

http://doxmate.cool/node-webot/co-wechat-api/api.html

```

# install
```
npm install github:dhso/hapi-wechat
```

# options
```
routerPath: '/wechat, required'
handlerPath: 'wechat/*.js', required'
appid: 'your appid, required',
appsecret: 'your appsecret, required'
token: 'your token, required'
encodingAESKey: 'your encodingAESKey, optional'
```

# example
* support type
```
['text', 'image', 'voice', 'video', 'location', 'link', 'subscribe', 'unsubscribe', 'click']
```
* text handler
```javascript
// /wechat/text.js

module.exports = async (inMsg, req, h) => {
    try {
        // wechat api send message
        let res = await h.wechat_api.sendText(inMsg.fromUsername, 'Hello world');
        console.log(res);
        // send common message
        return {
            content: inMsg.content
        }
    } catch (err) {
        // send error message
        return {
            content: err.message
        }
    }
};
```

* subscribe handler
```javascript
// /wechat/subscribe.js

module.exports = (inMsg, req, h) => {
    return {
        msgType: 'news',
        content: [
            {
                title: "欢迎关注ThinkApp",
                description: "这是本人测试用的订阅公众号，欢迎访问我的Oschina Git。",
                url: "https://gitee.com/dhso"
            }
        ]
    }
};
```

* a tuling robot example
```javascript
module.exports = async (inMsg, req, h) => {
    try {
        let reply, tuling = await h.axios.post('http://www.tuling123.com/openapi/api', {
            "key": "your key",
            "userid": inMsg.fromUsername,
            "info": inMsg.content
        });
        switch (tuling.data.code) {
            case 100000:
                reply = { 'content': tuling.data.text };
                break;
            case 200000:
                reply = {
                    'msgType': 'news',
                    'content': [{
                        'title': tuling.data.text,
                        'description': '请点击跳转到结果页面。',
                        'url': tuling.data.url
                    }]
                }
                break;
            case 302000:
                reply = {
                    'msgType': 'news',
                    'content': []
                }
                for (var item of tuling.data.list) {
                    if (reply.content.length >= 8) break;
                    reply.content.push({
                        'title': item.article,
                        'description': item.source,
                        'picurl': item.icon.indexOf('//') == 0 ? 'http:' + item.icon : item.icon,
                        'url': item.detailurl
                    });
                }
                break;
            case 308000:
                reply = {
                    'msgType': 'news',
                    'content': []
                }
                for (var item of tuling.data.list) {
                    if (reply.content.length >= 8) break;
                    reply.content.push({
                        'title': item.name,
                        'description': item.info,
                        'picurl': item.icon.indexOf('//') == 0 ? 'http:' + item.icon : item.icon,
                        'url': item.detailurl
                    });
                }
                break;
        }
        return reply;
    } catch (err) {
        return {
            content: err.message
        }
    }
};
```

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