前提条件

SlackのWebhook設定確認

  1. https://[slack名].slack.com/services/new にアクセス
  2. 検索窓より[Slash Commands]を検索
  3. "Slash Commands"をインストール
  4. "Choose a Command"にコマンド名を入力(今回は[/sayhello]と入力)
  5. 設定画面遷移後に"Outgoing Data"の"token=<token name>"のToken名をコピー

Tokenの暗号化

  1. IAMユーザー名:lambdaSlackTest
  2. IAMポリシー: ※今回はKMSによる暗号化のみ許可
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "kms:Encrypt"
            ],
            "Resource": "*"
        }
    ]
}
  1. AWSマネジメントコンソール >> IAM >> 暗号化キー よりキーの作成

    リージョン: アジアパシフィック(東京)
    エイリアス:lambdaSlackTest

  2. "キー管理者"に作成したIAMユーザー"lambdaSlackTest"を追加
$ aws configure
AWS Access Key ID [None]: xxxxxxxxxx
AWS Secret Access Key [None]: xxxxxxxxxx
Default region name [None]: ap-northeast-1
Default output format [None]: json
$ aws kms encrypt --key-id alias/lambdaSlackTest --plaintext "<token name>"
{
    "KeyId": "arn:aws:kms:ap-northeast-1:xxxxxxxxxxxx:key/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
    "CiphertextBlob": "<encrypted token>"
}

Lambda Function作成

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "kms:Decrypt"
            ],
            "Resource": [
                "arn:aws:kms:ap-northeast-1:xxxxxxxxxxxx:key/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
            ]
        }
    ]
}

IAM Roleの追加

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "logs:CreateLogGroup",
                "logs:CreateLogStream",
                "logs:PutLogEvents"
            ],
            "Resource": "arn:aws:logs:*:*:*"
        }
    ]
}
{
"body":
"token=<token name>&team_id=T0001&team_domain=example&channel_id=C2147483705&channel_name=test&user_id=U2147483697&user_name=Steve&command=/weather&text=94070&response_url=https://hooks.slack.com/commands/1234/5678"
}
"Steve invoked /weather in test with the following text: 94070"

API Endpoint追加

  1. Lambda Functions >> "slack-echo-command-python" >> "API Endpoints"より"Add API endpoint"をクリック

    API endpoint type: API Gateway
    API name: LambdaPythonSlackintegrationTest
    Resource name: /Slack-echo-command-python
    Method: POST
    Deployment stage: prod
    Security: Open

  2. "Submit"をクリック

API Gateway設定

  1. /Slack-echo-command-python >> "統合リクエスト"をクリック
  2. "マッピングテンプレートの追加"をクリック

    Content-Type: application/x-www-form-urlencoded
    Template: { "body": $input.json("$") }

  3. "保存"をクリック
  4. "アクション" >> "APIのデプロイ" >> デプロイされるステージ:prod でデプロイ

SlackのWebhook設定

https://xxxxxxxxxx.execute-api.ap-northeast-1.amazonaws.com/prod/Slack-echo-command-python
/sayhello test
"<user> invoked /sayhello in directmessage with the following text: test"

トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS