[Python] Sending results from a CSV to a slack user

Discussão em 'Python' iniciado por Stack, Agosto 7, 2025.

  1. Stack

    Stack Membro Participativo

    I am writing a script to help automate tennis club planning. The way I intend this to work

    1. Schedule a Polly poll for club members to sign up on specific days (done)
    2. I manually export the data & upload to databricks (done)
    3. A script in databricks read the csv (done)
    4. script forms groups of 4 or 2 players, avoiding odds (done)
    5. It posts a message in Slack that shows all the groupings & the day they signed up for (need help!)

    Here is the code so far:

    %pip install slack_sdk

    # read csv upload from Polly export

    import pandas as pd

    df = pd.read_csv('/Workspace/Users/christy.kennedy@akunacapital.com/tennis club/27vJwb6Mus4KXRArn.csv')

    # Expand days into one row per person per day
    df_expanded = df.assign(day=df['Response'].str.split(', ')).explode('day')

    # Clean whitespace
    df_expanded['day'] = df_expanded['day'].str.strip()

    # Optional: sort
    df_expanded = df_expanded.sort_values(by=['day', 'Username'])

    from collections import defaultdict

    grouped = defaultdict(list)
    results = []

    for day, group in df_expanded.groupby('day'):
    players = list(group['Username'])
    while len(players) >= 4:
    results.append({'day': day, 'group': players[:4]})
    players = players[4:]
    while len(players) >= 2:
    results.append({'day': day, 'group': players[:2]})
    players = players[2:]
    if players:
    # log or handle leftover player
    results.append({'day': day, 'group': players, 'note': 'Uneven group, consider finding another partner or rescheduling'})

    # Convert to DataFrame
    results = pd.DataFrame(results)
    display(results)


    Up next I want to send the results in a slack message (I have a slack token and a user_id - I want to send this to myself first to test)

    The below test works, I just need to actually send the results:

    #Test Send Slack Bot Message in DM

    from slack_sdk import WebClient

    slack_token = "----------------" # Slackbot token
    client = WebClient(token=slack_token)

    user_id = "-------" # my slack id

    response = client.chat_postMessage(
    channel=user_id, # DM self
    text=" Hello! This is a test message from the tennis club bot."
    )

    print("Success:", response["ok"])




    don't know where to begin here

    Continue reading...

Compartilhe esta Página