Slack 块 + 发送响应在 python 中不起作用,即使在异步时也不起作用 - 仅在 CURL 中


  • 向Slack发送块-它在Slack上正确显示
  • 按钮被点击,我得到了交互式事件的webhook
  • webhook向slack返回200 OK,而不是在调度到response_url的异步POST之前
  • 从webhook函数返回200 ack后,返回消息被发布到response_url
  • Slack响应404response_url
  • 当卷曲在python之外时,相同的响应URL也能工作

我不明白为什么slack会拒绝404发布的返回消息,而在python之外的curl中使用相同的response_url时也能工作。

我的webhook处理器:

def slack_webhook(request):
json_dict = json.loads(request.body.decode('utf-8'))
token = json_dict['token'] if 'token' in json_dict else None
message = json_dict['message'] if 'message' in json_dict else None
trigger_id = json_dict['trigger_id'] if 'trigger_id' in json_dict else None
response_url = json_dict['response_url'] if 'response_url' in json_dict else None
actions = json_dict['actions'] if 'actions' in json_dict else None
for action in actions:
print(f"** received action {action['action_id']}")
print(f"** response_url: {response_url}")
print(f"** trigger_id: {trigger_id}")
payload = {
"replace_original": True,
"text": "Success!",
}
# async via Celery...
send_slack_interactive_response.delay(response_url, payload, trigger_id)

return HttpResponse(status=200) 

发送的异步芹菜任务

@app.task(bind=True, retry_kwargs={'max_retries': 10, 'countdown': 5})
def send_slack_interactive_response(self, response_url, payload, trigger_id):
print(f"**  -> response_url: {response_url}")
print(f"** -> trigger_id: {trigger_id}")
if response_url and payload and trigger_id:
headers = {"Content-Type": "application/json"}
payload['trigger_id'] = trigger_id
print(json.dumps(payload))
r = requests.post(response_url, data=payload, headers=headers)
print(r.__dict__)

此函数失败,返回404。然而,当我使用response_urltrigger_id并使用命令行curl创建完全相同的POST时,它就工作了。

我做错了什么?

只是对您的代码的一个评论:您可以执行token = json_dict.get("token", None),从而节省大量代码

关于你的问题:

  1. 确保Celery参数没有奇怪的编码(response_url被发送到messagerie并被编码(
  2. 请确保请求参数使用良好(例如使用json而不是data…(

最新更新