Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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