-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlambda_function.py
36 lines (33 loc) · 1.15 KB
/
lambda_function.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import json
import urllib
def lambda_handler(event, context):
# TODO implement
body = str(get_line_message(event))
post_slack(body)
return {
'statusCode': 200,
# テストで日本語も使いたいためensure_asciiをFalseにして \uXXXXを回避
'body': json.dumps(body, ensure_ascii=False)
}
# Lineから送られてきたメッセージを抽出するメソッド
def get_line_message(event):
b = event["body"]
try:
jos = json.loads(b)
return jos["events"][0]["message"]["text"]
except:
return b
def post_slack(text):
# ここにslack botの incoming webhook URLを入れる
url = "https://hooks.slack.com. your slack webhook URL"
method = "POST"
# slackにはlineのテキストを転送するだけ
item = {
"text":str(text)
}
# SlackにPOSTする
header = {"Content-Type" : "application/json"}
json_data = json.dumps(item).encode("utf-8")
request = urllib.request.Request(url, data= json_data, method=method, headers=header)
with urllib.request.urlopen(request) as response:
response_body = response.read().decode("utf-8")