Skip to content

Instantly share code, notes, and snippets.

@totegamma
Created February 16, 2021 13:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save totegamma/af3e23d17d5482cc28c94c920bb0b0a2 to your computer and use it in GitHub Desktop.
Save totegamma/af3e23d17d5482cc28c94c920bb0b0a2 to your computer and use it in GitHub Desktop.
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