# Jkfans 直播
###### tags: `facebook` `page` `messenger` `live` `bot`
## 需求
* 在粉絲專頁直播時針對 viewer(s) 的**留言**<sub>(comments)</sub>做自動回應;例如:*+1*、*贊助$1000*等。
## Facebook Graph API
The Graph API is the primary way for apps to read and write to the Facebook social graph. All of Facebook SDKs and products interact with the Graph API in some way, and other APIs are extensions of the Graph API, so understanding how the Graph API works is crucial.
### 概览
The Graph API is the primary way to get data into and out of the Facebook platform. It's an HTTP-based API that apps can use to programmatically query data, post new stories, manage ads, upload photos, and perform a wide variety of other tasks.
#### The Basics
The Graph API is named after the idea of a "social graph" — a representation of the information on Facebook. It's composed of:
* **nodes** — basically individual objects, such as a User, a Photo, a Page, or a Comment.
* **edges** — connections between a collection of objects and a single object, such as Photos on a Page or Comments on a Photo.
* **fields** — data about an object, such as a User's birthday, or a Page's name.
Typically you use **nodes** to get data about a specific object, use **edges** to get collections of objects on a single object, and use **fields** to get data about a single object or each object in a collection.
#### Structure
In general we:
* use **nodes** to get data about individual objects.
* use **edges** to get collections of objects connected to a node, or to publish objects to those collections.
* use **fields** to specify which data you want included in responses.
資料來源:https://developers.facebook.com/docs/graph-api/
## Pages API
使應用程式可建立、管理維護粉絲專頁的內容(或設定)。
### Components
#### Access Tokens
大多數 endpoints 都需要 [Page access tokens](https://developers.facebook.com/docs/facebook-login/access-tokens#pagetokens),每個粉絲專頁、應用程式、應用程式用戶都是唯一的並具有逾期時間。
#### Permissions
大部份 endpoints 都需要應用程式用戶透過 Facebook Login 授權給應用程式,所有的粉絲專頁權限都須通過應用程式審查。
> 在2020年05月05日開放了六個新的粉絲專頁權限來取代舊有的、即將於2022年05月淘汰的`manage_pages`、`publish_pages`。
| Permission | Description | Dependent on |
| - | - | - |
| `pages_messaging`[✅](https://developers.facebook.com/docs/apps/review "須應用程式審查") | 允許**應用程式**在 **Messenger** 取得、管理維護**粉絲專頁**的對話。 | `pages_manage_metadata` |
| `pages_show_list` | 允許**應用程式**顯示某用戶管理維護的**粉絲專頁**列表。 | |
| `pages_manage_ads`[✅](https://developers.facebook.com/docs/apps/review "須應用程式審查") | 允許**應用程式**管理維護與**粉絲專頁**的廣告。 | |
| `pages_manage_cta`[✅](https://developers.facebook.com/docs/apps/review "須應用程式審查") | 允許**應用程式**對**粉絲專頁**的**按鈕**執行`POST`和`DELETE`。 | |
| `pages_manage_engagement`[✅](https://developers.facebook.com/docs/apps/review "須應用程式審查") | 允許**應用程序**管理維護**粉絲專頁**上發表的**留言**;搭配`pages_read_user_content`還能刪除**其它**粉絲專頁發表的留言。 | `pages_read_user_content` |
| `pages_manage_instant_articles`[✅](https://developers.facebook.com/docs/apps/review "須應用程式審查") | 允許以**應用程式用戶**所管理的**粉絲專頁身份**維護 [Instant Articles<sub>(即時文章)</sub>](https://www.facebook.com/facebookmedia/solutions/instant-articles)。 | |
| `pages_manage_metadata`[✅](https://developers.facebook.com/docs/apps/review "須應用程式審查") | 允許**應用程式**訂閱、接收**粉絲專頁**用的 **webhooks** 並可管理維護粉絲專頁的**設定**。 | |
| `pages_manage_posts`[✅](https://developers.facebook.com/docs/apps/review "須應用程式審查") | 允許**應用程式**管理維護**粉絲專頁**的**貼文**;搭配`pages_read_user_content`還能刪除**粉絲**撰寫在粉絲專頁上的貼文。 | `pages_read_engagement` |
| `pages_read_engagement`[✅](https://developers.facebook.com/docs/apps/review "須應用程式審查") | 允許**應用程式**讀取**粉絲專頁**發表的內容(**貼文**、**相片**、**影片**、**活動**)、追蹤者的資料(含**粉絲專頁範圍編號**)。 | |
| `pages_read_user_content`[✅](https://developers.facebook.com/docs/apps/review "須應用程式審查") | 允許**應用程式**讀取**粉絲**在**粉絲專頁**上生成的內容,諸如**貼文**、**留言**和**評分**。 | |
估計需要以下權限:
* `pages_manage_metadata`
> 訂閱、接收**粉絲專頁**用的 webhooks。
* `pages_messaging`
> 在 Messenger 取得、管理維護粉絲專頁的對話。
* `pages_read_user_content`
> 讀取粉絲在粉絲專頁上的留言。
* `pages_manage_engagement`
> 管理維護粉絲專頁上發表的留言。
* `pages_read_engagement`
> 讀取粉絲專頁發表的影片、追蹤者的資料(含粉絲專頁範圍編號)。
* `pages_manage_posts`
> 管理維護粉絲專頁的貼文。
#### [粉絲專頁範圍編號](https://developers.facebook.com/docs/messenger-platform/identity/id-matching "Page-Scoped User IDs")
Users who interact with Pages are identified by Page-Scoped User IDs (PSID). PSIDs are IDs that are unique to each User-Page pair. Pages API and Messenger API endpoints rely on PSIDs, so you can use a PSID to identify a User's interactions with a Page, as well as the User's public Messenger conversations with that Page.
> **App-Scoped User IDs**
>
> Before May 1, 2018, Pages API endpoints relied on App-Scoped User IDs (ASID). Apps created before this date continued receiving ASIDs in endpoint responses, unless an app admin used the App Dashboard to opt-in to receive PSIDs. Starting with v7.0+, Pages API endpoints will only accept and return PSIDs for all apps, regardless of their creation date. This change will also be applied to all API versions on May 5th, 2021.
>
> The Page-Scoped ID API allows you to map ASIDs to their PSID equivalents. The API is deprecated in v7.0+, but can be used with older versions until May 5th, 2021.
#### [速率限制](https://developers.facebook.com/docs/graph-api/overview/rate-limiting#pages "Rate Limits")
The Page Rate Limits may use either the Platform or BUC rate limit logic depending on the type of token used. Any Pages API calls that are made using a Page or system user access token use the rate limit calculation below. Any calls made with application or user access tokens are subject to application or User rate limits.
Requests made by your app to the Pages API using a Page access token or system User access token are counted against the app’s call count. An app’s call count is the number of calls it can make during a rolling one hour window and is calculated as follows:
> Calls within one hour = 4800 * Number of Engaged Users
The Number of Engaged Users is the number of Users who engaged with your Page per 24 hours.
Requests made by your app to the Pages API using a User access token or App access token follow the Platform Rate Limit logic.
## Webhooks<sup>([出處](https://developers.facebook.com/docs/graph-api/webhooks/))</sup>
使 app 接收到來自 Facebook Social Graph 特定 objects 即時更新的 **notification**;例如:app 用戶在**粉絲專頁**上的 objects 留言。Facebook Social Graph 有多種 objects,像是 User objects、Page obejcts;故設置 Webhook 時須選擇 object。不同的 objects 有不同的 fields 故必須 subscribe 該 object 的特定 field,則在 subscribed 的該 field 的 value 有改變時能收到 **notification**。**notification** 會透過 HTTP POST request 帶著以 JSON 格式的更新資料給 app;例如:設置`User` Webhook 並 subscribe 其`Photos` field 可讓 app 用戶上傳相片時發出 notification 給 app。
Webhooks 必須建置在有效的 TLS/SSL certificate 環境並僅能支援 HTTPS requests。使用 Webhooks 不用應用程式審查,但各 object 所需的 permission(s)還是會影響。
App 為**調整中**的狀態下**無法**接收即時 **webhooks notifications**,如要測試僅能透過 app dashboard<sub>(應用程式的主控板)</sub>。
### 設定 Webhook<sup>[(出處)](https://developers.facebook.com/docs/graph-api/webhooks/getting-started)</sup>
#### 建立 endpoint
#### 在應用程式主控板裡設置
## 應用程式審查
* [App Review](https://developers.facebook.com/docs/app-review)
* [Sample Submissions](https://developers.facebook.com/docs/app-review/resources/sample-submissions)
* [Facebook 開放平台政策](https://developers.facebook.com/policy)
* [Common Mistakes](https://developers.facebook.com/docs/app-review/submission-guide/common-mistakes)
## 技術文檔
* [Bot 範例](https://developers.facebook.com/docs/messenger-platform/getting-started/sample-apps)
* [演示 Messenger Platform 機器人的最佳示範](https://github.com/fbsamples/messenger-bot-samples)
* [图谱 API:Page](https://developers.facebook.com/docs/graph-api/reference/page/)
* [保護圖形 API 要求](https://developers.facebook.com/docs/graph-api/securing-requests/)
* [管理 Facebook 粉絲專頁](https://developers.facebook.com/docs/pages/managing)
* [X-Hub-Signature SHA1 signature in Java](https://stackoverflow.com/questions/17221253/facebook-real-time-update-validating-x-hub-signature-sha1-signature-in-java)
## 參考對象
* [BotBonnie](https://botbonnie.com/)
## 常數
| 鍵 | 值 | 說明 |
| - | - | - |
| `FACEBOOK_APP_ACCESS_TOKEN` | `756593051752703|OrVhHMdWwb2hlpJUwGpWx3fldxI` | 應用程式存取權杖 |
| `FACEBOOK_APP_ID` | `756593051752703` | 應用程式編號 |
| `FACEBOOK_APP_SECRET` | `b0cfef270a3482947e2f809b5b9b7f1c` | 應用程式密鑰 |
| `FACEBOOK_PAGE_ACCESS_TOKEN` | `EAAKwHgwW2P8BAOnFThY2ftjgPu0FGLvpZCaCUfksRMFX0wH6BBzQ7tMC0LejaeyeLsexsGGuZBVEGTh6hMmn4BZCA3eNZCSjJMNgIS5SZAqPa7LiGYOzCirIp1Pd4hZB7Gt2MhmjwLFleh3TGPDm1KbdNjbIBWALohcEQYQEPpXo66hPuKjNDZC4XzmZCZAX7YI3TZBV1jHEluNAZDZD` | 粉絲專頁權杖 |
| `FACEBOOK_PAGE_ID` | `113977140360460` | 粉絲專頁編號 |
## 網域
* musemodel.app
* jkfan.musemodel.app
* jkfan.musemodel.app/webhook
## 2020/09/25
創作者主頁
打賞、加盟
官方活動
金流
自訂小活動
內容:photos、videos
僅能一或三:一般粉絲、VIP粉絲、至尊粉絲
粉絲名稱由平台定義、金額由主播自訂
單則解鎖:針對某付費限定內容對會員做試用付費
能有免費內容(open to the public)
## 一元打賞
LINE login
LINE@
市價、實價(成本)、定價
### 買籤邏輯
1. 籤數 < 總籤數
## Lovense 智能情趣玩具
```
POST https://api.lovense.com/api/lan/{command} HTTP/1.1
```
|||
|-|-|
|`token`<SUP style="color:#F00">*</SUP>|`GXPc6Ch7JTbThyYWdUCsRaGUGhj3oxRKCg3/mLp3lcNqkpOMlwXRuus3qzXnlmz7`|
|`uid`<SUP style="color:#F00">*</SUP>|我們平台自己維護的用戶識別碼|
|`command`<SUP style="color:#F00">*</SUP>|指令(詳見下表)|
|`v`<SUP style="color:#F00">*</SUP>|值|
|`t`|玩具識別碼(若未指定則所有連至該裝置的玩具都會收到指令)|
|`sec`|秒數|
### 指令
| |  |  |  |  |  |  |  |  | |
| --------------------- | ---------------------------------------- | ----------------------------------------- | ------------------------------------------ | ---------------------------------------- | ---------------------------------------- | ------------------------------------------ | ---------------------------------------- | ------------------------------------------ | --- |
| `Vibrate` | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | |
| `Vibrate1` | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | |
| `Vibrate2` | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | |
| `Rotate` | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ✅ | ❌ | |
| `RotateAntiClockwise` | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ✅ | ❌ | |
| `RotateClockwise` | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ✅ | ❌ | |
| `RotateChange` | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ✅ | ❌ | |
| `AirAuto` | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | |
| `AirIn` | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | |
| `AirOut` | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | |
| `Preset` | ✅ | ❌ | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ | |
| `Battery` | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | |
| `AVibrate` | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | |
| `ARotate` | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ✅ | ❌ | |
| `AAirLevel` | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | |
| `AVibRotate` | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ✅ | ❌ | |
| `AVibAir` | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | |
| `AVibrate1` | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | |
| `AVibrate2` | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | |
| | Edge | Max 2 | Domi 2 | Hush | Ambi | Lush 2 | Nora | Osci 2 | |