Created
July 17, 2016 15:07
-
-
Save jobbin/53be0df171638dfa6401c5dc63a327c4 to your computer and use it in GitHub Desktop.
【Slack de AWS CLI】SlackのChannelからAWS CLIを実行できるようにしてみました! ref: http://qiita.com/jobbin/items/d8b32be94593a202c23b
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Slash CommandでSlackのChannelにAWS CLIのコマンド(message)を送ったら、 | |
そのAWS CLIのコマンドがAPI Gateway経由でLambdaに送られ、 | |
Lambdaが送信されたAWS CLIのコマンドを実行して、実行結果をSlackに返す |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
token=3mVXdAC5EFNbsXYGNk0sah3M | |
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## &区切りでPOSTされてきたデータを分割する | |
#set($httpPost = $input.path('$').split("&")) | |
## 出力されるJSONデータの生成 | |
{ | |
#foreach( $keyValue in $httpPost ) | |
## "key=value"というデータをkeyとvalueに分ける | |
#set($data = $keyValue.split("=")) | |
## JSONのフォーマットに整形する(まだ走査するデータがあれば","を挿入する) | |
"$data[0]" : "$data[1]"#if( $foreach.hasNext ),#end | |
#end | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import commands | |
import json | |
import os | |
from cStringIO import StringIO | |
import re | |
import urllib | |
from urllib2 import Request, urlopen, URLError, HTTPError | |
print('Loading function') | |
def _(cmd): | |
return commands.getoutput(cmd) | |
def lambda_handler(event, context): | |
print("Received event: " + json.dumps(event, indent=2)) | |
#response_urlのデコード | |
response_url = urllib.unquote(event['response_url']).encode('raw_unicode_escape').decode('utf-8') | |
print(response_url) | |
slack_message = { | |
'channel': event['channel_name'], | |
'response_type': 'ephemeral', | |
'isDelayedResponse': 'true', | |
'text': "response for: `aws " + event['text'] + "`\n" + _("./aws " + event['text']) | |
} | |
req = Request(response_url) | |
req.add_header('Content-Type', 'application/json') | |
try: | |
response = urlopen(req, json.dumps(slack_message)) | |
response.read() | |
print("Message posted to %s", slack_message['channel']) | |
except HTTPError as e: | |
print("Request failed: %d %s", e.code, e.reason) | |
except URLError as e: | |
print("Server connection failed: %s", e.reason) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment