Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Text not registering with event.get("text") on message sent via datadog app #1587

Open
ebruodok opened this issue Nov 6, 2024 · 5 comments
Labels
question M-T: User needs support to use the project

Comments

@ebruodok
Copy link

ebruodok commented Nov 6, 2024

Hi team 👋
I created a slack app via the python sdk for tagging specific people in messages that the Datadog slack app sends to a channel. For context, we receive monitor alerts in a channel as messages sent by the Datadog app, and we have action items for on call engineers. The idea is that the slack app I created would be able to parse through the text of the message and tag people accordingly.

Unfortunately this isn't working, because seemingly there is no text from this message event.

How I attempt to get the text from the Datadog app's message:

@self.app.event("message"):
def handle_message(self):
    text = str(event.get("text"))
    logging.info("text=%s", text)
    logging.info("trying to get text again=%s", str(event["text"]))

Expected result:

I would expect to recognize the text of the message sent by the Datadog app. Logs should look something like this:

log="2024-11-06 17:17:52,134 - INFO - text=my test text" pod=XXXX
log="2024-11-06 17:17:52,134 - INFO - trying to get text again=my test text" pod=XXXX

Actual result:

Logs show that the app I created is recognizing that an event is sent in a channel, and it correctly identifies the user ID and channel ID. However, it doesn't pick up any text. Logs:

log="2024-11-06 17:17:52,134 - INFO - text=" pod=XXXX
log="2024-11-06 17:17:52,134 - INFO - trying to get text again=" pod=XXXX

Weirdly, if I copy and paste the same message and send it from my own user id, the app I created works as expected. It only fails on messages sent from the Datadog app agent. Is there a different way I should be getting the text from a message sent from a Slack app?

Requirements

For general questions/issues about Slack API platform or its server-side, could you submit questions at https://my.slack.com/help/requests/new instead. 🙇

Please read the Contributing guidelines and Code of Conduct before creating this issue or pull request. By submitting, you are agreeing to those rules.

@seratch seratch added question M-T: User needs support to use the project and removed untriaged labels Nov 7, 2024
@seratch
Copy link
Member

seratch commented Nov 7, 2024

Hi @ebruodok, thanks for your question.

I haven't checked how the Datadog app works, but one possible reason for your situation might be that the app does not set the top-level "text" property when sending a message via the chat.postMessage API. This means the app might only set "blocks" (the Block Kit message data representation) and skip the top-level "text." Although this request is accepted by the Slack server, we generally recommend including both text and blocks for a better user experience. In the channel UI, only the blocks data is used to display the message, but the text is used for mobile app notifications, accessibility features, and Slack's search engine index.

On your end, a feasible workaround is to scan the "blocks" property data to find the text data you're looking for. I'm not sure if this will meet your needs, but I hope it was helpful to you.

@ebruodok
Copy link
Author

ebruodok commented Nov 7, 2024

Thank you so much for responding!
If I'm understanding your response correctly, it seems like if the Block Kit was used for the Datadog app, then we would we would not receive the block's text in the mobile app notification or be able to search for the text via Slack's search engine index. If that's the case, then it doesn't seem like the Datadog app is using blocks- I get phone notifications with the text on the Datadog message, and I am able to search keywords from the Datadog app message.
Am I understanding this correctly?

@seratch
Copy link
Member

seratch commented Nov 7, 2024

If that's the case, message "subtype" events might be causing the confusion you're experiencing. As shown here: https://api.slack.com/events/message#subtypes, there are several additional patterns of message events. Many of these do not have "text" data at the top level (e.g., message_changed, message_deleted).

If your app aims to handle only newly posted messages, it's best to respond only to those patterns. You can check this code to learn how to filter subtype patterns: https://github.com/slackapi/bolt-python/blob/v1.21.2/slack_bolt/app/app.py#L885-L900

If this does not resolve your issue, I'd suggest adding debug logs to print the whole payload your app receives. Also, if the above suggestion resolves the issue, would you mind closing this issue?

@ebruodok
Copy link
Author

ebruodok commented Nov 8, 2024

Unfortunately the app I am creating needs to be able to access the text that the Datadog bot is sending in new messages so it can regex match.
I logged the message subtype and it says None. What would you recommend as next steps? Can you point me to documentation to printing the whole payload please? Thanks.

@seratch
Copy link
Member

seratch commented Nov 11, 2024

Can you point me to documentation to printing the whole payload please?

I meant just printing the whole payload data this way:

@app.event("message")
def handle_message(payload):
    logging.info(payload)

With this, you may be able to see variation of event data patterns. If the payload does not have "text" at top level, the payload pattern might be something different than you expect. If a message event really does not have "text" in it, at least "blocks" or "attachments" should exist. If that's the case, you may be able to use these data to respond to the bot message. That said, I still believe message event data usually has "text".

Also, if your app does not need to handle subtype events, the following code could be simpler:

@app.message("") # matches all newly posted messages
def handle_message(payload):
    logging.info(payload)

To further assist you on this, sharing the actual message event data (rather than just the existence of "text" / "subtype" property) with us would be appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question M-T: User needs support to use the project
Projects
None yet
Development

No branches or pull requests

2 participants