slackapi / deno-slack-sdk

SDK for building Run on Slack apps using Deno
https://api.slack.com/automation
MIT License
158 stars 27 forks source link

[QUERY] How to listen on all channels or joined channels when create a event trigger like app_mentioned #222

Closed imdgr886 closed 4 months ago

imdgr886 commented 11 months ago

Question

When I follow the tutorials to create a event trigger, I don't know how to set the event propoty.

const trigger: Trigger<typeof AppMentionedWorkflow.definition> = {
  type: TriggerTypes.Event,
  name: "Mentioned response",
  description: "responds to mentioned event",
  workflow: `#/workflows/${AppMentionedWorkflow.definition.callback_id}`,
  event: {
    event_type: TriggerEventTypes.AppMentioned,
    channel_ids: ["C123ABC456"], // <- my question is here, I can't hard code  the channel id
  },
  inputs: {
    message: {
      value: TriggerContextData.Event.AppMentioned.text,
    },
    channel: {
      value: TriggerContextData.Event.AppMentioned.channel_id,
    },
    user: {
      value: TriggerContextData.Event.AppMentioned.user_id,
    },
  },
};
pranith12345 commented 11 months ago

We can update the trigger's Channel Ids at runtime programmatically using the below method. https://api.slack.com/automation/triggers/manage#update_runtime

imdgr886 commented 11 months ago

We can update the trigger's Channel Ids at runtime programmatically using the below method. https://api.slack.com/automation/triggers/manage#update_runtime

but I still have a question, how do the bot knows it was invited into a channel. I see other sdk can receive event for bot_join. But I can't find any event for deno sdk.

Does it mean I mush execute a shotcut command first for get channel id?

Thank you!

pranith12345 commented 11 months ago

yes. i was also facing the same situation . i wrote a shortcut command like AllowBot which captures the ChannelId and dynamically updates the AppMentioned Trigger using the above method.

filmaj commented 11 months ago

Indeed, the channel_id property on many event triggers is required today. However, the team will be working on dropping this requirement in the future. We have no timeline at this moment on when that will be.

In the meantime, dynamically updating a trigger is one workaround, as @pranith12345 mentions. The general pattern of having a shortcut trigger trip a workflow that configures a second workflow that does the work you are trying to accomplish is a common one that we have seen emerge to address this general issue. I have turned to calling this the 'configurator' pattern. I am not a big fan of it but it does get the job done. Hopefully in the near future we can make changes to the platform to remove the need for this pattern.

imdgr886 commented 11 months ago

Indeed, the channel_id property on many event triggers is required today. However, the team will be working on dropping this requirement in the future. We have no timeline at this moment on when that will be.

In the meantime, dynamically updating a trigger is one workaround, as @pranith12345 mentions. The general pattern of having a shortcut trigger trip a workflow that configures a second workflow that does the work you are trying to accomplish is a common one that we have seen emerge to address this general issue. I have turned to calling this the 'configurator' pattern. I am not a big fan of it but it does get the job done. Hopefully in the near future we can make changes to the platform to remove the need for this pattern.

yes shortcut is a good solution to update get the channel_id dynamically. But this deno sdk supports much less events than classical sdk. I want to send a tutorial after the bot was been invited into a channel so my user will get start easier.

it seems can implement by this sdk

filmaj commented 11 months ago

It's true, the new automation platform supports less events at this time compared to the old platform. There are many security and permission considerations related to tokens, workflows and an entire new persona (the builder persona: the person that builds workflows in Workflow Builder, possibly using functions from other third party apps) that need careful review and refactor, all the while maintaining backwards compatibility for the existing platform.

We will be working to improve that, but it will take time. In the mean time, I will certainly forward this feedback to our team so that we can weigh it when prioritizing work.

filmaj commented 5 months ago

An update on this issue: we will be rolling out support for a all_resources: true / false property that can be used instead of channel_ids for the events that have to-date required channel_ids. Support for this should already be live in production, and I am working on exposing this ability for Trigger definitions using this SDK.

filmaj commented 5 months ago

This is now out in deno-slack-sdk 2.10 and deno-slack-api 2.4!

wnamen commented 4 months ago

@filmaj thanks for this update! I was able to add it my slack app, but the bot seems to still only work in the channels whose ID I had specified earlier on and invited. Is there anything else I need to do aside from adding all_resources: true to the event definition?

const trigger: Trigger<typeof workflow.definition> = {
  type: TriggerTypes.Event,
  name: "Trigger the app mention workflow",
  workflow: `#/workflows/${workflow.definition.callback_id}`,
  event: {
    event_type: TriggerEventTypes.AppMentioned,
    // channel_ids: ["old_channel_id"],
    all_resources: true,
  },
  inputs: {
    channel_id: { value: TriggerContextData.Event.AppMentioned.channel_id },
    user_id: { value: TriggerContextData.Event.AppMentioned.user_id },
    message_ts: { value: TriggerContextData.Event.AppMentioned.message_ts },
    text: { value: TriggerContextData.Event.AppMentioned.text },
  },
};
filmaj commented 4 months ago

@wnamen for the app mentioned event, your app has to already be a member of the channel to get notified of the event - even if you set all_resources: true. Can you try testing the scenario where both:

wnamen commented 4 months ago

@filmaj thanks for your quick response. Yep that all makes sense! I tried the following scenarios:

  1. existing channel (pre/post all_resources change) + existing member of channel + was a previously hard coded channel_id -> works!
  2. existing channel (pre/post all_resources change) + existing member of channel + was not previously hard coded channel_id -> does not work :(
  3. new channel (post all_resources change) + invited to new channel + was not previously hard coded channel_id -> does not work :(

Here's a screenshot of 3. The bot should respond with a message. I checked the slack activity logs and I didn't see any event.

Screenshot 2024-04-22 at 10 04 41 AM
filmaj commented 4 months ago

Going to re-open this issue and investigate. A couple of requests:

The above is mostly so that I can try to reproduce the problem. Appreciate your patience and assistance in providing this info 🙇

wnamen commented 4 months ago

@filmaj false alarm! Your second bullet reminded me that our team had not run the slack trigger update command. After doing so and re-deploying, it works as expected.

For what it's worth, we definitely expected the slack deploy command to act as a rebuild of the trigger definitions. Sorry for dragging you into this rabbit hole.

filmaj commented 4 months ago

@wnamen no worries, I'm happy to hear it is working for you. And totally hear you re: management of triggers; having triggers live in their own kind of 'lifecycle', with separate create/update commands in certain cases, creates an experience where I think it's easy to forget updating the trigger definition. For example, when deploying or running an app, and the CLI detects that your app has no triggers created and there are trigger definitions available in your project, it will ask if you want to create a trigger. While this is a nice first-run experience, the CLI doesn't provide this kind of experience once triggers are defined for the app - which I think naturally leads to the situation you found yourself in!

I wonder what more we could do here to prevent this from happening 🤔

In any case, I will close this issue, but if you have further questions or problems feel free to re-open or file a new issue.

wnamen commented 4 months ago

@filmaj these are great thoughts. I think in the meantime, we will expand our CI/CD scripts to detect when a commit contains a trigger/workflow def change and throw an error/warning reminder. It's a simple solution while y'all continue to explore improvements for the slack cli.