From e09cf4cbfe80c8a230cc1118123a2f508cfa3c52 Mon Sep 17 00:00:00 2001 From: Kenneth Jao Date: Tue, 28 Feb 2017 00:54:45 -0500 Subject: [PATCH 01/10] Initial --- breakbot/date.dat | Bin 0 -> 44 bytes breakbot/slackbot.py | 105 +++++++++++++++++++++++++++++++++++++++++++ breakbot/slackid.py | 19 ++++++++ 3 files changed, 124 insertions(+) create mode 100644 breakbot/date.dat create mode 100644 breakbot/slackbot.py create mode 100644 breakbot/slackid.py diff --git a/breakbot/date.dat b/breakbot/date.dat new file mode 100644 index 0000000000000000000000000000000000000000..2e07a9a1f90ea89f9536adeedc227b57be4b762f GIT binary patch literal 44 ocmZo*PEJWINiE6DP33|!3K^Wa*dMYmGJruLV{0K(P$9D(05KU0o&W#< literal 0 HcmV?d00001 diff --git a/breakbot/slackbot.py b/breakbot/slackbot.py new file mode 100644 index 0000000..79e6f89 --- /dev/null +++ b/breakbot/slackbot.py @@ -0,0 +1,105 @@ +import time +import datetime +import pickle +from slackclient import SlackClient +from random import randint + +BOT_ID = 'U4AMH4C4C' +dt = datetime.datetime +f = open('date.dat','rb') +targetDate = pickle.load(f) +f.close() + +# constants +AT_BOT = "<@" + BOT_ID + ">" +EXAMPLE_COMMAND = "do" + +# instantiate Slack & Twilio clients +slack_client = SlackClient("xoxb-146731148148-cOhyqUViCY4wI2gWYMcoOkpW") + +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()) + 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()) + 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+" hours, "+minutes+" minutes, and "+seconds.split(".")[0]+" seconds left!" + third = "Other representations are: " + "".join(approx); + response = first+"\n"+second+"\n"+third + elif command.startswith("set new time "): + newDate = command.lower().replace("set new time ","").split(" ") + try: + newDate = dt(int(newDate[2]),int(newDate[0]),int(newDate[1])) + if(newDate < dt.now()): + response = "Please enter in a date after today!" + else: + targetDate = newDate + f = open('date.dat', 'wb') + pickle.dump(targetDate,f) + f.close() + response = "Ok! Your new date is *" + targetDate.strftime("%B %d, %Y") + "*!" + except ValueError: + response = "That's an invalid date! The format is: *Day Month Year*" + + elif command.startswith("target date"): + response = "The current target date is *" + targetDate.strftime("%B %d, %Y") + "*!" + 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]: 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 + print (output) + 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..79259fb --- /dev/null +++ b/breakbot/slackid.py @@ -0,0 +1,19 @@ +import os +from slackclient import SlackClient + + +BOT_NAME = 'break' + +slack_client = SlackClient("xoxb-146752022133-ChFrlR3ptyZEJ9uctmOEKL5Z") + + +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) From 8a7ed0b106cbb7c575331e6be8099a9295825098 Mon Sep 17 00:00:00 2001 From: Kenneth Jao Date: Tue, 28 Feb 2017 01:59:57 -0500 Subject: [PATCH 02/10] Time functionality --- breakbot/date.dat | Bin 44 -> 44 bytes breakbot/slackbot.py | 44 +++++++++++++++++++++++++++++-------------- 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/breakbot/date.dat b/breakbot/date.dat index 2e07a9a1f90ea89f9536adeedc227b57be4b762f..0d69e0471f34c02f3719136afdf29f6c6c3133f2 100644 GIT binary patch delta 20 ZcmdPVnIOy0BgX&)g^aC*OhJXrdH^Fp1DyZ> delta 20 XcmdPVnIOy000xDOt%XcMh0J;YAm{^g diff --git a/breakbot/slackbot.py b/breakbot/slackbot.py index 79e6f89..3f6f8a4 100644 --- a/breakbot/slackbot.py +++ b/breakbot/slackbot.py @@ -15,7 +15,7 @@ AT_BOT = "<@" + BOT_ID + ">" EXAMPLE_COMMAND = "do" # instantiate Slack & Twilio clients -slack_client = SlackClient("xoxb-146731148148-cOhyqUViCY4wI2gWYMcoOkpW") +slack_client = SlackClient("xoxb-146731148148-L3NkZn2dI5N645QUU34sl2mi") def handle_command(command, channel): global targetDate @@ -25,13 +25,22 @@ def handle_command(command, channel): response = "You have *" + str((targetDate.date() - dt.now().date()).days) + "* days left." elif command.startswith("time left in hell"): - timeDelta = str(targetDate - dt.now()) - days, other = timeDelta.split(",") + 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()) - days, other = timeDelta.split(",") + 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, ", @@ -41,26 +50,34 @@ def handle_command(command, channel): 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+" hours, "+minutes+" minutes, and "+seconds.split(".")[0]+" seconds left!" + 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.lower().replace("set new time ","").split(" ") + + 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])) + 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") + "*!" + 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*" + 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") + "*!" + 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)] @@ -69,7 +86,7 @@ def handle_command(command, channel): "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]: Sets new target 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." ] @@ -86,7 +103,6 @@ def parse_slack_output(slack_rtm_output): for output in output_list: if output and 'text' in output and AT_BOT in output['text']: # return text after the @ mention, whitespace removed - print (output) return output['text'].split(AT_BOT)[1].strip().lower(), \ output['channel'] From b10f5a644067ed6f4a7721811e252a9ad7bfa429 Mon Sep 17 00:00:00 2001 From: Kenneth Jao Date: Tue, 28 Feb 2017 02:07:53 -0500 Subject: [PATCH 03/10] gitignore --- breakbot/.gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 breakbot/.gitignore diff --git a/breakbot/.gitignore b/breakbot/.gitignore new file mode 100644 index 0000000..5fd4427 --- /dev/null +++ b/breakbot/.gitignore @@ -0,0 +1 @@ +slackbot2.py From 2aa6059b28c733b450f4c30a136c2a68d50aba72 Mon Sep 17 00:00:00 2001 From: Kenneth Jao Date: Tue, 28 Feb 2017 02:16:15 -0500 Subject: [PATCH 04/10] slack key update --- breakbot/slackbot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/breakbot/slackbot.py b/breakbot/slackbot.py index 3f6f8a4..9de5af1 100644 --- a/breakbot/slackbot.py +++ b/breakbot/slackbot.py @@ -15,7 +15,7 @@ AT_BOT = "<@" + BOT_ID + ">" EXAMPLE_COMMAND = "do" # instantiate Slack & Twilio clients -slack_client = SlackClient("xoxb-146731148148-L3NkZn2dI5N645QUU34sl2mi") +slack_client = SlackClient("xoxb-146731148148-MJkaszrYUiBJB8p7340c6wxI") def handle_command(command, channel): global targetDate From 611bc1b7c12d6ca24cdbf9a5ddd47ccf7ec91830 Mon Sep 17 00:00:00 2001 From: Kenneth Jao Date: Tue, 28 Feb 2017 02:29:49 -0500 Subject: [PATCH 05/10] key update --- breakbot/slackbot.py | 2 +- breakbot/slackid.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/breakbot/slackbot.py b/breakbot/slackbot.py index 9de5af1..8609da4 100644 --- a/breakbot/slackbot.py +++ b/breakbot/slackbot.py @@ -15,7 +15,7 @@ AT_BOT = "<@" + BOT_ID + ">" EXAMPLE_COMMAND = "do" # instantiate Slack & Twilio clients -slack_client = SlackClient("xoxb-146731148148-MJkaszrYUiBJB8p7340c6wxI") +slack_client = SlackClient("xoxb-146731148148-GPuD20MDuClwInYYn378ENL9") def handle_command(command, channel): global targetDate diff --git a/breakbot/slackid.py b/breakbot/slackid.py index 79259fb..d63f465 100644 --- a/breakbot/slackid.py +++ b/breakbot/slackid.py @@ -4,7 +4,7 @@ from slackclient import SlackClient BOT_NAME = 'break' -slack_client = SlackClient("xoxb-146752022133-ChFrlR3ptyZEJ9uctmOEKL5Z") +slack_client = SlackClient("xoxb-146752022133-pxiuOc2RXHMJDaPO0hSHL67X") if __name__ == "__main__": From 579dbe061b9338485841d2dc20d276898f94e550 Mon Sep 17 00:00:00 2001 From: Kenneth Jao Date: Tue, 28 Feb 2017 02:51:36 -0500 Subject: [PATCH 06/10] no key update --- breakbot/.gitignore | 1 + breakbot/slackbot.py | 2 +- breakbot/slackid.py | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/breakbot/.gitignore b/breakbot/.gitignore index 5fd4427..1ea2423 100644 --- a/breakbot/.gitignore +++ b/breakbot/.gitignore @@ -1 +1,2 @@ slackbot2.py +keys.txt diff --git a/breakbot/slackbot.py b/breakbot/slackbot.py index 8609da4..e2ae277 100644 --- a/breakbot/slackbot.py +++ b/breakbot/slackbot.py @@ -15,7 +15,7 @@ AT_BOT = "<@" + BOT_ID + ">" EXAMPLE_COMMAND = "do" # instantiate Slack & Twilio clients -slack_client = SlackClient("xoxb-146731148148-GPuD20MDuClwInYYn378ENL9") +slack_client = SlackClient("...") def handle_command(command, channel): global targetDate diff --git a/breakbot/slackid.py b/breakbot/slackid.py index d63f465..8ecbbd7 100644 --- a/breakbot/slackid.py +++ b/breakbot/slackid.py @@ -4,7 +4,7 @@ from slackclient import SlackClient BOT_NAME = 'break' -slack_client = SlackClient("xoxb-146752022133-pxiuOc2RXHMJDaPO0hSHL67X") +slack_client = SlackClient("...") if __name__ == "__main__": From bbac5fb7ae52a1216e3a79358df9287bab509d06 Mon Sep 17 00:00:00 2001 From: yamanq Date: Tue, 28 Feb 2017 12:53:24 -0500 Subject: [PATCH 07/10] add sys.argv --- breakbot/slackbot.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/breakbot/slackbot.py b/breakbot/slackbot.py index e2ae277..24422df 100644 --- a/breakbot/slackbot.py +++ b/breakbot/slackbot.py @@ -3,8 +3,9 @@ import datetime import pickle from slackclient import SlackClient from random import randint +import sys -BOT_ID = 'U4AMH4C4C' +BOT_ID = sys.argv[1] dt = datetime.datetime f = open('date.dat','rb') targetDate = pickle.load(f) @@ -15,7 +16,7 @@ AT_BOT = "<@" + BOT_ID + ">" EXAMPLE_COMMAND = "do" # instantiate Slack & Twilio clients -slack_client = SlackClient("...") +slack_client = SlackClient(sys.argv[2]) def handle_command(command, channel): global targetDate From a5e256677a0229c0f175f8251c25d36b44a9607b Mon Sep 17 00:00:00 2001 From: yamanq Date: Mon, 23 Oct 2017 22:32:04 -0400 Subject: [PATCH 08/10] Create README.md --- breakbot/README.md | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 breakbot/README.md 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. From 9c32fb1682e5cb7648038ebfc83571809a18ef47 Mon Sep 17 00:00:00 2001 From: yamanq Date: Mon, 23 Oct 2017 22:32:20 -0400 Subject: [PATCH 09/10] Update slackid.py --- breakbot/slackid.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/breakbot/slackid.py b/breakbot/slackid.py index 8ecbbd7..82918e1 100644 --- a/breakbot/slackid.py +++ b/breakbot/slackid.py @@ -16,4 +16,4 @@ if __name__ == "__main__": 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) + print("Could not find bot user with the name " + BOT_NAME) From 40c21fd035bb53c9596a2c851302209ebeaf1135 Mon Sep 17 00:00:00 2001 From: yamanq Date: Mon, 23 Oct 2017 22:33:07 -0400 Subject: [PATCH 10/10] Remove unused variable --- breakbot/slackbot.py | 1 - 1 file changed, 1 deletion(-) diff --git a/breakbot/slackbot.py b/breakbot/slackbot.py index 24422df..58cc8e6 100644 --- a/breakbot/slackbot.py +++ b/breakbot/slackbot.py @@ -13,7 +13,6 @@ f.close() # constants AT_BOT = "<@" + BOT_ID + ">" -EXAMPLE_COMMAND = "do" # instantiate Slack & Twilio clients slack_client = SlackClient(sys.argv[2])