我写了以下代码,以便遍历松弛通道的消息:
如果你只想遍历“text”和“ts”怎么办?我自己写了以下代码,但我得到了一个错误
TypeError:列表索引必须是整数或切片,而不是字符串
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:列表索引必须是整数或切片,而不是字符串
如果我在消息中添加索引:
messages = response['messages'][0]然后,它将打印相同的时间戳和相同的文本10次或与我在限制属性中打印的次数一样多,这是有意义的。
发布于 2020-04-23 01:22:13
我认为你应该在你的循环中用消息替换消息
https://stackoverflow.com/questions/61371152
复制相似问题