Skip to content

Commit

Permalink
complete refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgiolaga committed May 27, 2023
1 parent 358da54 commit e130ca3
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 0 deletions.
10 changes: 10 additions & 0 deletions behaviours/edit_summary.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from telegram import Update
from telegram.ext import CallbackContext

def edit_summary(current_situation, bot_last_message_id, update: Update, context: CallbackContext):
context.bot.edit_message_text(chat_id=update.effective_chat.id, message_id=bot_last_message_id,
parse_mode='markdown', text=current_situation)
try:
context.bot.pin_chat_message(chat_id=update.effective_chat.id, message_id=bot_last_message_id)
except:
print("No admin rights to pin the message")
23 changes: 23 additions & 0 deletions behaviours/print_new_summary.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from telegram import Update
from telegram.ext import CallbackContext
from telegram.utils.helpers import escape_markdown
from db.queries import update_bot_last_message_id_on_db

def print_new_summary(chat_id, current_situation, update: Update, context: CallbackContext):
markdown_error = False
try:
msg = context.bot.send_message(chat_id=update.effective_chat.id, parse_mode='markdown',
text=current_situation)
except:
markdown_error = True
error_message = "Sembra che tu abbia inserito nella descrizione un carattere speciale di telegram (`, *, _).\n" \
"Per favore cambiala con /setdescription <descrizione> assicurandoti di non inserire uno di questi caratteri.\n" \
"Se la tua intenzione era, invece, di formattare il testo, ricordati di usare anche il carattere di chiusura, come in questo *esempio*."
msg = context.bot.send_message(chat_id=update.effective_chat.id, parse_mode='markdown',
text=escape_markdown(error_message))
if not markdown_error:
try:
context.bot.pin_chat_message(chat_id=update.effective_chat.id, message_id=msg.message_id)
except:
print("No admin rights to pin the message")
update_bot_last_message_id_on_db(chat_id, msg.message_id)
7 changes: 7 additions & 0 deletions behaviours/remove_job_if_exists.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
def remove_job_if_exists(name, context):
current_jobs = context.job_queue.get_jobs_by_name(name)
if not current_jobs:
return False
for job in current_jobs:
job.schedule_removal()
return True
20 changes: 20 additions & 0 deletions behaviours/trigger_payment_reminder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from jobs.payment_reminder import payment_reminder
from utils.utils import extract_match_time, extract_match_day, compute_seconds_from_now

def trigger_payment_reminder(update, context, day, time):
chat_id = update.effective_message.chat_id
match_time = extract_match_time(time)
reminder_time_from_now = -1

if match_time == -1:
match_date = extract_match_day(day)
waiting_time_in_seconds = 36 * 3600
else:
match_date = extract_match_day(f"{day} {time}")
waiting_time_in_seconds = 2 * 3600

if match_date != -1:
reminder_time_from_now = compute_seconds_from_now(match_date) + waiting_time_in_seconds

if reminder_time_from_now > 0:
context.job_queue.run_once(payment_reminder, reminder_time_from_now, context=context, name=str(chat_id))
14 changes: 14 additions & 0 deletions jobs/payment_reminder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from db.queries import update_bot_last_message_id_on_db

def payment_reminder(context):
job = context.job
chat_id = job.name

msg = context.bot.send_message(chat_id, text="Ciao bestie 😎, com'è andata la partita? C'è qualcuno che non ha ancora pagato la sua quota?")

try:
context.bot.pin_chat_message(chat_id=chat_id, message_id=msg.message_id)
except:
print("No admin rights to pin the message")

update_bot_last_message_id_on_db(chat_id, msg.message_id)

0 comments on commit e130ca3

Please sign in to comment.