1

I’m not entirely sure that I’m even thinking about this correctly, but here’s the goal and here’s what I’m thinking.

I need to make a block kit formatted post in a Slack channel when an external monitoring system catches an error. My thought was that I could configure the external monitoring system to send the alert to the incoming webhook I can create in the Slack app. I would then need this webhook to trigger a script which would handle the posting and the necessary user interactions. Does that make sense? Is that possible? I’m doing this in Python. I’ve seen some documentation on posting a message from the payload sent to the webhook but I can’t figure out how to use that webhook to run the script.

This is all relatively new to me so I may be missing some crucial information. I’m just running into a wall with what to Google and feel like I’m only going in circles at this point so hoping for any ideas on how to proceed.

Thanks!

EDIT: What I have right now is mostly focusing on getting the posts to work, so the part of getting it all to trigger with the webhook is what I'm struggling with. Here's what I have right now:

import requests
from slack_sdk import WebClient
from slack_sdk import App
from slack_bolt.adapter.socket_mode import SocketModeHandler
from slack_sdk.errors import SlackApiError

app = App(token=SLACK_APP_TOKEN)
client = WebClient(token=SLACK_APP_TOKEN)
channel_id = "channelID"
channel_id2 = "channelID"
# We need to post in channel 1 to say there is an issue, and then notify the team that would resolve the issue by posting in their channel (channel 2)

message_block = [message block]
message_block2 = [message block]
# first block message goes to channel 1 and second goes to channel 2 so slightly different messaging

try:
    result = client.chat_postMessage(channel=channel_id, text="Error has occurred", blocks=message_block)
    result2 = client.chat_postMessage(channel=channel_id2, text="An error has occurred", blocks=message_block2)
    print(result, result2)
except SlackApiError as e:
    print(f"Error posting message: {e}")

response_ts = result.get("ts")

@app.action("working_on_issue")
# this references a button in the block message. when a user clicks on this button in their team channel, we want to reply to the general alert channel (channel_id1) to notify that the user clicked they are working on it
def working_on_issue_button(ack, body, logger, client):
    ack()
    user_id = body.get("user").get("id")
    button_response = client.chat_postMessage(channel=channel_id, thread_ts=response_ts, text=f"<@{user_id}> is working on this issue.")
    print(button_response)

app.event("working_on_issue")(working_on_issue_button)

def main():
    SocketModeHandler(app, SLACK_SOCKET_TOKEN).start()

if __name__ == "__main__":
    main()
monkeyduo
  • 23
  • 5
  • In the abstract - yes, this is possible. Do you have any code that you've written that attempts to make use of Slack's APIs to do this at all? – Makoto May 02 '23 at 21:40
  • @Makoto Hi! Sorry I should've posted the code. I just updated the original post to include the code and some context. I was looking at using AsyncWebhookClient from slack_sdk.webhook.async_client but I'm not understanding how to utilize that to trigger the rest of the script, or if that would even be the correct thing to use. – monkeyduo May 02 '23 at 22:05

0 Answers0