编程语言
首页 > 编程语言> > Slack 下一代平台 - “reaction_icode9”事件触发器

Slack 下一代平台 - “reaction_icode9”事件触发器

作者:互联网

创建一个空白项目

当你开始一个新项目时,你可以运行slack create命令。在本教程中,您将从头开始构建一个应用程序。所以从列表中选择“空白项目”:

$ slack create
? Select a template to build from:

  Hello World
  A simple workflow that sends a greeting

  Scaffolded project
  A solid foundational project that uses a Slack datastore

> Blank project
  A, well.. blank project

  To see all available samples, visit github.com/slack-samples.

生成项目后,让我们检查slack run命令是否正常运行。此命令将新应用程序的“开发”版本安装到连接的 Slack 工作区中。现在您的应用程序的机器人用户位于工作区中,并且您的应用程序具有用于 API 调用的机器人令牌。

$ cd distracted-bison-253
$ slack run
? Choose a workspace  seratch  T03E94MJU
   App is not installed to this workspace

Updating dev app install for workspace "Acme Corp"

⚠️  Outgoing domains
   No allowed outgoing domains are configured
   If your function makes network requests, you will need to allow the outgoing domains
   Learn more about upcoming changes to outgoing domains: https://api.slack.com/future/changelog
✨  seratch of Acme Corp
Connected, awaiting events

如果您看到Connected, awaiting events日志消息,则应用已成功连接到 Slack。您可以按“Ctrl + C”来终止本地应用程序进程。

定义工作流和触发器

让我们从定义一个简单的演示工作流及其链接触发器开始。一如既往,将源代码保存为workflow_and_trigger.ts

// ----------------
// Workflow Definition
// ----------------
import { DefineWorkflow, Schema } from "deno-slack-sdk/mod.ts";
export const workflow = DefineWorkflow({
  callback_id: "example-workflow",
  title: "Example Workflow",
  input_parameters: {
    properties: {
      // All the possible inputs from the "reaction_added" event trigger
      channel_id: { type: Schema.slack.types.channel_id },
      user_id: { type: Schema.slack.types.user_id },
      message_ts: { type: Schema.types.string },
      reaction: { type: Schema.types.string },
    },
    required: ["channel_id", "user_id", "message_ts", "reaction"],
  },
});

// TODO: Add function steps here

// ----------------
// Trigger Definition
// ----------------
import { Trigger } from "deno-slack-api/types.ts";
const trigger: Trigger<typeof workflow.definition> = {
  type: "event", // Event Trigger
  name: "Trigger the example workflow",
  workflow: `#/workflows/${workflow.definition.callback_id}`,
  event: {
    // "reaction_added" event trigger
    event_type: "slack#/events/reaction_added",
    channel_ids: ["C04FB5UF1C2"], // TODO: Update this list
    // The condition to filter events
    filter: {
      version: 1,
      // Start the workflow only when the reaction is :eyes:
      root: { statement: "{{data.reaction}} == eyes" },
    },
  },
  inputs: {
    channel_id: { value: "{{data.channel_id}}" },
    user_id: { value: "{{data.user_id}}" },
    message_ts: { value: "{{data.message_ts}}" },
    reaction: { value: "{{data.reaction}}" },
  },
};
export default trigger;

请注意,触发器event.event_type必须是"slack#/events/reaction_added"触发器有五个可能的输入值。要了解最新的输入列表,请参阅官方文档页面data上的属性详细信息

此外,该channel_ids: ["C04DPBYUQUC"], // TODO: Update this list部分需要更新。请注意,您必须选择一个公共(请注意,在撰写本文时,仅支持公共频道)频道来添加此工作流并在 Slack 客户端 UI 中复制其频道 ID。单击频道名称时,将打开一个弹出模式。向下滚动到底部后,您会在那里找到该频道:

然后,将工作流添加到manifest.ts

import { Manifest } from "deno-slack-sdk/mod.ts";
import { workflow as DemoWorkflow } from "./workflow_and_trigger.ts";

export default Manifest({
  name: "distracted-bison-253",
  description: "Demo workflow",
  icon: "assets/default_new_app_icon.png",
  workflows: [DemoWorkflow],
  outgoingDomains: [],
  botScopes: [
    "commands",
    "chat:write",
    "reactions:read", // required for the "reaction_added" event trigger
    "channels:history", // will use in custom functions later
    "channels:join", // will use in custom functions later
  ],
});

请注意,此事件触发器不仅需要添加工作流,还需要添加"reactions:read"范围。botScopes当您在应用程序中使用不同的偶数触发器时,请在此处检查触发器所需的范围。

创建事件触发器

接下来,您将使用两个终端窗口。一个用于slack run命令,另一个用于slack triggers create命令。

要注册工作流,slack run请在第一个终端窗口中运行命令。然后,运行slack triggers create --trigger-def workflow_and_trigger.ts另一个。您将看到以下输出:

$ slack triggers create --trigger-def ./example.ts
? Choose an app  seratch (dev)  T03E*****
   distracted-bison-253 (dev) A04FNE*****

⚡ Trigger created
   Trigger ID:   Ft04EJ8*****
   Trigger Type: event
   Trigger Name: Trigger the example workflow

标签:Slack,icode9,平台,新项目,应用程序,触发,频道
来源: