npm 包 hubot-interactive-messages 使用教程

阅读时长 6 分钟读完

什么是 hubot-interactive-messages

hubot-interactive-messages 是一个基于 Node.js、Hubot 和 Slack 的 npm 包,用于在 Slack 中创建交互式消息。 基于 Slack 手册中推出的交互式消息功能,可以在消息中里嵌入多个按钮,让用户能够更便捷地与 bot 进行交互。在时间敏感性比较高的业务场景中,使用 hubot-interactive-messages 可以大幅提升工作效率。

安装与使用

安装

使用 npm 安装即可:

使用

hubot-interactive-messages 可用在 Hubot 和 Slack 之间传递交互消息。

这里以 Hubot 为例:

-- -------------------- ---- -------
-------------- - -------- ------- -
  --- ------------------- - --------------------------------------
  ---------------------------

  -------------------------------- -------- ------ -
    --- ------- - ------------------
    --- --------- - ------------------
    --- ----------- - ------

    -------------------------- ---------- -
      -- -------------- --- ----- -
        ----------- - -----
        ------ ------
      -
    ---

    -- -------------- -
      -------
    -

    ------------------------------------- -
      -
        ----- ---------------
        --------- ---------------- ----- ------ --- ----
        ------------ ---------------
        ---------------- ----------
        -------- -
          - ----- ------ ----- ----- ----- --------- ------ ----- --
          - ----- ----- ----- ----- ----- --------- ------ ---- --
          - ----- ---------- ----- ----- ----- --------- ------ --------- --
        --
      --
    -- -
        ----- ------
        -------- -------------------
        ---------- -----------
        -------- -----
    ---
  ---

  ----------------------------- -------- ----- -
    --- ------- - ---------------------
    --- ------- - -------------------------------

    ------------------------ -------- -
      -- ------------- --- --------------------- -
        --------------------------------- -
          ----- -------- - ------------
        ---
      -
    ---
  ---
--

代码中,使用 Hubot 声明了事件消息,配置了交互式消息。点击 Hubot 发出的消息中的 +1 后,自动触发 Slack.attachmentActions 事件,显示 werewolf 投票选项。

深度解析

Slack 消息中的附加信息

Slack 的消息体是一个对象,包括 text、attachments、reactions 等信息。其中,reactions 指点赞图标。

-- -------------------- ---- -------
-
    ----- ----------
    ----- ------------
    ----- ----- -- ---- ------------
    --- --------------------
    -------- ------------
    --------- --------------------
    ---------- --
        ----- -----
        ------ --
        ------ -------
    --
-

交互式消息的数据传递

使用 hubot-interactive-messages,将交互消息传回 Hubot 以进行后续处理。

代码示例:

在发送 attachment 消息时,通过 emit 函数触发 Slack.attachmentActions 事件,这是 hubot-interactive-messages 封装的函数,用于向 Hubot 发送交互式消息。而 emit 函数所带的第二个参数,是一个消息配置对象,包括 text、channel、timestamp 等信息,表示最终在 Slack 中要显示的消息内容和位置。

接收到数据后,在 Hubot 代码中进行处理:

这里用 on 来监听 Slack.attachmentActions 事件,接收从 Slack 中带回来的数据。可以定义不同的 callback_id 以进行不同风格、不同用途的消息传递。

总结

本文介绍了使用 hubot-interactive-messages 创建交互式消息的基本流程,同时分享了代码示例以及深入解析了部分实现细节。

交互式消息是企业消息传递中的重要一环,对提升工作效率与准确性有很大帮助。希望本文内容对广大前端开发者有所帮助,通过 hubot-interactive-messages 打造更高效智能的工作流。

来源:JavaScript中文网 ,转载请注明来源 https://www.javascriptcn.com/post/600562e281e8991b448e06d2

纠错
反馈