Last active
November 7, 2018 14:25
See: https://qiita.com/ma2shita/items/8f1b4b12faa99dd17063 | SORACOM LTE-M Button (AWS IoT 1-Click) -> this function)
This file contains 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
""" | |
required ENVs; | |
- INCOMING_WEBHOOK: Slack's Incoming Webhook URL | |
- PARSON_(SINGLE|DOUBLE|LONG): JSONfy String | |
{"name": <Display name and Thing name:String>, | |
"slack_id": <Slack member ID (see profile):String>} | |
e.g.) {"name":"max","slack_id":"UC0RVEWBZ"} | |
""" | |
import json | |
import os | |
import logging | |
logger = logging.getLogger() | |
logger.setLevel(logging.INFO) | |
import urllib.request, urllib.parse | |
import boto3 | |
def lambda_handler(event, context): | |
logger.info('Received event: ' + json.dumps(event)) # Output to Cloudwatch Log | |
parson = json.loads(os.environ['PARSON_{}'.format(event['deviceEvent']['buttonClicked']['clickType'])]) | |
body = {'text': '<@{}> が呼ばれたよ!現場に行けそうなら `ok ~~` と返すとステータスが更新されるよ。気付いてなさそうならフォローを!'.format( | |
parson['slack_id'], parson['name']) | |
} | |
headers = {'Content-Type': 'application/json'} | |
req = urllib.request.Request(os.environ['INCOMING_WEBHOOK'], | |
data=json.dumps(body).encode('utf-8'), | |
method='POST', headers=headers) | |
with urllib.request.urlopen(req) as res: | |
logger.info(res.read().decode("utf-8")) | |
shadowDoc = {'state':{'reported':{'status':'calling'}}} | |
iot = boto3.client('iot-data') | |
iot.update_thing_shadow(thingName=parson['name'], payload=json.dumps(shadowDoc)) | |
f = boto3.client("lambda") | |
f.invoke( | |
FunctionName='max1click-demo_JF2018_transiton_to_idle', | |
InvocationType='Event', | |
Payload=json.dumps({ | |
'thing_name': parson['name'], | |
'transition_wating_sec': 30 | |
}) | |
) | |
return {"statusCode": 204} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment