Slack Events API:发布事件(多次)以响应自己的机器人用户的响应 [英] Slack Events API: Events are posted (multiple times) for responses of own bot user

查看:134
本文介绍了Slack Events API:发布事件(多次)以响应自己的机器人用户的响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过Slack Events API响应传入消息时,我似乎有问题( im.消息事件).

I seem to have a problem when responding to incoming messages via the Slack Events API (im.message event).

  • 当用户(在本例中为UQ364CBPF)向我的App Home发送消息时,事件API会将事件正确发布到我的后端(=以下日志的第一行).

  • When a user (in this case UQ364CBPF) sends a message to my App Home, the events API correctly posts an event to my backend (=first line in the logs below).

我用HTTP 200 OK响应该事件,并且在我的代码(请参见下文)中,我触发了Bot用户的响应.

I respond to the event with an HTTP 200 OK, and in my code (see below), I trigger a response from my Bot User.

此响应在Slack中正确发送.

This response is sent correctly in Slack.

但是:之后,事件API会继续发布我自己的机器人用户已在此频道中发布消息的事件...

But: after that, the events API keeps posting events that my own bot user has posted a message in this channel...

此外,事件通常仅发布3次,而这些事件只会不断发布.即使该漫游器用户仅在Slack中发送了一条响应消息.

Also, where the events are usually posted only 3 times, these events just keep being posted non-stop. Even though the bot user only sent one response message in Slack.

User UQ364CBPF has posted message: I'm typing a message to my Slack bot. in DQ5FF35N2 of channel type: im
[26/Dec/2019 15:16:30] "POST /slack/events/ HTTP/1.1" 200 0
User UQ5529KTR has posted message: Thank you for your message! We will get back to you soon! in DQ5FF35N2 of channel type: im
[26/Dec/2019 15:16:32] "POST /slack/events/ HTTP/1.1" 200 0
User UQ5529KTR has posted message: Thank you for your message! We will get back to you soon! in DQ3KX0NPJ of channel type: im
[26/Dec/2019 15:16:33] "POST /slack/events/ HTTP/1.1" 200 0
User UQ5529KTR has posted message: Thank you for your message! We will get back to you soon! in DQ3KX0NPJ of channel type: im
[26/Dec/2019 15:16:35] "POST /slack/events/ HTTP/1.1" 200 0
User UQ5529KTR has posted message: Thank you for your message! We will get back to you soon! in DQ3KX0NPJ of channel type: im
[26/Dec/2019 15:16:37] "POST /slack/events/ HTTP/1.1" 200 0
User UQ5529KTR has posted message: Thank you for your message! We will get back to you soon! in DQ3KX0NPJ of channel type: im
[26/Dec/2019 15:16:39] "POST /slack/events/ HTTP/1.1" 200 0
User UQ5529KTR has posted message: Thank you for your message! We will get back to you soon! in DQ3KX0NPJ of channel type: im
[26/Dec/2019 15:16:40] "POST /slack/events/ HTTP/1.1" 200 0
User UQ5529KTR has posted message: Thank you for your message! We will get back to you soon! in DQ3KX0NPJ of channel type: im
[26/Dec/2019 15:16:42] "POST /slack/events/ HTTP/1.1" 200 0
User UQ5529KTR has posted message: Thank you for your message! We will get back to you soon! in DQ3KX0NPJ of channel type: im
[26/Dec/2019 15:16:43] "POST /slack/events/ HTTP/1.1" 200 0
User UQ5529KTR has posted message: Thank you for your message! We will get back to you soon! in DQ3KX0NPJ of channel type: im

在我的代码中,这是一个更大的函数的一部分,该函数处理Slack事件,相关部分:

In my code, this is part of a bigger function that processes Slack events, the relevant part:

@csrf_exempt
def slack_events(request):
    if request.method == 'POST':
        json_data = json.loads(request.body)
        event_callback_type = json_data['event']['type']
        if event_callback_type == 'message':
                user_id = json_data['event']['user']
                text = json_data['event']['text']
                channel_id = json_data['event']['channel']
                channel_type = json_data['event']['channel_type']
                timestamp = json_data['event']['ts']
                print ('User ' + user_id + ' has posted message: ' + text + ' in ' + channel_id + ' of channel type: ' + channel_type)
                slack_message_received(user_id, channel_id, channel_type, team_id, timestamp, text)
                return HttpResponse(status=200)

这里(或函数slack_message_received中)没有任何东西可以触发此重复流.

There is nothing in here (or in the function slack_message_received) could trigger this recurring flow.

所以

1. Slack用所有这些轰炸我是不是很奇怪的行为 来自我自己的漫游器用户的POST请求?

1. Is it strange behavior that Slack is bombarding me with all these POST requests from my own bot user?

2.除了过滤掉自己的Bot用户ID并在后端忽略这些请求之外,我是否可以避免这种行为?

推荐答案

事件API的标准行为是,您的机器人将接收所有消息事件,包括来自其自己帖子的消息事件.通过对它们的反应,您已经创建了无尽的循环.

It's standard behavior of the Events API that your bot will receive all message events, including from it's own posts. And by reacting to them you have created and endless loop.

要解决此问题,您需要按照#2中的建议将其过滤掉,以停止对机器人自身的消息做出反应.这是标准方法.

To address this issue you need to stop reacting to the bot's own messages by filtering them out as you suggested in #2. That is the standard approach.

最简单的方法是,如果您不需要收听其他漫游器,则仅过滤掉所有漫游器消息.然后,您可以忽略所有具有subtype属性的消息.另外,您当然可以从您自己的漫游器中过滤我们的消息.

The easiest is to just filter out all bot messages, if you don't need to listen to other bots. Then you can just ignore all messages that have the subtype property. Alternatively you can of course filter our messages from your own bot.

这篇关于Slack Events API:发布事件(多次)以响应自己的机器人用户的响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆