Sample to send slack notification with python lambda

Configuration

--The handler is specified by file name.function name image.png

Add Imcoming WebHook in Slack

image.png

code

--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

Operation check

--Run test from lambda --The JSON string to be tested at this time is appropriate (because it is not used in the code) image.png

Recommended Posts

Sample to send slack notification with python lambda
Post to slack with Python 3
Send experiment results (text and images) to slack with Python
Site monitoring and alert notification with AWS Lambda + Python + Slack
Send push notifications to iOS apps with Python2 (with sample code)
HTML email with image to send with python
Send a message from Python to Slack
Connect to s3 with AWS Lambda Python
Sample to convert image to Wavelet with Python
Send a message from Slack to a Python server
Send a message to LINE with Python (LINE Notify)
Send an email to Spushi's address with python
Easy server monitoring with AWS Lambda (Python) and result notification in Slack
Export RDS snapshot to S3 with Lambda (Python)
Upload files to Google Drive with Lambda (Python)
Send email with Python
[Python] Mention to multiple people with Slack API
Flow to complete Slack authentication with Flask (Python)
Create a setting in terraform to send a message from AWS Lambda Python3.8 to Slack
Write multiple records to DynamoDB with Lambda (Python, JavaScript)
I want to AWS Lambda with Python on Mac!
Sample data created with python
Connect to BigQuery with Python
Send Japanese email with Python3
Post from Python to Slack
Post to vim → Python → Slack
Operate TwitterBot with Lambda, Python
Connect to Wikipedia with Python
Switch python to 2.7 with alternatives
Write to csv with Python
How to use Python lambda
Post to Slack in Python
Send using Python with Gmail
Posted as an attachment to Slack on AWS Lambda (Python)
How to install NPI + send a message to line with python
Python sample to learn XOR with genetic algorithm with neural network
I tried various methods to send Japanese mail with Python
[AWS] Try adding Python library to Layer with SAM + Lambda (Python)
[Python] Regularly export from CloudWatch Logs to S3 with Lambda
[Introduction to Udemy Python 3 + Application] 58. Lambda
Python: How to use async with
Link to get started with python
[Python] Write to csv file with Python
Create folders from '01' to '12' with python
Nice to meet you with python
Try to operate Facebook with Python
Face detection with Lambda (Python) + Rekognition
Output to csv file with Python
[Automation] Send Outlook email with Python
[Lambda] [Python] Post to Twitter from Lambda!
Convert list to DataFrame with python
MP3 to WAV conversion with Python
To do tail recursion with Python2
What to do with PYTHON release?
Unable to install Python with pyenv
How to use FTP with Python
How to calculate date with python
Notify HipChat with AWS Lambda (Python)
Easily post to twitter with Python 3
I want to debug with Python
Use PostgreSQL with Lambda (Python + psycopg2)