Last active
August 15, 2019 12:26
-
-
Save benjiao/a86581472d4925f61a314f5dadf2345c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import time | |
import json | |
import psutil | |
from slackclient import SlackClient | |
slack_client = SlackClient("xoxb-103696790404-jv1XDqw2w5dezNWZy0K5ykdG") | |
# Fetch your Bot's User ID | |
user_list = slack_client.api_call("users.list") | |
for user in user_list.get('members'): | |
if user.get('name') == "pibot": | |
slack_user_id = user.get('id') | |
break | |
# Start connection | |
if slack_client.rtm_connect(): | |
print "Connected!" | |
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'.*(cpu).*', message_text, re.IGNORECASE): | |
cpu_pct = psutil.cpu_percent(interval=1, percpu=False) | |
slack_client.api_call( | |
"chat.postMessage", | |
channel=message['channel'], | |
text="My CPU is at %s%%." % cpu_pct, | |
as_user=True) | |
if re.match(r'.*(memory|ram).*', message_text, re.IGNORECASE): | |
mem = psutil.virtual_memory() | |
mem_pct = mem.percent | |
slack_client.api_call( | |
"chat.postMessage", | |
channel=message['channel'], | |
text="My RAM is at %s%%." % mem_pct, | |
as_user=True) | |
time.sleep(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@danflurry try using threads so you can keep listening to more requests while you process something.