Last active
November 7, 2018 15:11
-
-
Save ma2shita/4c49cf93034ce2b43c46068b3f475a28 to your computer and use it in GitHub Desktop.
See: https://qiita.com/ma2shita/items/8f1b4b12faa99dd17063 | Slack via API Gateway -> this function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| INPUT: API Gateway | |
| required ENVs; | |
| - OUTGOING_TOKEN: Slack's Outgoing Webhook Token | |
| """ | |
| import json | |
| import logging | |
| logger = logging.getLogger() | |
| logger.setLevel(logging.INFO) | |
| import urllib.parse | |
| import os | |
| import boto3 | |
| NAME_IDX = { | |
| 'ma2shita': 'max', | |
| 'moto': 'moto', | |
| 'katayama': 'yaman' | |
| } | |
| def lambda_handler(event, context): | |
| logger.info('Received event: ' + json.dumps(event)) | |
| b = urllib.parse.parse_qs(event['body']) | |
| if b['token'][0] != os.environ['OUTGOING_TOKEN']: | |
| return { | |
| 'statusCode': 403, | |
| 'body': json.dumps('Invalid Token') | |
| } | |
| shadowDoc = {'state':{'reported':{'status':'running'}}} | |
| iot = boto3.client('iot-data') | |
| thingName = NAME_IDX[b['user_name'][0]] | |
| iot.update_thing_shadow(thingName=thingName, payload=json.dumps(shadowDoc)) | |
| f = boto3.client("lambda") | |
| f.invoke( | |
| FunctionName='max1click-demo_JF2018_transiton_to_idle', | |
| InvocationType='Event', | |
| Payload=json.dumps({ | |
| 'thing_name': thingName, | |
| 'transition_wating_sec': 15 | |
| }) | |
| ) | |
| return {"statusCode": 204} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment