Universal Robots Forum

Help - Python Program to Send Error Messages via Slack

Hi,

I am new to the forums but am looking for some assistance as I am very new to python and raspberry pi equipment. I am trying to write a program that will read an input from my UR3 and send a slack message as a bot to all of the employees at our facility to tell them to come reset the robot. I have a Rasp 3+ and a solid state relay to run from the robots output. I need to just read an input on the GPIO and have the change in state trigger a message post. Seems simple enough but there are a lot of functions I am still new to so I am not sure how to write it. If anyone has any ideas of functionality that would help with a solution, it would be greatly appreciated. My goal is to decrease downtime from bad material that the robot is running in the cell.

Thanks,
Tripp

import re
import json
import RPi.GPIO as GPIO
import time
from slackclient import SlackClient

# GPIO SETUP
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
# gpio pins used in program
GPIO.setup(8, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

# SLACK API
slack_client = SlackClient("xoxb-300771038241-1473000243860-5MrBWszttvw2euHD8neePrkB")

# Fetch your Bot's User ID
# PUT YOUR BOT'S NAME
user_list = slack_client.api_call("users.list")
for user in user_list.get('members'):
    if user.get('name') == "agatha":
        slack_user_id = user.get('id')
        break


# functions to automate when message is called
# personalize
def URError():
    GPIO.input(8, 1)
    print("Agatha has stopped!")


# Start connection
if slack_client.rtm_connect():
    print("Connected!")
    # matching messages
    while True:
        for message in slack_client.rtm_read():
            if 'text' in message and message['text'].startswith("<@%s>" % slack_user_id):

                print("Message received: %s" % json.dumps(message, indent=2))

                message_text = message['text']. \
                    split("<@%s>" % slack_user_id)[1]. \
                    strip()

                if re.match(r'.*(URError).*', message_text, re.IGNORECASE):
                    URError()
                    slack_client.api_call(
                        "chat.postMessage",
                        channel=message['channel'],
                        text="Agatha has stopped!",
                        as_user=True)
        time.sleep(1)




indent preformatted text by 4 spaces