我想遍历python中松弛通道的消息和时间戳

为了遍历松弛通道的消息,我编写了以下内容:


如果您只想遍历 'text' 和 '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 属性中相同的时间戳和相同的文本相同的次数,这是有道理的。


慕田峪4524236
浏览 111回答 1
1回答

红颜莎娜

我认为您应该在循环中用消息替换消息
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python