在松弛通道中每5小时发布一个报告我们需要从中对一些信息进行排序/过滤并将其放入文件中,那么有没有办法在该时间之前连续读取通道或运行一些命令并捕获报告以备将来处理。
是的,这是可能的。以下是解决方案的基本概要:
channels:history
权限范围)channel.history
作为公共频道),过滤掉所需内容,然后将报告存储为文件。另一种方法是连续读取来自频道的每个新消息,解析触发器(例如发送它的特定用户或报告的名称),然后在报告出现时过滤并保护报告。如果您能够识别出可靠的触发器,那么根据我的经验,这将是更稳定的解决方案,因为预定的报告可能会延迟。
对于该方法,使用Events API of Slack而不是CRON并订阅接收消息(例如,公共频道的message事件)。 Slack会在发布后立即自动将每条新消息发送到您的脚本。
如果您是创建Slack应用程序的新手,我建议您在Slack API网站上学习优秀的official documentation和tutorials,以便开始使用。
可以在这里找到这种方法的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]