Makis Kahrimanidis
Makis Kahrimanidis

Reputation: 93

I want to iterate through messages and timestamp of a slack channel in python

I have wrote the following in order to iterate through messages of a slack channel:

What if you want to iterate through the 'text' and 'ts' only? I have wrote the following myself but im getting an error

TypeError: list indices must be integers or slices, not 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()

Im getting this error:

TypeError: list indices must be integers or slices, not str

If i add index in messages:

messages = response['messages'][0]

Then it will print 10 times or as many times as i have in limit attribute the same timestamp and same text, which makes sense.

Upvotes: 0

Views: 920

Answers (1)

NiklasF
NiklasF

Reputation: 61

I think you should replace messages with message in your loop

Upvotes: 1

Related Questions