--The handler is specified by file name.function name
--The module called requests that comes out seems to have been absorbed in urllib
--Throw json to slack's WEBHOOK address by POST
--def lambda_handler (event, context):
event
is JSON data passed to the function when Lambda is executed
--Of course, the contents differ depending on the resource of the execution source, but since it is JSON data, it can be handled like a template.
import json
import urllib.request
def post_slack():
send_data = {
"username": "notify_slack",
"icon_emoji": ":vampire:",
"text": "test",
}
send_text = "payload=" + json.dumps(send_data)
method = 'POST'
headers = {'Content-Type': 'application/json'}
WEB_HOOK_URL = "https://hooks.slack.com/services/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"
#Assemble the object
request = urllib.request.Request(
WEB_HOOK_URL,
data=send_text.encode('utf-8'),
method=method
)
#POST is actually executed here
with urllib.request.urlopen(request) as response:
response_body = response.read().decode('utf-8')
#First called by Lambda
def lambda_handler(event, context):
response = post_slack()
return response
--Run test
from lambda
--The JSON string to be tested at this time is appropriate (because it is not used in the code)
Recommended Posts