From 138ac0671b71495e7373203a9a91821348bd70e2 Mon Sep 17 00:00:00 2001 From: hasemar Date: Sun, 14 Jan 2018 08:46:56 -0800 Subject: [PATCH 1/2] added another condition to parse_event logic so the slackbot wont recursively call itself, resulting in an infinite loop. Slightly modified return on get_bot_id to always have a return --- .gitignore | 3 +++ bot.py | 5 +++-- event.py | 2 +- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index cd2946a..0121627 100644 --- a/.gitignore +++ b/.gitignore @@ -45,3 +45,6 @@ $RECYCLE.BIN/ Network Trash Folder Temporary Items .apdisk + +# Visual Studio Code stuff +.vscode \ No newline at end of file diff --git a/bot.py b/bot.py index 1531e81..156098d 100644 --- a/bot.py +++ b/bot.py @@ -19,11 +19,12 @@ def get_bot_id(self): if api_call.get('ok'): # retrieve all users so we can find our bot users = api_call.get('members') + user_id = None for user in users: if 'name' in user and user.get('name') == self.bot_name: - return "<@" + user.get('id') + ">" + user_id = "<@" + user.get('id') + ">" - return None + return user_id def listen(self): if self.slack_client.rtm_connect(with_team_state=False): diff --git a/event.py b/event.py index 1a1baaf..f42dbc9 100644 --- a/event.py +++ b/event.py @@ -14,7 +14,7 @@ def wait_for_event(self): self.parse_event(event) def parse_event(self, event): - if event and 'text' in event and self.bot.bot_id in event['text']: + if event and event['type'] == 'message' and self.bot.bot_id in event['text'] and event['user'] not in self.bot.bot_id: self.handle_event(event['user'], event['text'].split(self.bot.bot_id)[1].strip().lower(), event['channel']) def handle_event(self, user, command, channel): From f083b8a54cac2cbef925a4551835e61aea26eff6 Mon Sep 17 00:00:00 2001 From: hasemar Date: Mon, 15 Jan 2018 18:36:00 -0800 Subject: [PATCH 2/2] adjusted parse_event condition because I found a bug when bot posts without text event --- event.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/event.py b/event.py index f42dbc9..49fda08 100644 --- a/event.py +++ b/event.py @@ -14,7 +14,7 @@ def wait_for_event(self): self.parse_event(event) def parse_event(self, event): - if event and event['type'] == 'message' and self.bot.bot_id in event['text'] and event['user'] not in self.bot.bot_id: + if event and 'text' in event and self.bot.bot_id in event['text'] and event['user'] not in self.bot.bot_id: self.handle_event(event['user'], event['text'].split(self.bot.bot_id)[1].strip().lower(), event['channel']) def handle_event(self, user, command, channel):