Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Amplifyのビルド通知をDiscordに送るLambda(Python)
from __future__ import print_function
import urllib.request
import json
print('Loading function')
# == :: 設定項目 :: ===========================================
endpoint = "https://discord.com/api/webhooks/YOURENDPOINT-URL"
accountName = 'YOUR ACCOUNT NAME'
accountImage = 'YOUR ACCOUNT IMAGE URL'
apps = {
'YOUR AMPLIFY APP ID': 'YOUR AMPLIFY APP NAME'
}
# =============================================================
def post(url, data, headers ={}):
req = urllib.request.Request(url,
data=json.dumps(data).encode('utf-8'),
headers=headers,
method='POST')
return urllib.request.urlopen(req)
def lambda_handler(event, context):
message = event['Records'][0]['Sns']['Message']
status = 'UNKNOWN'
color = 0xFF00FF
if 'STARTED' in message:
status = 'STARTED'
color = 0xFFFF00
if 'SUCCEED' in message:
status = 'SUCCEED'
color = 0x00FF00
if 'FAILED' in message:
status = 'FAILED'
color = 0xFF0000
appname = 'unknown app'
for key in apps:
if key in message:
appname = apps[key]
fields = [
{'name': 'AppName', 'value': appname, 'inline': True},
{'name': 'Status', 'value': status, 'inline': True}
]
data = {
'embeds': [
{
'title': 'AWS Amplify Build Event',
'color': color,
'fields': fields,
'author': {
'name': accountName,
'icon_url': accountImage
}
}
]
}
post(endpoint, data=data, headers={'Content-Type': 'application/json', "User-Agent": "curl/7.64.1"})
return {
'statusCode': 200
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment