Skip to content

Commit

Permalink
add source code dockerfile and compose
Browse files Browse the repository at this point in the history
  • Loading branch information
Lyreplus committed Nov 5, 2024
1 parent d745f88 commit 70abd0e
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 0 deletions.
16 changes: 16 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Usa una base di Python
FROM python:3.10

# Imposta la directory di lavoro
WORKDIR /app

# Copia requirements.txt e installa le dipendenze
COPY requirements.txt .
RUN pip install -r requirements.txt

# Copia il codice del bot
COPY . .

# Comando di avvio
CMD ["python", "bot.py"]

93 changes: 93 additions & 0 deletions bot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import os
import psycopg2
from telegram import Update
from telegram.ext import Application, CommandHandler, ContextTypes
from collections import defaultdict

DATABASE_URL = os.getenv("DATABASE_URL")
BOT_TOKEN = os.getenv("BOT_TOKEN")

def create_table():
conn = psycopg2.connect(DATABASE_URL)
cursor = conn.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS odg_topics (
id SERIAL PRIMARY KEY,
topic_id BIGINT,
author TEXT,
topic_text TEXT,
timestamp TIMESTAMPTZ DEFAULT NOW()
)
""")
conn.commit()
cursor.close()
conn.close()


def add_topic_to_db(topic_id, author, topic_text):
conn = psycopg2.connect(DATABASE_URL)
cursor = conn.cursor()
cursor.execute("""
INSERT INTO odg_topics (topic_id, author, topic_text)
VALUES (%s, %s, %s)
""", (topic_id, author, topic_text))
conn.commit()
cursor.close()
conn.close()


def get_topics_from_db(topic_id):
conn = psycopg2.connect(DATABASE_URL)
cursor = conn.cursor()
cursor.execute("""
SELECT topic_text, author FROM odg_topics WHERE topic_id = %s
""", (topic_id,))
rows = cursor.fetchall()
cursor.close()
conn.close()
return rows


def reset_topics_in_db(topic_id):
conn = psycopg2.connect(DATABASE_URL)
cursor = conn.cursor()
cursor.execute("""
DELETE FROM odg_topics WHERE topic_id = %s
""", (topic_id,))
conn.commit()
cursor.close()
conn.close()


# Funzione per gestire i comandi /odg
async def handle_odg(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
topic_id = update.message.message_thread_id if update.message.message_thread_id else update.message.chat.id
author = update.message.from_user.full_name

if len(context.args) == 0:
topics = get_topics_from_db(topic_id)
if topics:
topics_text = ""
for topic_text, author in topics:
topics_text += f"📋{topic_text}\n👤 {author}\n\n"
await update.message.reply_text("Contenuto Ordine Del Giorno:\n\n" + topics_text.strip())
else:
await update.message.reply_text("Niente in programma, cara")
elif context.args[0] == "reset":
reset_topics_in_db(topic_id) # Reset dei topics nel database
await update.message.reply_text("odg reset effettuato")
else:
topic_text = ' '.join(context.args)
add_topic_to_db(topic_id, author, topic_text)
await update.message.reply_text(f"Aggiunto all'odg: {topic_text}")


if __name__ == "__main__":
def main():
create_table()

application = Application.builder().token(BOT_TOKEN).build()
application.add_handler(CommandHandler("odg", handle_odg))
application.run_polling()

main()
18 changes: 18 additions & 0 deletions compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
services:
bot:
build: .
env_file:
- ./env/pythonas.env
depends_on:
- db

db:
image: postgres:13
env_file:
- ./env/pythonas.env
volumes:
- postgres_data:/var/lib/postgresql/data

volumes:
postgres_data:

0 comments on commit 70abd0e

Please sign in to comment.