Skip to content
Snippets Groups Projects
Commit e4fff0af authored by Charlielv04's avatar Charlielv04
Browse files

Add board messages functionality

Add functionality for users to be able to see messages from each of the board members of a given committee
parent 72f85d02
No related branches found
No related tags found
No related merge requests found
from telegram import ReplyKeyboardMarkup, Update
from telegram import ReplyKeyboardMarkup, Update, InlineKeyboardMarkup, InlineKeyboardButton
from telegram.ext import (
CommandHandler,
ContextTypes,
ConversationHandler,
MessageHandler,
filters,
CallbackQueryHandler,
)
from telegram.constants import ParseMode
import re
import time
import datetime
import math
import utils
......@@ -19,13 +21,13 @@ class Committee:
def __init__(self, name, home_handlers: list | None = None, extra_states: dict | None = None):
self.name = name
self.info = utils.config.committees_info[self.name]
self.board_members = "\n".join([key for key in self.info["board"]])
self.board_members = "\n".join([f'{self.info["board"][key]["role"]}: {key}' for key in self.info["board"]])
self.board_keyboard = self.create_keyboard()
self.reply_keyboard = [
["Yay", "Nay"],
]
self.MARKUP = ReplyKeyboardMarkup(self.reply_keyboard, one_time_keyboard=True)
self.EXIT, self.HOME, self.SUB, self.UNSUB = range(4)
self.EXIT, self.HOME, self.SUB, self.UNSUB, self.BOARD = range(5)
self.states = {**{
self.HOME: [
MessageHandler(
......@@ -35,6 +37,9 @@ class Committee:
CommandHandler("event", self.get_events),
CommandHandler("exit", self.exit)
] + (home_handlers if home_handlers else []),
self.BOARD: [
CallbackQueryHandler(self.board_selection)
],
self.SUB: [
MessageHandler(
filters.Regex(re.compile(r'yay', re.IGNORECASE)), self.sub
......@@ -59,18 +64,51 @@ class Committee:
async def intro(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
"""Intro for the bar"""
try:
for line in self.info["messages"]["intro.py"]:
for line in self.info["messages"]["intro"]:
await self.send_message(update, context, text=line)
except KeyError:
await self.send_message(update, context, text=f"Welcome to the {self.name} section of the Telegram Bot")
await self.send_message(update, context, text="In here you can learn about our board, get the link to join our groupchat, find out about our next events and even subscribe to our notifications from this bot")
await self.send_message(update, context, text="To exit this section of the bot just use the command /exit")
return self.HOME
def create_balanced_layout(self):
names = list(self.info["board"].keys())
total_members = len(names)
ideal_group_size = math.isqrt(total_members)
remainder = total_members % ideal_group_size
groups = [names[i:i + ideal_group_size] for i in range(0, total_members - remainder, ideal_group_size)]
# Distribute the remaining members across the groups
for i in range(remainder):
groups[i].append(names[total_members - remainder + i])
return groups
def create_keyboard(self):
layout = self.create_balanced_layout()
keyboard = []
for name_list in layout:
keyboard_row = []
for name in name_list:
keyboard_row.append(InlineKeyboardButton(name, callback_data=name))
keyboard.append(keyboard_row)
keyboard.append([InlineKeyboardButton('Nay', callback_data='Nay')])
return InlineKeyboardMarkup(keyboard)
async def board_selection(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
query = update.callback_query
await query.answer()
if query.data == 'Nay':
await query.edit_message_text(text='Alright')
return self.HOME
await query.edit_message_text(text=self.info["board"][query.data]["message"])
await self.send_message(update, context, text='Do you want to learn more about any other members?', reply_markup=self.board_keyboard)
async def board(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
"""Introduces the board"""
await self.send_message(update, context, text=f"Voici the members of the {self.name} board:\n" + self.board_members)
return self.HOME
await self.send_message(update, context, text='Do you want to learn more about any of these members?', reply_markup=self.board_keyboard)
return self.BOARD
async def exit(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
"""Exit of the committee section"""
......
......@@ -9,7 +9,7 @@ from utils import db, config
load_dotenv()
BOT_TOKEN = os.getenv("SAILORE_BX_BOT")
with open(config.ROOT + '/data/Initial.json', encoding='utf-8') as f:
with open(config.ROOT + '/data/lore.json', encoding='utf-8') as f:
texts = json.load(f)
from telegram import __version__ as TG_VER
......
{
"example": {
"name": "Example \uD83D\uDC40",
"command": "/example",
"board": {
"Joe": {"role": "Prez", "message": "Hello"},
......
......@@ -2,5 +2,5 @@
"start": ["Ahoy Sailor, what can I help you with?", "We can talk about those shiny gems, the mighty Sail'ore or the different committees a pirate can join", "What do you want to talk about?"],
"predetermined": ["Oh me matey I didn't understand what you meant, but I know lots of other stories","We can talk about those shiny gems, the mighty Sail'ore or the different committees a pirate can join"],
"spis": ["Spis are the pompiers of campus, whenever you have any problem you may call them", "Their number is 01 69 33 34 33"],
"coach": ["The coaches are there to help you every time", "The number of the coach in duty is 06 30 01 99 70", "Then the other coaches: \n- Fernando: 06 61 65 30 53\n- Eddy: 06 64 07 86 55\n- Jeremy: 06 86 49 13 14"],
"coach": ["The coaches are there to help you every time", "The number of the coach in duty is 06 30 01 99 70", "Then the other coaches: \n- Fernando: 06 61 65 30 53\n- Eddy: 06 64 07 86 55\n- Jeremy: 06 86 49 13 14"]
}
\ No newline at end of file
from . import config
from . import db
from . import gc
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment