为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕田峪4524236 2022-10-18 19:44:11
为了遍历松弛通道的消息,我编写了以下内容:如果您只想遍历 'text' 和 'ts' 怎么办?我自己写了以下内容,但出现错误TypeError:列表索引必须是整数或切片,而不是 strimport osfrom slack import WebClientfrom slack.errors import SlackApiErrorimport datetimeclient = 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 属性中相同的时间戳和相同的文本相同的次数,这是有道理的。
查看完整描述

1 回答

?
红颜莎娜

TA贡献1842条经验 获得超13个赞

我认为您应该在循环中用消息替换消息



查看完整回答
反对 回复 2022-10-18
  • 1 回答
  • 0 关注
  • 110 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号