Skip to content

Commit

Permalink
Add dotenv environment
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanhigueram committed Mar 21, 2024
1 parent c34ea2f commit f11592d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
21 changes: 16 additions & 5 deletions echolab_candidates_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,21 @@
from slack_bolt.adapter.socket_mode import SocketModeHandler
from slack_sdk import WebClient
from tqdm import tqdm
from dotenv import load_dotenv


from src.retrieve_messages import parsing_messages, retrieve_messages
from src.utils import send_messages_to_google_spreadsheet

logging.basicConfig(level=logging.DEBUG)

# Set up the Slack client
# Set up the Slack client and Bolt app

# Load environment variables
load_dotenv()

slack_token = os.getenv("SLACK_API_TOKEN")
slack_bot_token = os.getenv("SLACK_BOT_TOKEN")
slack_secret = os.getenv("SLACK_SIGNING_SECRET")
client = WebClient(token=slack_token)
bolt_app = App(token=slack_token)

Expand Down Expand Up @@ -53,7 +58,9 @@ def event_test(say):
def summary_command(say, ack):
ack("Querying database... 👨🏽‍💻")

conn = sqlite3.connect("/home/topcat/projects/python_slack_bot/data/slackbot_messages.db")
conn = sqlite3.connect(
"/home/topcat/projects/python_slack_bot/data/slackbot_messages.db"
)

query = """
WITH table_group AS (
Expand Down Expand Up @@ -104,7 +111,9 @@ def reload_command(say, ack):
poster_ids = ["U06N7CSQQKZ", "WBA9HFDCL"]
save_data = "/home/topcat/projects/python_slack_bot/data/downloads"

conn = sqlite3.connect("/home/topcat/projects/python_slack_bot/data/slackbot_messages.db")
conn = sqlite3.connect(
"/home/topcat/projects/python_slack_bot/data/slackbot_messages.db"
)
"""Reload database to include new candidates in channel"""
ack("Loading database... 👨🏽‍💻")
# Retrieve messages from the channel
Expand All @@ -123,7 +132,9 @@ def reload_command(say, ack):

# Send parsed messages to Google Spreadsheet
send_messages_to_google_spreadsheet(
parsed_messages, credentials="creds.json", conn=conn
parsed_messages=parsed_messages,
credentials="/home/topcat/projects/python_slack_bot/creds.json",
conn=conn,
)

# Send message to Slacks
Expand Down
3 changes: 1 addition & 2 deletions src/retrieve_messages.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import os
import sqlite3

import pandas as pd
import requests
from slack_sdk.errors import SlackApiError
from tqdm import tqdm

from .extractor import run_model_w_examples
from .database_manager import create_database, structure_messages
from .database_manager import structure_messages


def retrieve_messages(
Expand Down
14 changes: 8 additions & 6 deletions src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,11 @@ def send_messages_to_google_spreadsheet(parsed_messages, credentials, conn):

# Build a data frame with the records in list

if parsed_messages is None:
return
else:
if parsed_messages is not None:
data = pd.concat(parsed_messages)

# Format a nice table using SQL to upload to Google Sheets
data.to_sql(name="parsed_messages", con=conn, if_exists="append", index=False)
# Format a nice table using SQL to upload to Google Sheets
data.to_sql(name="parsed_messages", con=conn, if_exists="append", index=False)

# Send clean data to Google Spreadsheet by replacing channel_id with the channel name
data_tidy = pd.read_sql_query(
Expand All @@ -45,6 +43,10 @@ def send_messages_to_google_spreadsheet(parsed_messages, credentials, conn):
conn,
)

# Transform the ts column to a datetime object and add the current processing date
data_tidy["ts"] = pd.to_datetime(data_tidy["ts"], unit="s")
data_tidy["processing_date"] = pd.to_datetime("today")

# Send data to Google Spreadsheet
if credentials:
gc = gspread.service_account(filename=credentials)
Expand All @@ -56,7 +58,7 @@ def send_messages_to_google_spreadsheet(parsed_messages, credentials, conn):

set_with_dataframe(worksheet, data_tidy)

return data
return None


def load_examples(path_to_examples):
Expand Down

0 comments on commit f11592d

Please sign in to comment.