Skip to content

Commit

Permalink
implents ignoring @ mentions which are not meant for the bot itself
Browse files Browse the repository at this point in the history
refs: #44
  • Loading branch information
bb-Ricardo committed Aug 13, 2020
1 parent 727f710 commit 6bedbe2
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions icinga-bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
args = None
config = None
user_info = SlackUsers()
my_user_id = None


#################
Expand Down Expand Up @@ -93,11 +94,6 @@ async def handle_command(slack_message, slack_user=None):

default_response_text = "I didn't understand the command. Please use `help` for more details."

# strip any mention "strings" from beginning of message
matches = re.search(mention_regex, slack_message)
if matches:
slack_message = matches.group(2).strip()

bot_commands = BotCommands()
called_command = bot_commands.get_command_called(slack_message)

Expand Down Expand Up @@ -144,6 +140,20 @@ async def handle_command(slack_message, slack_user=None):
return response


@slack.RTMClient.run_on(event="open")
async def client_start_handler(**payload):
"""read web socket info and retrieve own user id
Parameters
----------
payload : object
Slack payload to parse
"""
global my_user_id
my_user_id = payload.get('data').get('self').get("id")


# noinspection PyUnresolvedReferences
@slack.RTMClient.run_on(event="message")
async def message(**payload):
Expand All @@ -166,18 +176,29 @@ async def message(**payload):
if data.get("text") is not None:
channel_id = data.get("channel")
bot_id = data.get("bot_id")
slack_message = data.get("text")

# don't answer if message was sent by a bot
if bot_id is not None:
return

logging.debug("Received new Slack message: %s" % data.get("text"))

# strip any mention "strings" from beginning of message
matches = re.search(mention_regex, slack_message)
if matches:

if matches.group(1) != my_user_id:
logging.debug("This message was not for me. Ignoring.")
return

slack_message = matches.group(2).strip()

# noinspection PyTypeChecker
user_info.set_web_handle(web_client)

# parse command
response = await handle_command(data.get("text"), user_info.get(data.get("user")))
response = await handle_command(slack_message, user_info.get(data.get("user")))

slack_api_response = post_slack_message(web_client, channel_id, response)

Expand Down

0 comments on commit 6bedbe2

Please sign in to comment.