如何从松弛通道的 json 响应中提取结果并将其粘贴到其他通道

How to extract result from a json response of slack channel and paste it in other channel

我有点卡住了,我需要提取从松弛通道收到的响应。以下脚本只是从松弛通道中获取结果。

payload = {'token': 'XXXXXX', 'channel': 'C0L8MGLMN' , 'count': '10'}
r = requests.get('https://slack.com/api/channels.history', params=payload)
print r.status_code
pprint(r.json())

这导致:

200
{u'has_more': False,
 u'is_limited': True,
 u'messages': [{u'text': u'This is not a test!',
                u'ts': u'1459763505.000003',
                u'type': u'message',
                u'user': u'U03FE3Z7D'},
               {u'subtype': u'bot_message',
                u'text': u'This is a test!',
                u'ts': u'1459750060.000002',
                u'type': u'message',
                u'username': u'facsimile_test'}],
 u'ok': True}

我想从 json 响应中提取 usernametext。并将提取的消息文件上传到另一个松弛通道。我不知道该怎么做

如果我对你的问题的理解正确,你想将从一个频道提取的消息副本上传到另一个频道。

首先,要从 JSON 中提取消息,您只需像访问 Python 中的任何字典一样访问它。例如提取您使用的第一条消息 r.json()["messages"][0]["text"](另请参阅评论)

接下来,您要post将消息发送到新频道。很遗憾,您无法将精确副本上传到其他频道。但是您可以使用 API 方法 chat.postMessage 将提取的消息的内容重新post 作为新消息发送到另一个频道。请注意,该消息将显示为来自您的机器人而不是原始用户的 post。 (这取决于 'as_user' 的设置以及您使用的令牌类型。)所以您可能想要 post 某事。喜欢 "user x said on channel y: text"

另请参阅 chat.postMessage 的 Slack 文档。