1

I'm playing around with trying to make a bot in the slack channel so that I can understand how the whole process works. I have 2 questions. [1st question solved]First, I know how to send a message if someone said anything but I can't figure out how to check what was specifically said. Here's my code:

import os
import time
from slackclient import SlackClient

BOT_TOKEN = os.getenv('SLACK_BOT_USER_TOKEN')
print(BOT_TOKEN)
CH_NM = "bot_testing"


def main():
    sc = SlackClient(BOT_TOKEN)
    SM = sc.rtm_send_message

    if sc.rtm_connect():
        print("Bot running")
        SM(CH_NM, "Started")

        while True:
            for slack_message in sc.rtm_read():
                message = slack_message.get("text")
                user = slack_message.get("user")
                if 'hello' in message:
                    SM(CH_NM, "Hey, <@{}>!".format(user))
                if not message or not user:
                    continue
                SM(CH_NM, """<@{}> wrote something...""".format(user))
            time.sleep(.5)


if __name__ == '__main__':
    main()

The main line I'm having trouble with is

if 'hello' in message:
    SM(CH_NM, "Hey, <@{}>!".format(user))

because I can't iterate through 'message' since it is 'NoneType'. How then would I go about checking if it contained a specific string?

Second question, I see that there are all sorts of event types, but mine only seems to work for the "message" type of events. If I wanted to return something every time a specific user started typing for example what would I add to make that work? I already tried adding typing = slack_message.get("user_typing") but understandably it doesn't work since 'user_typing' is an event type, not part of the message event type I'm pulling 'text' and 'user' out of.

So you know, I'm using python 3.6, Windows 10, powershell.

Cdhippen
  • 615
  • 1
  • 10
  • 32
  • I will definitely do that, but in the case of when I was finally able to stumble across the answer to my own question, and cannot accept that answer for two days, would you recommend just leaving it be for those 2 days? I'm ok with that, I just want to clarify. – Cdhippen Dec 27 '17 at 23:00
  • 2
    That depends. If a question (and your answer to it) might benefit other users (i.e. if there is a chance other users might experience the same problem): leave your question in place and accept your own answer after the 2 days have passed. If your problem is so particular that it is unlikely for others to ever have the same issue, or if you can't even reproduce your problem anymore, it's better to just delete the question. – Ansgar Wiechers Dec 27 '17 at 23:12

1 Answers1

0

The issue was that I had my if not message or not user: continue line below the if 'hello' in message: line, so it would error out due to any other event that is happening that isn't a message. 1st Issue solved.

Cdhippen
  • 615
  • 1
  • 10
  • 32