我想在python中迭代松弛通道的消息和时间戳



为了迭代松弛通道的消息,我写了以下内容:

如果只想遍历"text"one_answers"ts",该怎么办?我自己写了以下内容,但我收到了一个错误

TypeError:列表索引必须是整数或切片,而不是str

import os
from slack import WebClient
from slack.errors import SlackApiError
import datetime
client = WebClient(token=os.environ["SLACK_API_TOKEN"])
channel_to_listen = os.environ['CHANNEL_TO_LISTEN']
def main():
response = client.conversations_history(channel=channel_to_listen, limit= 10)
messages = response['messages']
for message in messages:
timestamp = messages['ts']
content = messages['text']
print(timestamp + " " + content)

if __name__ == '__main__':
main()

我收到这个错误:

TypeError:列表索引必须是整数或切片,而不是str

如果我在消息中添加索引:

messages = response['messages'][0]

然后它会打印10次,或者是我在limit属性中打印的次数——相同的时间戳和相同的文本,这是有意义的。

我认为您应该将消息替换为循环中的消息

最新更新