{"id":627,"date":"2020-04-12T21:41:47","date_gmt":"2020-04-12T12:41:47","guid":{"rendered":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2020\/04\/12\/positive-line-bot"},"modified":"2020-04-12T21:41:47","modified_gmt":"2020-04-12T12:41:47","slug":"positive-line-bot","status":"publish","type":"post","link":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2020\/04\/12\/positive-line-bot\/","title":{"rendered":"\u81ea\u52d5\u8fd4\u4fe1LINE Bot\u3092AWS SAM+DynamoDB\u3067\u4f5c\u3063\u3066\u307f\u305f"},"content":{"rendered":"

\u3053\u3093\u306b\u3061\u306f\u3001\u95a2\u53e3\u3067\u3059\u3002
\n\u6700\u8fd1\u306fUber Eats\u3068Amazon\u30d5\u30ec\u30c3\u30b7\u30e5\u3092\u591a\u7528\u3057\u3066\u4ee5\u524d\u306b\u3082\u307e\u3057\u3066\u5f15\u304d\u3053\u3082\u308a\u6c17\u5473\u3067\u3059\u3002<\/p>\n

\u4eca\u56de\u306f\u73fe\u72b6\u6240\u5c5e\u3057\u3066\u3044\u308b\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u306e\u30ad\u30e3\u30c3\u30c1\u30a2\u30c3\u30d7\u7684\u610f\u5473\u5408\u3044\u3092\u8fbc\u3081\u3066\u3001
\n\u30dd\u30b8\u30c6\u30a3\u30d6\u306a\u30ef\u30fc\u30c9\u3092\u8fd4\u3057\u3066\u304f\u308c\u308b\u81ea\u52d5LineBot\u3092
\nAWS SAM\u3068DynamoDB\u3067\u8a66\u3057\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n

\u96db\u5f62\u4f5c\u6210<\/h2>\n

\u307e\u305a\u306f\u96db\u5f62\u306e\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002<\/p>\n

$ sam init --runtime go1.x --name positive-line-bot\n\n$ cd positive-line-bot\n\n\/\/\u4f9d\u5b58\u30e2\u30b8\u30e5\u30fc\u30eb\u7ba1\u7406\n$ go mod init line-positive-bot<\/code><\/pre>\n

DynamoDB<\/h2>\n

Line\u3068\u9023\u643a\u3059\u308b\u524d\u306b\u3001
\nDynamoDB\u304b\u3089\u81ea\u52d5\u8fd4\u4fe1\u3055\u308c\u308b\u30ef\u30fc\u30c9\u3092\u53d6\u5f97\u3059\u308b\u90e8\u5206\u3092\u5b9f\u88c5\u3057\u307e\u3059\u3002<\/p>\n

\u4e00\u89a7\u3092DynamoDB\u306e\u30c6\u30fc\u30d6\u30eb\u304b\u3089\u53d6\u5f97\u3057\u3066\u304d\u3066\u3001
\n\u305d\u306e\u3046\u30611\u3064\u3092\u30e9\u30f3\u30c0\u30e0\u306b\u62bd\u51fa\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n

type Positive struct {\n    ID   int    `json:"ID"`\n    Name string `json:"Name"`\n}\n\nfunc handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {\n\n    endpoint := os.Getenv("DYNAMODB_ENDPOINT")\n    tableName := os.Getenv("DYNAMODB_TABLE_NAME")\n\n    sess := session.Must(session.NewSession())\n    config := aws.NewConfig().WithRegion("ap-northeast-1")\n    if len(endpoint) > 0 {\n        config = config.WithEndpoint(endpoint)\n    }\n\n    db := dynamodb.New(sess, config)\n\n    result, err := db.Scan(&dynamodb.ScanInput{\n        TableName:              aws.String(tableName),\n        ConsistentRead:         aws.Bool(true),\n        ReturnConsumedCapacity: aws.String("NONE"),\n    })\n    if err != nil {\n        return events.APIGatewayProxyResponse{}, err\n    }\n\n    var positives []Positive\n\n    err = dynamodbattribute.UnmarshalListOfMaps(result.Items, &positives)\n    if err != nil {\n        fmt.Println(err)\n        return events.APIGatewayProxyResponse{}, err\n    }\n\n    var words []string\n    for _, positive := range positives {\n        words = append(words, positive.Name)\n    }\n\n    rand.Seed(time.Now().UnixNano())\n    i := rand.Intn(len(words))\n    word := words[i]\n\n    return events.APIGatewayProxyResponse{\n        Body:       word,\n        StatusCode: 200,\n    }, nil\n}\n\nfunc main() {\n    lambda.Start(handler)\n}<\/code><\/pre>\n

template.yml<\/code><\/p>\n

Resources:\n  PositiveLineBotFunction:\n    Type: AWS::Serverless::Function\n    Properties:\n      CodeUri: positive-line-bot\/\n      Handler: positive-line-bot\n      Runtime: go1.x\n      Tracing: Active\n      Policies:\n        - arn:aws:iam::aws:policy\/AmazonDynamoDBFullAccess\n      Events:\n        CatchAll:\n          Type: Api\n          Properties:\n            Path: \/positive\n            Method: GET\n      Environment:\n        Variables:\n          DYNAMODB_ENDPOINT: ''\n          DYNAMODB_TABLE_NAME: 'PositiveLineBotTable'\n\n  PositiveLineBotTable:\n    Type: AWS::DynamoDB::Table\n    Properties:\n      TableName: 'PositiveLineBotTable'\n      AttributeDefinitions:\n        - AttributeName: 'ID'\n          AttributeType: 'N'\n      KeySchema:\n        - AttributeName: 'ID'\n          KeyType: 'HASH'\n      ProvisionedThroughput:\n        ReadCapacityUnits: '2'\n        WriteCapacityUnits: '2'<\/code><\/pre>\n

DynamoDB Local<\/h3>\n

\u3046\u307e\u304f\u3067\u304d\u3066\u3044\u308b\u304b\u3069\u3046\u304b\u30ed\u30fc\u30ab\u30eb\u3067\u78ba\u8a8d\u3057\u307e\u3059\u3002<\/p>\n

docker-compose.yml<\/code><\/p>\n

version: '3'\n\nservices:\n  dynamodb:\n    image: amazon\/dynamodb-local\n    container_name: dynamodb\n    ports:\n      - 8000:8000<\/code><\/pre>\n

\u30ed\u30fc\u30ab\u30eb\u30c6\u30b9\u30c8\u3067\u7528\u3044\u308b\u30c6\u30fc\u30d6\u30eb\u5b9a\u7fa9\u304a\u3088\u3073\u3001\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002
\ntest\/positive-line-bot_table.json<\/code><\/p>\n

{\n  "AttributeDefinitions": [\n    {\n      "AttributeName": "Id",\n      "AttributeType": "N"\n    }\n  ],\n  "TableName": "PositiveLineBotTable",\n  "KeySchema": [\n    {\n      "AttributeName": "Id",\n      "KeyType": "HASH"\n    }\n  ],\n  "ProvisionedThroughput": {\n    "ReadCapacityUnits": 2,\n    "WriteCapacityUnits": 2\n  }\n}<\/code><\/pre>\n

test\/positive-line-bot_table_data.json<\/code><\/p>\n

{\n  "PositiveLineBotTable": [\n    {\n      "PutRequest": {\n        "Item": {\n          "ID": {"N": "1"},\n          "Name": {"S": "test"}\n        }\n      }\n    },\n    {\n      "PutRequest": {\n        "Item": {\n          "ID": {"N": "2"},\n          "Name": {"S": "testtest"}\n        }\n      }\n    }\n  ]\n}<\/code><\/pre>\n

\u30ed\u30fc\u30ab\u30eb\u30c6\u30b9\u30c8\u7528\u306e\u74b0\u5883\u5909\u6570\u3092\u8a2d\u5b9a\u3057\u307e\u3059<\/p>\n

{\n  "PositiveLineBotFunction": {\n    "DYNAMODB_ENDPOINT": "http:\/\/{\u30dd\u30fc\u30c8\u756a\u53f7}:8000",\n    "DYNAMODB_TABLE_NAME": "PositiveLineBotTable"\n  }\n}<\/code><\/pre>\n

AWS CLI \u306e\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3057\u3066\u30c7\u30fc\u30bf\u3092\u53cd\u6620\u3057\u305f\u4e0a\u3067
\n\u5b9f\u884c\u3059\u308b\u3068\u3001\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u306e\u5024\u304c1\u3064\u30e9\u30f3\u30c0\u30e0\u3067\u8fd4\u3055\u308c\u307e\u3059<\/p>\n

$ docker-compose up -d\n\n$ aws dynamodb create-table --cli-input-json file:\/\/test\/positive-line-bot_table.json --endpoint-url http:\/\/127.0.0.1:8000\n\n$ aws dynamodb batch-write-item --request-items file:\/\/test\/positive-line-bot_table_data.json --endpoint-url http:\/\/127.0.0.1:8000\n\n$ aws dynamodb scan --table-name PositiveLineBotTable --endpoint-url http:\/\/127.0.0.1:8000\n\n$ sam local start-api --env-vars test\/env.json\u3000--profile dummy\n\n$ curl http:\/\/localhost:3000\/positive\n{"id":1,"name":"testtest"}<\/code><\/pre>\n

Line\u3068\u9023\u643a\u3059\u308b<\/h2>\n

Messaging API\u3092\u5229\u7528\u3059\u308b\u306b\u306f<\/a>\u306a\u3069\u3092\u53c2\u8003\u306bLine\u5074\u306e\u8a2d\u5b9a\u884c\u3044\u307e\u3059\u3002<\/p>\n

\u8a2d\u5b9a\u6642\u3001ChannelSecret\u3068\u30a2\u30af\u30bb\u30b9\u30c8\u30fc\u30af\u30f3\uff08\u30ed\u30f3\u30b0\u30bf\u30fc\u30e0\uff09\u3092\u30e1\u30e2\u3057\u307e\u3059\u3002<\/p>\n

Line\u306b\u5165\u529b\u3055\u308c\u305f\u5024\u304c\u6e21\u3063\u3066\u304f\u308b\u3088\u3046\u306b\u3001\u30b3\u30fc\u30c9\u3068template.yml\u3092\u4fee\u6b63\u3057\u307e\u3059\u3002<\/p>\n

func UnmarshalLineRequest(data []byte) (LineRequest, error) {\n    var r LineRequest\n    err := json.Unmarshal(data, &r)\n    return r, err\n}\n\ntype LineRequest struct {\n    Events      []Event `json:"events"`\n    Destination string  `json:"destination"`\n}\n\ntype Event struct {\n    Type       string  `json:"type"`\n    ReplyToken string  `json:"replyToken"`\n    Source     Source  `json:"source"`\n    Timestamp  int64   `json:"timestamp"`\n    Message    Message `json:"message"`\n}\n\ntype Message struct {\n    Type string `json:"type"`\n    ID   string `json:"id"`\n    Text string `json:"text"`\n}\n\ntype Source struct {\n    UserID string `json:"userId"`\n    Type   string `json:"type"`\n}\n\nfunc handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {\n\n    myLineRequest, err := UnmarshalLineRequest([]byte(request.Body))\n    if err != nil {\n        log.Fatal(err)\n    }\n\n    bot, err := linebot.New(\n        "ChannelSecret",\n        "\u30a2\u30af\u30bb\u30b9\u30c8\u30fc\u30af\u30f3\uff08\u30ed\u30f3\u30b0\u30bf\u30fc\u30e0\uff09")\n    if err != nil {\n        log.Fatal(err)\n    }\n\n    \/\/ \u4e2d\u7565\n\n    var tmpReplyMessage string\n    tmpReplyMessage = word\n    if _, err = bot.ReplyMessage(myLineRequest.Events[0].ReplyToken, linebot.NewTextMessage(tmpReplyMessage)).Do(); err != nil {\n        log.Fatal(err)\n    }\n\n    return events.APIGatewayProxyResponse{\n        Body:       word,\n        StatusCode: 200,\n    }, nil\n\n}<\/code><\/pre>\n

template.yml<\/code><\/p>\n

Method: POST<\/code><\/pre>\n

\u30c7\u30d7\u30ed\u30a4<\/h2>\n

\u6700\u5f8c\u306b\u30c7\u30d7\u30ed\u30a4\u3092\u3057\u3066\u5b9f\u969b\u306b\u78ba\u8a8d\u3057\u3066\u307f\u307e\u3059\u3002<\/p>\n

$ sam validate --profile my_profile --debug\n\n$ sam build --template template.yaml --profile my_profile\n\n$ sam deploy --guided --profile my_profile<\/code><\/pre>\n

\u4f5c\u6210\u3055\u308c\u305fdynamoDB\u30c6\u30fc\u30d6\u30eb\u306b\u30c7\u30fc\u30bf\u3092\u5165\u308c\u305f\u72b6\u614b\u3067
\n\u8a66\u3057\u306b\u81ea\u8eab\u306e\u643a\u5e2f\u304b\u3089\u78ba\u8a8d\u3057\u305f\u3068\u3053\u308d\u7121\u4e8b\u81ea\u52d5\u8fd4\u7b54\u304c\u3055\u308c\u307e\u3057\u305f\u3002<\/p>\n

\"\"<\/p>\n

\u53c2\u8003\u30b5\u30a4\u30c8<\/h2>\n