Reputation: 93
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