1 year ago

#254574

test-img

user2178462

Match multiple keywords from string in Python

Doing some exploratory Python, be gentle with me :)

I'm trying to parse a message I'm receiving in Python. What I want to do is: if the 1-n list of keywords I'm looking for in the message match, perform a function (send a message back, as an example).

For example, let's say the incoming message is "The yellow bus drove on green grass under the blue sky": My code below should be able to interpret "yellow","green", and "blue", regardless of the order I actually have listed below, and perform the action.


#this doesn't work, I'm missing something with integrating a list lookup somewhere?
@app.message("green","blue","yellow")
def say_hello(message, say):
    user = message['user']
    say(f"Winner")

#this doesn't work as it only see's "Green" first and accepts it as correct. If the message was "The green and yellow car", that shouldn't be accepted as correct, as blue is not listed
@app.message(re.compile("(green|blue|yellow)"))
def say_hello_regex(say, context):
    greeting = context['matches'][0]
    say(f"Hi")

I spent a lot of time on SO looking through examples os using re.compile, with fun forward/back looking RE examples, piping (OR), etc, but none of them get to what I'm after. I'm thinking RE isn't my answer...I do see people recommending keyword lists, but unsure how I'm supposed to embed into the code.

python

slack

bolt

0 Answers

Your Answer

Accepted video resources