Skip to content

Instantly share code, notes, and snippets.

@ma2shita
Last active April 20, 2023 00:37
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 ma2shita/1c3564d4543b8ec2506d271cb291a0d5 to your computer and use it in GitHub Desktop.
Save ma2shita/1c3564d4543b8ec2506d271cb291a0d5 to your computer and use it in GitHub Desktop.
import os
import json
import urllib.request
import urllib.parse
SLACK_CHANNEL_ID = os.environ['SLACK_CHANNEL_ID']
def lambda_handler(event, _):
print(f'{event=}')
btn = event['deviceEvent']['buttonClicked']['clickType']
text = f'ボタンが「{btn}」で押されました。<@UC0RVEWBZ> 仕組み: https://ma2shita.s3.ap-northeast-1.amazonaws.com/awssummittokyo2023a/AWS+Summit+Tokyo+2023.png'
post_to_slack_bot(SLACK_CHANNEL_ID, text)
return {'statusCode': 204}
SLACK_BEARER_TOKEN = os.environ['SLACK_BEARER_TOKEN']
def post_to_slack_bot(channel, text):
print(f'{channel=}', f'{text=}')
payload = { 'channel': channel, 'text': text }
hdrs = {
'Content-Type': 'application/json; charset=utf-8',
'Authorization': f'Bearer {SLACK_BEARER_TOKEN}'
}
req = urllib.request.Request('https://slack.com/api/chat.postMessage',
method='POST', headers=hdrs,
data=json.dumps(payload).encode('utf-8'))
with urllib.request.urlopen(req) as res:
body = res.read().decode("utf-8")
print(f'{body=}')
return
if __name__ == "__main__":
testEvent_iot1click = {
"deviceInfo": {
"deviceId": "7MF6XXXXXXYY9999",
"type": "button",
"remainingLife": 99.933334,
"attributes": {
"projectRegion": "us-west-2",
"projectName": "event_and_context_dump",
"placementName": "placement1",
"deviceTemplateName": "invoke_lambda"
}
},
"deviceEvent": {
"buttonClicked": {
"clickType": "DOUBLE",
"reportedTime": "2022-12-27T07:32:48.709Z"
}
},
"placementInfo": {
"projectName": "event_and_context_dump",
"placementName": "placement1",
"attributes": {
"attr2": "value2",
"attr_add1": "value_add1",
"attr1": "value1"
},
"devices": {
"invoke_lambda": "7MF6XXXXXXYY9999"
}
}
}
lambda_handler(testEvent_iot1click, None)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment