python - 如何让 Python Slack 机器人在线程内回复?
问题描述
如果我将命令发布在一个线程中,我正在尝试让我的 Python Slack 机器人自动在一个线程中回复。但是,无论我在哪里发布我的命令 - 在线程中或其他地方,它仍然作为一般消息回复。
我希望让它在一个线程中回复。到目前为止,这是我的代码(为简洁起见,我截断了大部分初始化和启动代码):
import os, time, re, inspect, functools
from slackclient import SlackClient
class Bot(object):
def __init__(self, token):
...
def startup(self):
...
def parse_bot_commands(self, slack_events):
"""
Parses a list of events coming from the Slack RTM API to find bot commands.
If a bot command is found, this function returns a tuple of command and channel.
If its not found, then this function returns None, None.
"""
for event in slack_events:
if event["type"] == "message" and not "subtype" in event:
user_id, message = self.parse_direct_mention(event["text"])
if user_id == self.starterbot_id:
return message, event["channel"]
return None, None
def parse_direct_mention(self, message_text):
"""
Finds a direct mention (a mention that is at the beginning) in message text
and returns the user ID which was mentioned. If there is no direct mention, returns None
"""
matches = re.search(self.MENTION_REGEX, message_text)
# the first group contains the username, the second group contains the remaining message
return (matches.group(1), matches.group(2).strip()) if matches else (None, None)
def handle_command(self, command, channel):
"""
Executes bot command if the command is known
"""
# Default response is help text for the user
default_response = "Not sure what you mean. Try *{}*.".format(self.EXAMPLE_COMMAND)
# Finds and executes the given command, filling in response
response = None
# NOTE: This is where you start to implement more commands!
if command.lower().startswith("roll"):
response = 'Rock and Roll!"
# Sends the response back to the channel
self.slack_client.api_call("chat.postMessage", channel=channel, text=response or default_response)
'''START THE BOT!'''
#Initialize the token (when installing the app)
bot = Bot('xxx-xxx')
bot.startup()
解决方案
斜线命令在线程中无法正常工作。它是一个已知问题,迄今为止尚未解决。
另请参阅此答案:Slack 机器人可以获取发送斜杠命令的线程 id 吗?
推荐阅读
- google-cloud-platform - 使用 Pem 文件获取公钥/私钥
- python - PGP 加密密钥用法
- r - 如果不强制列表,数据框可以将向量附加到自身以创建新列吗?
- multithreading - @Transactional 在 Quarkus 的子线程中不起作用?
- git - 在拉取请求期间,Github 对“功能分支代码”而不是“合并后类似代码”运行“Junit”检查
- python - 如何将来自 xarray 中不同数据集的两个变量相加,同时确保仅对具有相同坐标和时间的数据求和?
- python-3.x - 数据框在子功能中被覆盖
- database - 在规范化方面,是否有必要在传递依赖之前去除部分依赖?解释你的观点
- javascript - 如何摆脱警告'类型......不可分配给类型'打字稿
- web-services - 我们如何使用面向服务的架构来自动化金融科技解决方案的应用程序流程?