From 6c6d7093b6aca3f50866ce1c866b127e11b47b9e Mon Sep 17 00:00:00 2001 From: Colin Powell Date: Fri, 7 Feb 2025 18:14:35 -0500 Subject: [PATCH] Initial commit --- envrc.example | 3 +++ kowalski.py | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 envrc.example create mode 100644 kowalski.py diff --git a/envrc.example b/envrc.example new file mode 100644 index 0000000..22090e4 --- /dev/null +++ b/envrc.example @@ -0,0 +1,3 @@ +SLACK_BOT_TOKEN="xoxb-test" +SLACK_APP_TOKEN="xapp-test" +NOTION_API_TOKEN="test" diff --git a/kowalski.py b/kowalski.py new file mode 100644 index 0000000..774361a --- /dev/null +++ b/kowalski.py @@ -0,0 +1,59 @@ +# /// script +# dependencies = [ +# "slack_bolt", +# ] +# /// +# +import os +import sqlite3 +from slack_bolt import App +from slack_bolt.adapter.socket_mode import SocketModeHandler + +# Initialize SQLite database +conn = sqlite3.connect("message_counts.db") +cursor = conn.cursor() + +# Create table if it doesn't exist +cursor.execute(''' + CREATE TABLE IF NOT EXISTS message_counts ( + user_id TEXT PRIMARY KEY, + message_count INTEGER + ) +''') +conn.commit() + +# Initialize Slack app +app = App(token=os.environ("SLACK_BOT_TOKEN")) + +def update_message_count(user_id): + """Increment the message count for the user in SQLite.""" + cursor.execute("SELECT message_count FROM message_counts WHERE user_id = ?", (user_id,)) + result = cursor.fetchone() + + if result: + # User exists, update their message count + new_count = result[0] + 1 + cursor.execute("UPDATE message_counts SET message_count = ? WHERE user_id = ?", (new_count, user_id)) + else: + # User doesn't exist, insert a new entry with a count of 1 + new_count = 1 + cursor.execute("INSERT INTO message_counts (user_id, message_count) VALUES (?, ?)", (user_id, new_count)) + + conn.commit() + return new_count + +@app.event("message") +def handle_message_events(event, say): + user = event.get("user") + text = event.get("text") + + first_token = text.split(" ")[0] + second_token = text.split(" ")[1] + if "@" in first_token and second_token == INDICATOR: + user_count = update_message_count(user, text) + say(f"{user} has {user_count} kk's!") + +# Start the bot +if __name__ == "__main__": + handler = SocketModeHandler(app, os.environ("SLACK_APP_TOKEN")) + handler.start()