# 发送whatsapp消息

## 推送消息给所有接收者 <a href="#fa-song-mo-ban-xiao-xi-shi-yong-zi-zhu-ding-yi-de-bian-liang" id="fa-song-mo-ban-xiao-xi-shi-yong-zi-zhu-ding-yi-de-bian-liang"></a>

当whatsapp公众号发送消息时相当于bot发送消息。需[opt-in](https://developers.facebook.com/docs/whatsapp/guides/opt-in)后才能推送消息，即当天发送过消息的用户才能进行消息的推送。whatsapp把需要推送的消息推送给所有[接收者](https://bothub.gitbook.io/project/advanced-tool/api/whatsapp-xiao-xi-ru-kou)，消息类型可以为文字、图片、文档等。

### 限制

１、一个调用whatsapp接口的频率常态不能超过20 request per second，瞬时值不能超过60 request per second。落实到发消息的层面，发一条消息需要一个request，并且在给用户消息之前还需要有一个request来确认是否可以发push notification给这个用户。因此发消息给新用户的频率限制就是10 users per second。

２、每天可以发送push notification的用户数的限制：

限制分为三个tier，tier1：每天可以最多发notification到1000个用户。tier2：每天可以最多发notification到10000个用户。tier3：每天可以最多发notification到10万个用户。

升级方式：tier1->tier2：需满足在不超过7天内发送notification到2000个用户。 tier2->tier3：需满足在不超过7天内发送notification到20000个用户。

### 发送请求

您可以用任意一个已经启用的API Key([怎样申请APIKey](https://bothubdocs.gitbook.io/project/~/edit/drafts/-LCWfeDNA9QGt2vCxD8D/untitled-2/api-jie-shao/kai-shi-shi-yong-api)) 向手机号用户发送消息，消息可以是文字、图片、文档等。这个请求的说明如下：

| 请求属性      | 属性名          | 说明                                                                                                                                                                                                                |
| --------- | ------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| 地址        |              | <https://api.moto-me.com>[/channels/{](https://whatsappapi.bothub.ai/channels/messages)[bot\_phone](https://whatsappapi.bothub.ai/channels/messages)[}/messages](https://whatsappapi.bothub.ai/channels/messages) |
| 请求方式      | POST         |                                                                                                                                                                                                                   |
| Header    | APIKEY       | 您的API KEY.                                                                                                                                                                                                        |
| Header    | Content-Type | application/json                                                                                                                                                                                                  |
| Form Data | id           | 由WhatsApp分配的消息ID                                                                                                                                                                                                  |
| Form Data | to           | channel的手机号                                                                                                                                                                                                       |
| Form Data | timestamp    | 信息发送的时间戳                                                                                                                                                                                                          |
| Form Data | source       | 消息来源 (botchat/livechat )                                                                                                                                                                                          |
| Form Data | type         | 消息类型（text/image/document/audio）                                                                                                                                                                                   |
| Form Data | text         | 当type=text 时显示。形式如下方Text message样例。                                                                                                                                                                               |
| Form Data | image        | 当type=image 时显示。形式如下方Image message样例。                                                                                                                                                                             |
| Form Data | document     | 当type=document 时显示。形式如下方Document message样例。                                                                                                                                                                       |
| Form Data | audio        | 当type=audio 时显示。形式如下方Audio message样例。                                                                                                                                                                             |

### **请求样例**

{% code title="Text message" %}

```javascript
{
    "id": "<unique message identifier>",
    "to": "<phone number of the user>",
    "timestamp": "<timestamp>",
    "source": "<botchat / livechat>",
    "type": "text",
    "text": {
        "body": "Hello world"
    }
}
```

{% endcode %}

{% code title="Image message" %}

```javascript
{
    "id": "<unique message identifier>",
    "to": "<phone number of the user>",
    "timestamp": "<timestamp>",
    "source": "<botchat / livechat>",
    "type": "image",
    "image": {
        "url": "your-media-url",
        "caption": "your-image-caption",
        "extension": "<see the list of available extensions>"
    }
}
```

{% endcode %}

{% code title="Document message" %}

```javascript
{
    "id": "<unique message identifier>",
    "to": "<phone number of the user>",
    "timestamp": "<timestamp>",
    "source": "<botchat / livechat>",
    "type": "document",
    "document": {
        "url": "your-document-url",
        "caption": "your-document-caption",
        "extension": "<see the list of available extensions>"
    }
}
```

{% endcode %}

{% code title="Audio message" %}

```javascript
{
    "id": "<unique message identifier>",
    "to": "<phone number of the user>",
    "timestamp": "<timestamp>",
    "source": "<botchat / livechat>",
    "type": "document",
    "audio": {
        "url": "your-audio-url",
        "caption": "your-audio-caption",
        "extension": "<see the list of available extensions>"
    }
}
```

{% endcode %}

### 请求返回值

* 成功返回值样例

```javascript
{
    "result": true,
    "data": {}
}
```

* 失败返回值样例

```javascript
{
    "error": {
        "code": 10011,
        "error_subcode": 0,
        "type": "",
        "message": "Request already processed",
        "request_id": 35122312158
    }
}
```

### 错误码列表

| 返回值   | 内容                     |
| ----- | ---------------------- |
| 10000 | 内部错误                   |
| 10001 | 未指定API KEY             |
| 10002 | 无效的API KEY             |
| 10003 | 未指定Request id          |
| 10004 | 未指定调用API的种类            |
| 10005 | 无效的API种类               |
| 10005 | 无效的API种类               |
| 10006 | Bot不存在或者已经被删除          |
| 10011 | Request id与之前的重复       |
| 10100 | 参数无效                   |
| 18000 | 内部错误                   |
| 19000 | 回调超时                   |
| 41006 | 未指定id                  |
| 41007 | 未指定to                  |
| 41008 | 未指定type                |
| 41009 | 未指定timestamp           |
| 41010 | 未指定image               |
| 41011 | 未指定document            |
| 41012 | 未指定text                |
| 41013 | 未指定audio               |
| 41014 | 无效的type                |
| 41015 | 未指定text内的body          |
| 41016 | 未指定document内的caption   |
| 41017 | 未指定document内的url       |
| 41018 | 未指定document内的extension |
| 41019 | 未指定image内的caption      |
| 41021 | 未指定image内的url          |
| 41022 | 未指定image内的extension    |
| 41023 | 未指定audio内的caption      |
| 41024 | 未指定audio内的url          |
| 41025 | 未指定audio内的extension    |
| 41035 | 未指定source              |
| 41036 | source值不正确             |
| 80001 | BSP错误                  |
| 80002 | network error          |

###
