/ NY_Tower_Light / lambda / code.py
code.py
 1  # SPDX-FileCopyrightText: 2019 Anne Barela for Adafruit Industries
 2  #
 3  # SPDX-License-Identifier: MIT
 4  
 5  import paho.mqtt.publish as publish
 6  import time
 7  
 8  AIO_USERNAME = 'YOURUSERNAMEHERE'
 9  AIO_KEY = 'YOURKEYHERE'
10  
11  AIO_TOPIC =       AIO_USERNAME + '/feeds/redlight'
12  AIO_YELLOWTOPIC = AIO_USERNAME + '/feeds/yellowlight'
13  AIO_GREENTOPIC =  AIO_USERNAME + '/feeds/greenlight'
14  
15  def webhook_handler(event, context):
16  	print('Starting webhook handler!')
17  	action = event.get('action')
18  	print('Issue action: {0}'.format(action))
19  	auth = {'username': AIO_USERNAME, 'password': AIO_KEY}
20  
21  	# for issues opened & closed
22  	if action == 'closed':
23  		publish.single(AIO_TOPIC, payload='OFF', hostname='io.adafruit.com', auth=auth)
24  	elif action in ('opened', 'reopened'):
25  		publish.single(AIO_TOPIC, payload='ON', hostname='io.adafruit.com', auth=auth)
26  	# starring & watching
27  	elif action == 'started':
28  		publish.single(AIO_YELLOWTOPIC, payload='ON', hostname='io.adafruit.com', auth=auth)
29  		time.sleep(1)
30  		publish.single(AIO_YELLOWTOPIC, payload='OFF', hostname='io.adafruit.com', auth=auth)
31  	# look for pushes
32  	elif "commits" in event:
33  		publish.single(AIO_GREENTOPIC, payload='ON', hostname='io.adafruit.com', auth=auth)
34  		time.sleep(1)
35  		publish.single(AIO_GREENTOPIC, payload='OFF', hostname='io.adafruit.com', auth=auth)
36  
37  	return 'OK'
38  
39  
40  if __name__ == '__main__':
41  	webhook_handler({'action': 'started'}, {})