AWS API GatewayとAWS Lambdaで、Facebook Botを作成する


# 事前準備

AWS API GatewayとAWS Lambdaで、HelloWorldを作成する
http://qiita.com/akira-sasaki/items/b9e8e8a0330818e3b71d

# Facebookアプリの作成

https://developers.facebook.com/quickstarts/?platform=web

![bot001.png](https://qiita-image-store.s3.amazonaws.com/0/63450/1bcfc80b-c0dc-4eb3-00f8-3cc002e6fa13.png)

![bot002.png](https://qiita-image-store.s3.amazonaws.com/0/63450/6ff9c666-5818-bb08-e792-c321aa3ddc78.png)

![bot003.png](https://qiita-image-store.s3.amazonaws.com/0/63450/3ed9a98b-b9ba-3572-0b6b-55048afe55d7.png)

![bot004.png](https://qiita-image-store.s3.amazonaws.com/0/63450/be9c11b5-a584-4b09-ab09-c2545ed09d2d.png)

Reloadを押す。

![bot005.png](https://qiita-image-store.s3.amazonaws.com/0/63450/bbee401e-c110-9ab6-8805-d26e700d4f17.png)

![bot007.png](https://qiita-image-store.s3.amazonaws.com/0/63450/3c6ca9f9-0835-be61-55d9-2b8c50f65c57.png)

# Facebook Pageの作成

https://www.facebook.com/pages/create/

![page001.png](https://qiita-image-store.s3.amazonaws.com/0/63450/df6336a2-93a7-4960-66a6-03cb2b671c75.png)

![page002.png](https://qiita-image-store.s3.amazonaws.com/0/63450/1a3ee4ce-f6c6-2e42-fcfc-c2068ef6db37.png)

![page003.png](https://qiita-image-store.s3.amazonaws.com/0/63450/f1dd5874-7e8f-3ff5-323b-8eeacdd7d942.png)

![page004.png](https://qiita-image-store.s3.amazonaws.com/0/63450/00191c8a-dd54-b0a3-bab7-faf5a6715848.png)

![page005.png](https://qiita-image-store.s3.amazonaws.com/0/63450/b6ce505a-088d-874f-c8ee-3546c455266c.png)

![page006.png](https://qiita-image-store.s3.amazonaws.com/0/63450/99749fda-1c6f-249b-ecfb-48c8d5be0815.png)

# AWS Lamdbaの作成

![lamdba001.png](https://qiita-image-store.s3.amazonaws.com/0/63450/87c33225-9dae-bb7e-8563-88819da461c6.png)

## メッセージ受信用のScript

```js:FacebookBotCallback
exports.handler = function(event, context) {
    console.log('Received event:', JSON.stringify(event, null, 2));
}
```

![lamdba002.png](https://qiita-image-store.s3.amazonaws.com/0/63450/b28a6be2-48fa-0e7d-5056-2ba402bfcb01.png)

![lamdba003.png](https://qiita-image-store.s3.amazonaws.com/0/63450/01aa5f9c-591e-c3f7-976b-3cb82a2bf970.png)

![lamdba004.png](https://qiita-image-store.s3.amazonaws.com/0/63450/b9e1851e-7b39-5851-941f-51268a730613.png)

![lamdba005.png](https://qiita-image-store.s3.amazonaws.com/0/63450/f69c5968-84bf-2754-254b-479bfffef2e0.png)

![lamdba006.png](https://qiita-image-store.s3.amazonaws.com/0/63450/44f41dc8-ab02-d778-4a99-9aa1e71757f0.png)

![lamdba007.png](https://qiita-image-store.s3.amazonaws.com/0/63450/827d2fd3-f4ed-6b36-d64c-683cd4bfd718.png)

![lamdba008.png](https://qiita-image-store.s3.amazonaws.com/0/63450/6da10334-7c5f-732a-e7ec-03433f253574.png)

![lamdba009.png](https://qiita-image-store.s3.amazonaws.com/0/63450/35626eb4-0296-56af-714b-e016a5366327.png)

![lamdba010.png](https://qiita-image-store.s3.amazonaws.com/0/63450/17570d76-cbb4-63c8-dbfa-d147514042d1.png)

![lamdba011.png](https://qiita-image-store.s3.amazonaws.com/0/63450/20ccf98f-2183-6f01-4ca5-2f7995f31f55.png)

![lamdba012.png](https://qiita-image-store.s3.amazonaws.com/0/63450/a4922af3-0750-931a-d68a-65a918e137bb.png)

## Cloud Watchでログをチェック

![lamdba013.png](https://qiita-image-store.s3.amazonaws.com/0/63450/19aee76f-991c-274b-ec31-b5e9ec8fb86c.png)

![watch001.png](https://qiita-image-store.s3.amazonaws.com/0/63450/954f5e6c-8824-795c-f3df-f182f10a5153.png)

![watch002.png](https://qiita-image-store.s3.amazonaws.com/0/63450/ae28bc74-064a-37ba-9baf-e3fd820e39e7.png)

![watch003.png](https://qiita-image-store.s3.amazonaws.com/0/63450/4caab358-54e7-4c3e-ad50-664bd27077ef.png)

![watch004.png](https://qiita-image-store.s3.amazonaws.com/0/63450/9285822e-c727-a7d8-fb2b-6cf758191aec.png)

![watch005.png](https://qiita-image-store.s3.amazonaws.com/0/63450/3c6e85a2-6fb6-7753-66e4-6e985bfa8ce9.png)

## Validation用のScript

```js:FacebookBotValidation
var validationToken = "ValidationToken";

exports.handler = function(event, context) {
    console.log('Received event:', JSON.stringify(event, null, 2));
    
    if (!event["hub.verify_token"] || !event["hub.challenge"]) {
        context.fail("invalid parameter");
        return;
    }

    if (event["hub.verify_token"] === validationToken) {
        context.done(null, Number(event["hub.challenge"]));
    } else {
        context.fail("validation failed");
    }
};
```

![val001.png](https://qiita-image-store.s3.amazonaws.com/0/63450/484605f3-4933-fdfe-230f-2bacf5935661.png)

![lamdba003.png](https://qiita-image-store.s3.amazonaws.com/0/63450/01aa5f9c-591e-c3f7-976b-3cb82a2bf970.png)

![val002.png](https://qiita-image-store.s3.amazonaws.com/0/63450/db254a92-8a6a-4ad6-2e3f-cf038b1a4549.png)

![val003.png](https://qiita-image-store.s3.amazonaws.com/0/63450/30dc074f-204d-2d20-b007-e4285d3be59d.png)

![val004.png](https://qiita-image-store.s3.amazonaws.com/0/63450/5daa3632-1a41-14f9-6ce8-f25ef5f6c3f6.png)

![val006.png](https://qiita-image-store.s3.amazonaws.com/0/63450/9a312205-32c6-9033-1deb-63b20e00c54d.png)


# AWS API Gateway

![api001.png](https://qiita-image-store.s3.amazonaws.com/0/63450/40c00c87-301f-5da0-bffd-bcae17604054.png)

![api002.png](https://qiita-image-store.s3.amazonaws.com/0/63450/d69c80b4-dc54-22fb-ed7a-0c80fa5e1c02.png)

![api101_1.png](https://qiita-image-store.s3.amazonaws.com/0/63450/665ca279-ae76-7923-81e0-0f2bea7e2e9a.png)

## 受信用のPOSTメソッドを作成

![api102_1.png](https://qiita-image-store.s3.amazonaws.com/0/63450/45be018e-3d08-37cd-5f0b-16bd68b3357f.png)

![api103_1.png](https://qiita-image-store.s3.amazonaws.com/0/63450/75dfd53a-b246-021b-1b6a-d74266818f87.png)

![api104_1.png](https://qiita-image-store.s3.amazonaws.com/0/63450/d9b2625d-35cf-4b46-f212-ae625f158a3f.png)

![api105_1.png](https://qiita-image-store.s3.amazonaws.com/0/63450/49ddf205-1757-18bd-2811-84714ec84be1.png)


Curlで呼び出してみる。

```shell-session
curl -v -H "Accept: application/json" -H "Content-type: application/json" -X POd '{"key1":"test1","key2":"test2","key3":"test3"}' https://#####.execute-api.ap-northeast-1.amazonaws.com/botcallback/botcallback
```

Cloud Watchでログ確認

![watch001.png](https://qiita-image-store.s3.amazonaws.com/0/63450/954f5e6c-8824-795c-f3df-f182f10a5153.png)

![watch002.png](https://qiita-image-store.s3.amazonaws.com/0/63450/ae28bc74-064a-37ba-9baf-e3fd820e39e7.png)

![watch003.png](https://qiita-image-store.s3.amazonaws.com/0/63450/4caab358-54e7-4c3e-ad50-664bd27077ef.png)

![watch004.png](https://qiita-image-store.s3.amazonaws.com/0/63450/9285822e-c727-a7d8-fb2b-6cf758191aec.png)

![api017.png](https://qiita-image-store.s3.amazonaws.com/0/63450/4545126f-b45e-7daa-85ca-dd4c7c33886f.png)

## Validation用のGETメソッドを作成

![api201_1.png](https://qiita-image-store.s3.amazonaws.com/0/63450/c322a021-c642-9778-0dad-6cfd7d55a8df.png)

![api202.png](https://qiita-image-store.s3.amazonaws.com/0/63450/77a93cd2-a522-a53c-2360-68adb692f48a.png)

![api204.png](https://qiita-image-store.s3.amazonaws.com/0/63450/5ba9352d-679c-6923-a929-054abd604d68.png)

![api205.png](https://qiita-image-store.s3.amazonaws.com/0/63450/779117cb-d5fa-1d94-9cd7-9f14aaf6b735.png)

![api206.png](https://qiita-image-store.s3.amazonaws.com/0/63450/0a1713a3-9156-6f2b-1ac1-338012542427.png)

![api207.png](https://qiita-image-store.s3.amazonaws.com/0/63450/adc81b22-b2f2-9d5e-5df9-bead7f19ca55.png)

![api208.png](https://qiita-image-store.s3.amazonaws.com/0/63450/6028a03f-9932-682b-269a-8fc38f14db0e.png)

![api209.png](https://qiita-image-store.s3.amazonaws.com/0/63450/92d5cfb3-58e2-96f8-731f-5a6ee8f8fcfe.png)

* hub.challenge		
* hub.verify_token

を追加。

![api210.png](https://qiita-image-store.s3.amazonaws.com/0/63450/79f8559f-e6f3-5b80-13f0-cf0172e6a75f.png)

![api211.png](https://qiita-image-store.s3.amazonaws.com/0/63450/ec2f4b9e-948b-6279-411a-30fb93e707fb.png)

![api212.png](https://qiita-image-store.s3.amazonaws.com/0/63450/5b0ea94b-688c-af46-6d94-2f093dc7ea6f.png)

![api213.png](https://qiita-image-store.s3.amazonaws.com/0/63450/90be1b82-13a0-b60a-1c61-ab322ec5ab14.png)



```json:application/json
{
  "hub.verify_token": "$input.params('hub.verify_token')",
  "hub.challenge": "$input.params('hub.challenge')"
}
```

# FacebookとAWS API Gatewayを連携

![fb001.png](https://qiita-image-store.s3.amazonaws.com/0/63450/4e67dfe0-c3fb-2b91-ea26-071fdbc7dc28.png)

![fb002.png](https://qiita-image-store.s3.amazonaws.com/0/63450/467cc8d1-c6f3-b405-9d69-0a3ec94247e6.png)

![fb003.png](https://qiita-image-store.s3.amazonaws.com/0/63450/7ddd61cf-6c92-7ce6-1c59-95f361379c16.png)

> curl -ik -X POST "https://graph.facebook.com/v2.6/me/subscribed_apps?access_token=トークン"

をコマンドラインで実行。

![fb004.png](https://qiita-image-store.s3.amazonaws.com/0/63450/cbbe1642-4774-1dcb-c165-13fe9ba28341.png)

![fb005.png](https://qiita-image-store.s3.amazonaws.com/0/63450/67a959f9-da2b-98aa-1d9f-68e11debdfc0.png)

![fb006.png](https://qiita-image-store.s3.amazonaws.com/0/63450/a425fd7b-e373-f296-e7c1-412d3758d7fe.png)

# 作成したページでメッセージ書き込み

作成したページのメッセージを書き込む

## Cloud Watchでログ確認

![watch001.png](https://qiita-image-store.s3.amazonaws.com/0/63450/954f5e6c-8824-795c-f3df-f182f10a5153.png)

![watch002.png](https://qiita-image-store.s3.amazonaws.com/0/63450/ae28bc74-064a-37ba-9baf-e3fd820e39e7.png)

![watch003.png](https://qiita-image-store.s3.amazonaws.com/0/63450/4caab358-54e7-4c3e-ad50-664bd27077ef.png)

![watch004.png](https://qiita-image-store.s3.amazonaws.com/0/63450/9285822e-c727-a7d8-fb2b-6cf758191aec.png)

メッセージがログにでたら成功!

# おうむ返しBotのプログラム作成　

http://qiita.com/akira-sasaki/items/cbb667653579db9743ec