X Tutup
Skip to content

Latest commit

 

History

History
45 lines (33 loc) · 1.14 KB

File metadata and controls

45 lines (33 loc) · 1.14 KB
title lang slug order
Listening to messages
en
message-listening
1

To listen to messages that your app has access to receive, you can use the message() method which filters out events that aren't of type message.

message() accepts an argument of type str or re.Pattern object that filters out any messages that don’t match the pattern.

# This will match any message that contains 👋
@app.message(":wave:")
def say_hello(message, say):
    user = message['user']
    say(f"Hi there, <@{user}>!")

Using a regular expression pattern

The re.compile() method can be used instead of a string for more granular matching.

import re

@app.message(re.compile("(hi|hello|hey)"))
def say_hello_regex(say, context):
    # regular expression matches are inside of context.matches
    greeting = context['matches'][0]
    say(f"{greeting}, how are you?")
X Tutup