Slack 后端:是否可以检测到编辑?

Slack backend: is it possible to detect an edit?

我们的 errbot 在看到正确的工单模式时向 JIRA 工单提供 links。不幸的是,在 slack 中,用户编辑他们的帖子是很常见的,如果两次编辑都包含 JIRA 票证模式,errbot 会提供两次 link,这很烦人。

我能否检测到消息是经过编辑的而不是原始消息?

这目前不可能(在任何后端上),不。对于允许编辑消息的聊天网络,errbot 目前将编辑后的消息作为新消息注入。

如果您需要此功能,请在 errbot 的问题跟踪器上提出问题,以便我们集思广益,讨论如何引入此功能。

现在您可以了,因为您可以从消息的 .extra 参数中获取来自 slack 的消息 ID。

我的机器人也关注并扩展对 Jira 问题的引用;我所做的其中一件事是跟踪在频道中看到了多少消息,以及我最后一次回复任何给定问题的时间。在最后 N(我使用 10)条消息中扩展的问题将被忽略。这样,如果他们自己编辑问题密钥,他们通常会得到一个新的扩展,但如果他们编辑消息的其他部分,则不会。

def activate(self):
    self.messages_seen={} # room: messages seen
    self.last_shown={} # issue_key : { room : message number last shown }
    super().activate()

def callback_message(self, msg):
    if msg.is_group:
        try:
            self.messages_seen[msg.to.id] += 1
        except KeyError:
            self.messages_seen[msg.to.id] = 1

def record_expanded(self, msg, key, orig_key=None):
    if msg.is_group:
        channel=msg.to.id
        keys = [ key, orig_key ] if orig_key and orig_key != key else [ key ]
        for k in keys:
            if k in self.last_shown:
                self.last_shown[ k ][ channel ] = self.messages_seen[ channel ]
            else:
                self.last_shown[ k ] = { channel : self.messages_seen[ channel ] }

def should_expand(self, msg, key):
    expand=False
    if msg.body.split()[0] == 'jira':
        # User said 'jira <key>', so always expand
        expand=True
    if msg.is_group:
        channel=msg.to.id
        message_number=self.messages_seen.get(channel, 1)

        expanded_in = self.last_shown.get(key, {})
        if expanded_in:
            if channel not in expanded_in: # key has been expanded, but not in this channel
                expand=True
            else:
                expanded_last_here = message_number - expanded_in[channel]
                if expanded_last_here >= self.bot_config.JIRA_EXPAND_ONLY_EVERY: # not recently enough
                    expand=True
                else:
                    self.log.debug("not expanding '%s' because expanded %d messages ago" % (key, expanded_last_here))
        else:
            # this key has not been seen anywhere before
            expand=True
    else:
        # direct IM - always expand
        expand=True
    if not expand:
        self.log.debug("Recently expanded %s, suppressing" % key)
    return expand