Skip to content

Instantly share code, notes, and snippets.

@harrisonlingren
Last active July 23, 2018 03:23
Show Gist options
  • Save harrisonlingren/84fa21f02a09929ec827d3f693325056 to your computer and use it in GitHub Desktop.
Save harrisonlingren/84fa21f02a09929ec827d3f693325056 to your computer and use it in GitHub Desktop.
from gpiozero import Button
from signal import pause
import sys, requests, json
pressed_count = 0
webhook_url = 'YOUR-WEBHOOK'
message = 'Hello, world!'
# action to perform on press
def DoAction():
global message
SendMessage(message)
# send a message to the webhook
def SendMessage(message):
payload = {'text': message}
r = requests.post(webhook_url, data=json.dumps(payload))
print(r.text)
# run the things
def main(argv):
global message
if argv == '--help' or argv == 'help':
print('Type: "> slack_button.py <your_message_here>"')
return
# set message to argument
message = argv
print('Using message: {}'.format(message))
# create button and bind to GPIO#4
button = Button(4)
button.when_pressed = DoAction
print('Waiting for input...')
pause()
if __name__ == '__main__':
if len(sys.argv) > 1:
main(sys.argv[1])
else:
print('No message set, defaulting to "Hello, world!"')
main(message)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment