def main():
#updater = Updater("6357993923:AAEM4cTve9hPcKwCHBQrzJT_eO-Vi1swc1M")
from telegram import Update
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackContext
# Dictionary to store user profiles
user_profiles = {}
# Define states
SELECT_ROLE, STUDENT, TUTOR, TUTOR_SUBJECT, TUTOR_AVAILABILITY, STUDENT_SUBJECT = range(6)
def start(update: Update, context: CallbackContext) -> int:
update.message.reply_text(
"Welcome to the Online Tutoring Matching Platform! "
"Are you a Student or a Tutor? Type /student or /tutor to get started."
)
return SELECT_ROLE
def select_role(update: Update, context: CallbackContext) -> int:
user_id = update.message.from_user.id
role = update.message.text.lower()
if role == '/student':
user_profiles[user_id] = {'role': 'student', 'profile': '', 'subject': '', 'availability': '', 'partner_id': None}
update.message.reply_text("You've selected the role of a student. Please enter your profile.")
return STUDENT
elif role == '/tutor':
user_profiles[user_id] = {'role': 'tutor', 'profile': '', 'subject': '', 'availability': '', 'partner_id': None}
update.message.reply_text("You've selected the role of a tutor. Please enter your profile.")
return TUTOR
else:
update.message.reply_text("Invalid role. Please type /student or /tutor.")
return SELECT_ROLE
def profile_handler(update: Update, context: CallbackContext, next_state: int):
user_id = update.message.from_user.id
user_profiles[user_id]['profile'] = update.message.text
update.message.reply_text("Profile created successfully! Please enter your subject.")
return next_state
def student(update: Update, context: CallbackContext) -> int:
return profile_handler(update, context, STUDENT_SUBJECT)
def tutor(update: Update, context: CallbackContext) -> int:
return profile_handler(update, context, TUTOR_SUBJECT)
def subject_handler(update: Update, context: CallbackContext, next_state: int):
user_id = update.message.from_user.id
user_profiles[user_id]['subject'] = update.message.text
update.message.reply_text(f"Great! You've selected {update.message.text} as your subject. Please enter your availability.")
return next_state
def tutor_subject(update: Update, context: CallbackContext) -> int:
return subject_handler(update, context, TUTOR_AVAILABILITY)
def student_subject(update: Update, context: CallbackContext) -> int:
return subject_handler(update, context, STUDENT)
def availability_handler(update: Update, context: CallbackContext):
user_id = update.message.from_user.id
user_profiles[user_id]['availability'] = update.message.text
matched_pairs = match_students_tutors()
if matched_pairs:
partner_id = matched_pairs[0][0]
user_profiles[user_id]['partner_id'] = partner_id
user_profiles[partner_id]['partner_id'] = user_id
update.message.reply_text("Thank you for providing your information. You're now ready to be matched!")
initiate_chat(update, context) # Initiating chat immediately after successful matching
else:
update.message.reply_text("Thank you for providing your information. Please wait for matching.")
def match_students_tutors():
matched_pairs = []
for tutor_id, tutor_profile in user_profiles.items():
if tutor_profile['role'] == 'tutor' and 'subject' in tutor_profile and 'availability' in tutor_profile and 'partner_id' not in tutor_profile:
for student_id, student_profile in user_profiles.items():
if student_profile['role'] == 'student' and 'subject' in student_profile and 'partner_id' not in student_profile:
if student_profile['subject'] == tutor_profile['subject']:
# Check availability and other criteria as needed
matched_pairs.append((student_id, tutor_id))
break # Stop after finding the first match
return matched_pairs
def initiate_chat(update: Update, context: CallbackContext):
user_id = update.message.from_user.id
partner_id = user_profiles.get(user_id, {}).get('partner_id')
if partner_id is None:
update.message.reply_text("You don't have a partner yet. Please wait for matching.")
else:
update.message.reply_text(f"You are now connected with {partner_id}. You can start chatting!")
def main():
updater = Updater("6357993923:AAEM4cTve9hPcKwCHBQrzJT_eO-Vi1swc1M")
dp = updater.dispatcher
dp.add_handler(CommandHandler('start', start))
dp.add_handler(MessageHandler(Filters.regex(r'^/(student|tutor)$'), select_role))
dp.add_handler(MessageHandler(Filters.text & ~Filters.command, student))
dp.add_handler(MessageHandler(Filters.text & ~Filters.command, tutor))
dp.add_handler(MessageHandler(Filters.text & ~Filters.command, tutor_subject))
dp.add_handler(MessageHandler(Filters.text & ~Filters.command, student_subject))
dp.add_handler(MessageHandler(Filters.text & ~Filters.command, availability_handler))
dp.add_handler(CommandHandler('chat', initiate_chat))
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()
Comments
Please log in or sign up to comment.