diff --git a/breakbot/.gitignore b/breakbot/.gitignore new file mode 100644 index 0000000..1ea2423 --- /dev/null +++ b/breakbot/.gitignore @@ -0,0 +1,2 @@ +slackbot2.py +keys.txt diff --git a/breakbot/README.md b/breakbot/README.md new file mode 100644 index 0000000..30701f7 --- /dev/null +++ b/breakbot/README.md @@ -0,0 +1,3 @@ +# breakbot +Simple countdown bot for slack. +This bot counts down to a custom date daily. Additionally, there are custom commands to ask for other options. diff --git a/breakbot/date.dat b/breakbot/date.dat new file mode 100644 index 0000000..0d69e04 Binary files /dev/null and b/breakbot/date.dat differ diff --git a/breakbot/slackbot.py b/breakbot/slackbot.py new file mode 100644 index 0000000..58cc8e6 --- /dev/null +++ b/breakbot/slackbot.py @@ -0,0 +1,121 @@ +import time +import datetime +import pickle +from slackclient import SlackClient +from random import randint +import sys + +BOT_ID = sys.argv[1] +dt = datetime.datetime +f = open('date.dat','rb') +targetDate = pickle.load(f) +f.close() + +# constants +AT_BOT = "<@" + BOT_ID + ">" + +# instantiate Slack & Twilio clients +slack_client = SlackClient(sys.argv[2]) + +def handle_command(command, channel): + global targetDate + global dt + response = "Not sure what you mean. Try @break help!" + if command.startswith("days left in hell"): + response = "You have *" + str((targetDate.date() - dt.now().date()).days) + "* days left." + + elif command.startswith("time left in hell"): + timeDelta = str(targetDate - dt.now()).replace(":0",":") + if "," not in timeDelta: + other = " "+timeDelta + days = "0 days" + else: + days, other = timeDelta.split(",") + hours, minutes, seconds = other.split(":") + response = "Only " + days + ","+hours+" hours, "+minutes+" minutes, and "+seconds.split(".")[0]+" seconds left!" + + elif command.startswith("summary"): + timeDelta = str(targetDate - dt.now()).replace(":0",":") + if "," not in timeDelta: + other = " "+timeDelta + days = "0 days" + else: + days, other = timeDelta.split(",") + hours, minutes, seconds = other.split(":") + approx = [ + str((targetDate.date() - dt.now().date()).days) + " days, ", + str(int(hours) + int(days.replace(" days",""))*24) + " hours, ", + ] + approx.append(str(int(minutes)+int(approx[1].replace(" hours, ",""))*60)+" minutes, or ") + approx.append(str(int(minutes)+int(approx[2].replace(" minutes, or ",""))*60)+" seconds.") + + first = "The current target date is *" + targetDate.strftime("%B %d, %Y") + "*!" + second = "There are " + days + ","+hours.lstrip("0")+" hours, "+minutes.lstrip("0")+" minutes, and "+seconds.split(".")[0].lstrip("0")+" seconds left!" + third = "Other representations are: " + "".join(approx); + response = first+"\n"+second+"\n"+third + + elif command.startswith("set new time "): + newDate = command.replace("set new time ","").split(" ") + time = list(map(int, newDate[3].split(":"))) + if newDate[4] == "pm": + if time[0] != 12: + time[0] += 12 + elif newDate[4] == "am" and time[0] == 12: + time[0] -= 12 + try: + newDate = dt(int(newDate[2]),int(newDate[0]),int(newDate[1]),time[0],time[1]) + if(newDate < dt.now()): + response = "Please enter in a date after today!" + else: + targetDate = newDate + print(targetDate) + f = open('date.dat', 'wb') + pickle.dump(targetDate,f) + f.close() + response = "Ok! Your new date is *" + targetDate.strftime("%B %d, %Y %I:%M %p").replace(" 0"," ") + "*!" + except ValueError: + response = "That's an invalid date! The format is: *Day Month Year Hour:Minute AM/PM*" + + elif command.startswith("target date"): + response = "The current target date is *" + targetDate.strftime("%B %d, %Y %I:%M %p").replace(" 0"," ") + "*!" + elif command.startswith("flip a coin"): + coin = ["Heads!", "Tails!"] + response = coin[randint(0,1)] + elif command.startswith("?") or command.lower().startswith("help"): + commands = [ + "Summary: Gives a summary of all information.", + "Days left in hell: Gives days left to date.", + "Time left in hell: Gives countdown left to date.", + "Set new time [Day Month Year Hour:Minute AM/PM]: Sets new target date.", + "Target date: Gives current target date.", + "Flip a coin: Gives heads or tails." + ] + response = "My current commands are:```"+'\n'.join(commands)+"```" + slack_client.api_call("chat.postMessage", channel=channel,text=response, as_user=True) +def parse_slack_output(slack_rtm_output): + """ + The Slack Real Time Messaging API is an events firehose. + this parsing function returns None unless a message is + directed at the Bot, based on its ID. + """ + output_list = slack_rtm_output + if output_list and len(output_list) > 0: + for output in output_list: + if output and 'text' in output and AT_BOT in output['text']: + # return text after the @ mention, whitespace removed + return output['text'].split(AT_BOT)[1].strip().lower(), \ + output['channel'] + + return None, None + +if __name__ == "__main__": + READ_WEBSOCKET_DELAY = 1 # 1 second delay between reading from firehose + if slack_client.rtm_connect(): + print("Bot connected and running!") + while True: + command, channel = parse_slack_output(slack_client.rtm_read()) + if command and channel: + handle_command(command, channel) + time.sleep(READ_WEBSOCKET_DELAY) + else: + print("Connection failed. Invalid Slack token or bot ID?") diff --git a/breakbot/slackid.py b/breakbot/slackid.py new file mode 100644 index 0000000..82918e1 --- /dev/null +++ b/breakbot/slackid.py @@ -0,0 +1,19 @@ +import os +from slackclient import SlackClient + + +BOT_NAME = 'break' + +slack_client = SlackClient("...") + + +if __name__ == "__main__": + api_call = slack_client.api_call("users.list") + if api_call.get('ok'): + # retrieve all users so we can find our bot + users = api_call.get('members') + for user in users: + if 'name' in user and user.get('name') == BOT_NAME: + print("Bot ID for '" + user['name'] + "' is " + user.get('id')) + else: + print("Could not find bot user with the name " + BOT_NAME)