File size: 1,730 Bytes
a31ba66 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
import json
import os
import sys
from datetime import datetime, timedelta
import requests
from llama_index import VectorStoreIndex, StorageContext, download_loader
from channel_id_mapper import ChannelIdMapper
token = os.environ.get('SLACK_API_TOKEN') # Retrieve your Slack API token from environment variables
fromDate = datetime.now() - timedelta(90)
index_root = './index'
def index_channels(channels, name_id_map):
SlackReader = download_loader("SlackReader")
loader = SlackReader(token, earliest_date=fromDate)
for channel in channels:
save_location = f"{index_root}/{channel}"
if not os.path.exists(save_location):
os.makedirs(save_location)
documents = loader.load_data([name_id_map[channel]], reverse_chronological=True)
storage_context = StorageContext.from_defaults()
index = VectorStoreIndex.from_documents(documents=documents, storage_context=storage_context)
storage_context.persist(save_location)
def main():
if len(sys.argv) < 2:
print('Usage: python3 index_slack.py channel1,channel2,channel3')
exit(1)
slack_token = os.environ.get('SLACK_API_TOKEN')
if not slack_token:
print('Please set the SLACK_API_TOKEN environment variable.')
exit(1)
channel_id_mapping = ChannelIdMapper(slack_token)
channel_ids_to_index = []
channels = sys.argv[1].split(',')
for channel in channels:
if channel not in channel_dict:
print(f'Channel {channel} not found. Available channels: {channel_dict.keys()}')
exit(1)
channel_ids_to_index.append(channel_dict[channel])
index_channels(channels, channel_dict)
if __name__ == '__main__':
main()
|