简体   繁体   English

使用Slack Python Webhook API回复线程

[英]Replying to a Thread using Slack Python Webhook API

I'm creating a Python script to send messages to a slack channel. 我正在创建一个Python脚本,以将消息发送到备用频道。 The message itself gets delivered fine, however when I try to start a thread and send replies to the thread, I am unable to get it working. 消息本身可以很好地传递,但是当我尝试启动一个线程并向该线程发送回复时,我无法使其正常工作。 The thread replies appear as a new parent message. 线程回复显示为新的父邮件。

I'm using the slack-python-webhook module found at https://github.com/satoshi03/slack-python-webhook 我正在使用https://github.com/satoshi03/slack-python-webhook上的slack-python-webhook模块

import slackweb
import json
slack = slackweb.Slack(
    url="https://hooks.slack.com/services/XXXX/XXXX/XXXXXX")


attachment = [{"text": "This is TEXT",
              "ts": "1564629129"
              }]


print(json.dumps(attachment))
slack.notify(attachments=attachment)

attachment = [{"text": "This is Thread REPLY",
              "thread_ts": "1564629129",
              "thread_ts": "1564629130"
              }]

print(json.dumps(attachment))
slack.notify(attachments=attachment)

I'd like to know what needs to be changed on the above code snippet so the second message will appear as a thread reply. 我想知道需要在上述代码段中进行哪些更改,以便第二条消息作为线程回复出现。

Your code won't work, because webhooks do not support threads. 您的代码将无效,因为网络钩子不支持线程。 If you want to reply to threads you need to use the API methods for posting message (eg chat.postMessage ). 如果要回复线程,则需要使用API​​方法来发布消息(例如chat.postMessage )。

That means you probably also need to use a different library, since the one mentioned in your question appears to only support webhooks. 这意味着您可能还需要使用其他库,因为问题中提到的库似乎仅支持webhooks。

Also your syntax is not correct. 另外,您的语法不正确。 thread_ts is not a property of an attachment, but a parameter of a API method like channel . thread_ts不是附件的属性,而是API方法(例如channel的参数。

I would recommend to check out the slackclient . 我建议检查slackclient It's the official Python library from Slack and has full support for all API methods incl. 它是Slack的官方Python库,并且完全支持包括incl在内的所有API方法。 threads. 线程。

Here is how to reply to a thread with slackclient: 以下是使用slackclient回复线程的方法:

import slack
import os

# init slack client with access token
client = slack.WebClient(token=os.environ['SLACK_TOKEN'])

# reply to a thread
response = client.chat_postMessage(
    channel="general",
    text="This is a reply",
    thread_ts="1561764011.015500"
)
assert response["ok"]
print(response)

See also this official guide about threads and this answer about webhooks and threads. 另请参阅有关线程的官方指南以及有关webhooks和线程的答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM