diff --git a/.gitignore b/.gitignore index 4be2e43..b85f8f7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ /logs/* /secrets/* +/config/* *.pyc # swap [._]*.s[a-v][a-z] diff --git a/README.md b/README.md index c22548a..3c0c715 100644 --- a/README.md +++ b/README.md @@ -35,10 +35,7 @@ As well as messaging them a custom message upon joining the Slack team. # Installing Dependencies PantherBot requires several python libraries to function. These can be easily installed with the `setup.bat` or `setup.sh` file. -``` -sudo setup.sh -``` -`setup.sh` should be run with elevated privileges to ensure dependencies install correctly (this is an issue for most unless you're using a virtualenv). +`setup.sh` should be run with elevated privileges to ensure dependencies install correctly (this is an issue for most unless you're using a virtualenv) (`sudo sh setup.sh` should be good enough). Likewise, pip may request your permission or a prompt if using the `setup.bat` file. If it fails, try using administrative privileges. # Setting up PantherBot diff --git a/bot.py b/bot.py index 3dacea5..37e8740 100644 --- a/bot.py +++ b/bot.py @@ -9,19 +9,20 @@ from apiclient.discovery import build #Other imports from scripts import Help, CatFacts, Flip, GiveFortune, Coin, Pugbomb, Unflip, Calendar, TaskMe -import os, sys, codecs, websocket, datetime, json, logging +import os, io, sys, codecs, websocket, datetime, json, logging #Custom Variables BOT_NAME = 'PantherBot' #Set to whatever you would like the Bot to post his name as in Slack BOT_ICON_URL = 'http://i.imgur.com/QKaLCX7.png' #Set to change whatever the profile picture is when the Bot posts a message SLACK = True #Set to False to disable connecting to the Slack RTM API... for whatever reason GOOGLECAL = False #Set to False to disable connecting and enabling the Google Calendar API integration -LOGGER = False #Set to True to get "detailed" error messages in the console. These error messages can vary from very helpful to utterly useless +LOGGER = True #Set to True to get "detailed" error messages in the console. These error messages can vary from very helpful to utterly useless GOOGLECALSECRET = "PantherBot-test.json" #Can make this a system environment variable if you really want to be careful NEWUSERGREETING = True #Set to True to send users that join the Slack Team a message (GREETING), appended with a link (LINK) (used for whatever you want, in our case, a "How to Use Slack" document) LINK = "https://test.link.pantherhackers.com" #link to be appended to GREETING GREETING = "Greetings newcomer! This is your friendly neighborhood PantherBot, a bot created by your fellow members of PantherHackers! We just wanted to say hello, and welcome you to the family! If Slack seems intimidating, have no fear! If you've ever messed with the likes of Discord, it is a lot like that. If you haven't messed with that either, again, no worries.\nTo get started, you have your default channels on the left (expand the menu by tapping the Panther icon in the top left if you are on mobile). To join more channels, click/tap on the plus button next to \"CHANNELS\" and you'll be well on your way.\nIf you are interested to learn more about Slack, you can go to our custom tutorial here: " + LINK -ADMIN = ["U25PPE8HH", "U262D4BT6", "U0LAMSXUM", "U3EAHHF40"] #Contains user IDs for those allowed to run $ commands +USER_LIST = [] +ADMIN = [] #["U25PPE8HH", "U262D4BT6", "U0LAMSXUM", "U3EAHHF40"] Contains user IDs for those allowed to run $ commands #initialize basic logging to see errors more easily if LOGGER == True: @@ -41,10 +42,7 @@ #function that is called whenever there is an event, including status changes, join messages, typing status, emoji reactions, everything def on_message(ws, message): - #converts to usable string format - s = message.encode('ascii') - - #converts to JSON so we can parse through it easier + s = message response = json.loads(s) print "PantherBot:LOG:message:" + response["type"] @@ -73,9 +71,9 @@ def on_message(ws, message): script_dir = os.path.dirname(__file__) fullDir = os.path.join(script_dir, filename) if os.path.isfile(fullDir) == True: - target = open(fullDir, "a") + target = io.open(fullDir, "a", encoding='utf-8') else: - target = open(fullDir, "w+") + target = io.open(fullDir, "w+",encoding='utf-8') user_name = temp_user["user"]["profile"]["first_name"] + " " + temp_user["user"]["profile"]["last_name"] #format: @@ -87,6 +85,7 @@ def on_message(ws, message): target.close() #Riyan's denial + #GIVES THE 'user' error we always see, its cause bots dont have the user field and its like hold up if "U0LJJ7413" in response["user"]: if response["text"][:1] in ["!", "$"] or response["text"].lower() in ["hey pantherbot", "pantherbot ping"]: rMsg(response, "No.") @@ -150,6 +149,11 @@ def on_message(ws, message): return else: rMsg(response, "It seems you aren't authorized to enable logging. If you believe this a mistake, contact the maintainer(s) of PantherBot") + if args[0].lower() == "$admin": + if response["user"] in ADMIN: + print "PantherBot:LOG:Approved User called $admin" + if args[1].lower() == "add": + adminAdd(response, args) #If not an ! or $, checks if it should respond to another message format, like a greeting elif response["text"].lower() == "hey pantherbot": @@ -176,6 +180,9 @@ def on_message(ws, message): username=BOT_NAME, icon_url=BOT_ICON_URL ) + USER_LIST = sc.api_call( + "users.list", + ) #!/usr/bin/env python # -*- coding: utf-8 -*- @@ -228,6 +235,27 @@ def log(response, words): LOGC = DUMMY return +#Command for adding members to the admin list based on username. +def adminAdd(response, words): + print "PantherBot:LOG:admin add called" + filename = "config/admin.txt" + #this is the only reason this function is here + script_dir = os.path.dirname(__file__) + fullDir = os.path.join(script_dir, filename) + + if os.path.isfile(fullDir) == True: + target = open(fullDir, "a") + else: + target = open(fullDir, "w+") + for user in USER_LIST["members"]: + for x in range(2, len(words)): + if user["name"] == words[x]: + ADMIN.append(user["id"]) + #format: + #USERID\n + target.write(user["id"] + "\n") + target.close() + #Unused things for WebSocketApp def on_error(ws, error): print error @@ -281,6 +309,13 @@ def rMsg(response, t): #If for some reason you need to debug without connecting to the Slack RTM API... this is for you. if SLACK == True: + #load config files + filename = "config/admin.txt" + script_dir = os.path.dirname(__file__) + fullDir = os.path.join(script_dir, filename) + ADMIN = [line.rstrip('\n') for line in open(fullDir)] + print ADMIN + #Get Token from local system environment variables t = os.environ['SLACK_API_TOKEN'] #initiates the SlackClient connection @@ -293,6 +328,11 @@ def rMsg(response, t): token = t ) + #Update current USER_LIST (since members may join while PantherBot is off, I think its safe to make an API call every initial run) + USER_LIST = sc.api_call( + "users.list" + ) + #creates WebSocketApp based on the wss returned by the RTM API print "PantherBot:LOG:Starting WebSocketApplication and connection" ws = websocket.WebSocketApp(bot_conn["url"], diff --git a/setup.bat b/setup.bat index 7935b17..01681de 100644 --- a/setup.bat +++ b/setup.bat @@ -1,3 +1,4 @@ pip install -r requirements.txt mkdir logs mkdir secrets +mkdir cocnfig diff --git a/setup.sh b/setup.sh index 5108fbd..b606a08 100755 --- a/setup.sh +++ b/setup.sh @@ -2,3 +2,4 @@ pip install -r requirements.txt mkdir logs mkdir secrets +mkdir config diff --git a/start.sh b/start.sh index 0103900..7cda7d7 100755 --- a/start.sh +++ b/start.sh @@ -1,2 +1,3 @@ #!/bin/bash -python bot.py +#this is no longer a relative path but for our sake of keeping the Pi as clean as possible, this is necessary. +python ~/PantherBot/bot.py