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.