# 購物車下單後推播訊息

## 目錄

1. [情境概述](#qing-jing-gai-shu)
2. [取得MantaGO Bot的ID](#qu-de-mantago-bot-de-id)
3. [拿（更新）Token和Secret Key ](#na-geng-xin-token-he-secret-key)
4. [推播訊息](#tui-bo-xun-xi)
   1. [推送給Line用戶](#tui-song-te-ding-dui-hua-mo-zu-gei-te-ding-line-yong-hu)
   2. [推送給Facebook用戶](#tui-song-wen-zi-gei-te-ding-facebook-yong-hu)

***

## 情境概述

用戶將購物車的商品做下單、並且結帳成功後，傳送後續的訊息給用戶，例如：提醒用戶結帳成功，可以至購物記錄查看商品的最新進度。

***

## 取得MantaGO Bot的ID

MantaGO Bot的ID在後續的API使用，都需要在request query或body之中，作為的botId的value進行帶入。

1. 例如，有下面幾個機器人：

<figure><img src="/files/xJbtZpfZzh7uDdu5Zdjz" alt=""><figcaption></figcaption></figure>

2. 要拿皮卡丘的機器人的Bot ID，點進去皮卡丘的機器人：

<figure><img src="/files/EkxOxBwuZIv8meVUiuRW" alt=""><figcaption></figcaption></figure>

3. 取得網址：`https://mantago.cc/chatbot/lNbWoobyg`。
4. 擷取網址內容的`/chatbot/`後的文字：`lNbWoobyg`，這個`lNbWoobyg`就是為皮卡丘的機器人的Bot ID。

***

## 拿（更新）Token和Secret Key

參考 [如何取得Token](/chang-jian-wen-ti/ru-he-qu-de-token.md)的方法來取得打API用的Token和Secret Key。

如果API用的Token和Secret Key過期了，會得到以下錯誤：

```javascript
{
    "success": false,
    "errResponse": {
        "message": "Token is invalid or expired",
        "code": 1004,
        "botId": "MantaGO Bot ID"
    }
}
```

可以打換發API Token和Secret Key的API來取得新的API Token和Secret Key。

這裡提供換發API Token和Secret Key的程式碼範例：

```javascript
const axios = require('axios');

const secret = "API Secret Key"
const token = "API Token"
const botId = "MantaGO Bot ID"

const data = { "secret": secret };

const config = {
  method: 'post',
  url: 'https://mantago.cc/api/openapi/token?botId=botId',
  headers: { 
    'Content-Type': 'application/json', 
    'Authorization': `Bearer ${token}`,
  },
  data : data
};

async function makeRequest() {
  try {
    const response = await axios.request(config);
    console.log(response.data);
  }
  catch (error) {
    console.log(error);
  }
}

makeRequest();

```

***

## 推播訊息

有了MantaGO Bot的ID以及有效的API Token和Secret Key，就可以進行推播。

從 [Broken mention](broken://pages/IAmujyCdqC2sGC36DJ67)的內容，在推播訊息的時候，有以下兩種選擇：

* 推送內容：文字或是特定對話模組。
* 平台用戶：以下將以Line和Facebook為例。

以下提供 『推送對話模組給特定Line用戶』 和 『推送文字給特定Facebook用戶』 兩種範例。

### 推送對話模組給特定Line用戶

傳送對話模組：

<figure><img src="/files/JHuKJEgqEQfdMXghcJks" alt=""><figcaption></figcaption></figure>

1. 進入對話模組取得這個對話模組的ID、拿網址：`https://mantago.cc/intent/YqaQwqdnj/rb2kYmMdW`、擷取最後一個『 / 』之後的文字，此對話模組的ID便是`rb2kYmMdW`。
2. 去打推播的API；推播訊息的程式碼範例：

```javascript
const axios = require('axios');

const secret = "API Secret Key"
const token = "API Token"
const botId = "MantaGO Bot ID"
const lineUUidList = [
    "Line user uuid"
  ]

const data = {
  "intentId": "rb2kYmMdW",
  "botId": botId,
  "clientIds": lineUUidList,
  "platform": "line",
  "secret": secret
};

const config = {
  method: 'post',
  url: 'https://mantago.cc/api/openapi/broadcast',
  headers: { 
    'Content-Type': 'application/json', 
    'Authorization': `Bearer ${token}`,
    },
  data : data
};

async function makeRequest() {
  try {
    const response = await axios.request(config);
    console.log(response.data);
  }
  catch (error) {
    console.log(error);
  }
}

makeRequest();

```

3. 推送成功的API Response：

```
{
    "success": true,
    "message": "Send customer broadcast successfully"
}
```

4. 訊息：

<figure><img src="/files/oVV4JpRGL724l2luNq9X" alt=""><figcaption></figcaption></figure>

### 推送文字給特定Facebook用戶

推送文字內容 ：『親愛的顧客您好，你已成功下單，可以至消費紀錄查看商品的最新進度』。

1. 去打API，推播訊息的程式碼範例：

```javascript
const axios = require('axios');

const secret = "API Secret Key"
const token = "API Token"
const botId = "MantaGO Bot ID"
const facebookUUidList = [
    "Facebook user uuid"
  ]

const data = {
  "message": "親愛的顧客您好，你已成功下單，可以至消費紀錄查看商品的最新進度",
  "botId": botId,
  "clientIds": facebookUUidList,
  "platform": "facebook",
  "fbTag": "POST_PURCHASE_UPDATE",
  "secret": secret
};

const config = {
  method: 'post',
  url: 'https://mantago.cc/api/openapi/broadcast',
  headers: { 
    'Content-Type': 'application/json', 
    'Authorization': `Bearer ${token}`,
    },
  data : data
};

async function makeRequest() {
  try {
    const response = await axios.request(config);
    console.log(response.data);
  }
  catch (error) {
    console.log(error);
  }
}

makeRequest();

```

2. 推送成功的API Response：

```
{
    "success": true,
    "message": "Send customer broadcast successfully"
}
```

3. 訊息：

<figure><img src="/files/QQcASCsZ3IsBMZnEOq5p" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://doc.mantago.cc/qing-jing-fan-li/gou-wu-ju-xia-dan-hou-tui-bo-xun-xi.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
