读取传入的松弛消息
Reading incoming slack message
slack 频道每 5 小时发布一次报告,我们需要从中 sort/filter 一些信息并将其放入文件中,那么有什么方法可以连续读取频道或 运行 在此时间前 5 分钟发出一些命令并捕获报告以供将来处理。
是的,这是可能的。以下是解决方案的基本概要:
- 基于有权访问的脚本(例如 Python)创建一个 Slack 应用程序
该频道的历史记录(例如,具有
channels:history
权限范围)
- 使用 cron 在需要的时间调用您的脚本
- 脚本读取频道历史记录(例如
channel.history
用于 public 频道),过滤出它需要的内容
然后将报告存储为文件。
另一种方法是连续读取来自频道的每条新消息,解析触发器(例如发送消息的特定用户或报告的名称),然后在报告出现时对其进行过滤和保护。如果您能确定一个可靠的触发器,根据我的经验,这将是更稳定的解决方案,因为计划的报告可能会延迟。
对于该方法,使用 public 频道的 Events API of Slack instead of CRON and subscribe to receiving messages (e.g. message 事件)。然后,Slack 会在发布每条新消息后立即自动将其发送到您的脚本。
如果您不熟悉创建 Slack 应用程序,我建议您在 Slack API 网站上学习优秀的 official documentation and 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]
slack 频道每 5 小时发布一次报告,我们需要从中 sort/filter 一些信息并将其放入文件中,那么有什么方法可以连续读取频道或 运行 在此时间前 5 分钟发出一些命令并捕获报告以供将来处理。
是的,这是可能的。以下是解决方案的基本概要:
- 基于有权访问的脚本(例如 Python)创建一个 Slack 应用程序
该频道的历史记录(例如,具有
channels:history
权限范围) - 使用 cron 在需要的时间调用您的脚本
- 脚本读取频道历史记录(例如
channel.history
用于 public 频道),过滤出它需要的内容 然后将报告存储为文件。
另一种方法是连续读取来自频道的每条新消息,解析触发器(例如发送消息的特定用户或报告的名称),然后在报告出现时对其进行过滤和保护。如果您能确定一个可靠的触发器,根据我的经验,这将是更稳定的解决方案,因为计划的报告可能会延迟。
对于该方法,使用 public 频道的 Events API of Slack instead of CRON and subscribe to receiving messages (e.g. message 事件)。然后,Slack 会在发布每条新消息后立即自动将其发送到您的脚本。
如果您不熟悉创建 Slack 应用程序,我建议您在 Slack API 网站上学习优秀的 official documentation and 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]