diff --git a/.travis.yml b/.travis.yml index 077923e..15f1aeb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,8 +3,8 @@ python: - "2.7" - "3.6" install: - - "pip install flake8" + - pip install flake8 script: - - flake8 . --ignore E501,F841,E302 + - flake8 . git: submodules: false diff --git a/ibm_cloud_functions/iot-pub.py b/ibm_cloud_functions/iot-pub.py index 8831552..ced2a27 100644 --- a/ibm_cloud_functions/iot-pub.py +++ b/ibm_cloud_functions/iot-pub.py @@ -1,13 +1,17 @@ -# This function publishes the results from the Watson Assistant service to the Watson IoT platform import requests + + def main(iot_obj): + """This function publishes the results from the Watson Assistant service + to the Watson IoT platform and subsequently all subscribed devices""" # import IOT platform credentials - iot_org_id = iot_obj['iot_org_id'] - device_id = iot_obj['device_id'] - device_type = iot_obj['device_type'] - api_token = iot_obj['api_token'] + auth = ('use-token-auth', iot_obj['api_token']) + headers = {'Content-Type': 'application/json'} # extract result from Watson Assistant - payload = {"d": iot_obj['msg']} - # publish Watson Assistant intent/entity pair to Watson IOT platform and subsequently all subscribed devices - requests.post('https://' + iot_org_id + '.messaging.internetofthings.ibmcloud.com:8883/api/v0002/device/types/' + device_type + '/devices/' + device_id + '/events/query', headers={'Content-Type': 'application/json'}, json=payload, auth=('use-token-auth', api_token)) - return {"msg": payload} + payload = {'d': iot_obj['msg']} + # publish Watson Assistant intent/entity pair to Watson IOT platform + url = ('https{iot_org_id}.messaging.internetofthings.ibmcloud.com:8883/' + 'api/v0002/device/types/{device_type}/devices/{device_id}/events/' + 'query'.format(**iot_obj)) + requests.post(url, headers=headers, json=payload, auth=auth) + return {'msg': payload}