阅读传入的松弛消息

问题描述 投票:3回答:2

在松弛通道中每5小时发布一个报告我们需要从中对一些信息进行排序/过滤并将其放入文件中,那么有没有办法在该时间之前连续读取通道或运行一些命令并捕获报告以备将来处理。

python python-2.7 python-3.x slack slack-api
2个回答
1
投票

是的,这是可能的。以下是解决方案的基本概要:

  • 基于脚本(例如在Python中)创建一个Slack应用程序,该脚本可以访问该通道的历史记录(例如,具有channels:history权限范围)
  • 使用cron在需要的时间调用脚本
  • 该脚本读取频道历史记录(例如,使用channel.history作为公共频道),过滤掉所需内容,然后将报告存储为文件。

另一种方法是连续读取来自频道的每个新消息,解析触发器(例如发送它的特定用户或报告的名称),然后在报告出现时过滤并保护报告。如果您能够识别出可靠的触发器,那么根据我的经验,这将是更稳定的解决方案,因为预定的报告可能会延迟。

对于该方法,使用Events API of Slack而不是CRON并订阅接收消息(例如,公共频道的message事件)。 Slack会在发布后立即自动将每条新消息发送到您的脚本。

如果您是创建Slack应用程序的新手,我建议您在Slack API网站上学习优秀的official documentationtutorials,以便开始使用。


2
投票

可以在这里找到这种方法的Python示例:https://gist.github.com/demmer/617afb2575c445ba25afc432eb37583b

此脚本计算每个用户的邮件数量。

基于此代码,我为您创建了以下示例:

# get the correct channel id
for channel in channels['channels']:
    if channel['name'] == channel_name:
        channel_id = channel['id']
if channel_id == None:
    raise Exception("cannot find channel " + channel_name)

# get the history as follows: 
history = sc.api_call("channels.history", channel=channel_id)

# get all the messages from the history: 
messages = history['messages']

# Or reference them by ID, so in this case get the first message:
ids = messages[0]
© www.soinside.com 2019 - 2024. All rights reserved.