0% found this document useful (0 votes)
26 views2 pages

Carpool Bot - Py

Compete code used for Rideshring service

Uploaded by

binkulsum
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views2 pages

Carpool Bot - Py

Compete code used for Rideshring service

Uploaded by

binkulsum
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

from telegram import Update, ReplyKeyboardMarkup

from telegram.ext import Updater, CommandHandler, MessageHandler, Filters,


ConversationHandler, CallbackContext

# Bot Token from BotFather


TOKEN = "1771845EQE-ROCAT5AD0VuUPLQ9RDV78RF6xHkj4"

# State definitions for ConversationHandler


REGISTER, RIDE_DETAILS, MATCH_RIDE = range(3)

# User storage
users = {}

# Start Command
def start(update: Update, context: CallbackContext):
update.message.reply_text(
"Welcome to the Carpool Bot!\n"
"Please use /register to get started or /find to find a ride."
)

# Registration Command
def register(update: Update, context: CallbackContext):
update.message.reply_text(
"Are you a Driver or a Passenger? (Reply with 'Driver' or 'Passenger')"
)
return REGISTER

# Save User Type


def save_user_type(update: Update, context: CallbackContext):
user_type = update.message.text.lower()
if user_type in ['driver', 'passenger']:
user_id = update.message.from_user.id
users[user_id] = {"type": user_type, "rides": []}
update.message.reply_text(
f"Registration successful as a {user_type.capitalize()}!\n"
"Now please enter your trip details (e.g., 'Origin, Destination, Date,
Time')."
)
return RIDE_DETAILS
else:
update.message.reply_text("Invalid response. Please reply with 'Driver' or
'Passenger'.")
return REGISTER

# Collect Trip Details


def ride_details(update: Update, context: CallbackContext):
ride_info = update.message.text
user_id = update.message.from_user.id
if user_id in users:
users[user_id]["rides"].append(ride_info)
update.message.reply_text(
"Your ride details have been saved!\n"
"Use /find to find matching rides."
)
return ConversationHandler.END
else:
update.message.reply_text("You need to register first using /register.")
return ConversationHandler.END
# Find Matching Rides
def find_ride(update: Update, context: CallbackContext):
user_id = update.message.from_user.id
if user_id not in users:
update.message.reply_text("You need to register first using /register.")
return ConversationHandler.END

user_type = users[user_id]["type"]
matches = []

# Find rides with opposite type


for uid, user_data in users.items():
if uid != user_id and user_data["type"] != user_type:
matches.extend(user_data["rides"])

if matches:
update.message.reply_text(
"Here are the available rides:\n" + "\n".join(matches)
)
else:
update.message.reply_text("No matching rides found at the moment.")

# Cancel Command
def cancel(update: Update, context: CallbackContext):
update.message.reply_text("Operation cancelled.")
return ConversationHandler.END

# Main Function to Set Up the Bot


def main():
updater = Updater(TOKEN, use_context=True)
dispatcher = updater.dispatcher

# Conversation handler for registration and ride details


conv_handler = ConversationHandler(
entry_points=[CommandHandler('register', register)],
states={
REGISTER: [MessageHandler(Filters.text & ~Filters.command,
save_user_type)],
RIDE_DETAILS: [MessageHandler(Filters.text & ~Filters.command,
ride_details)],
},
fallbacks=[CommandHandler('cancel', cancel)],
)

# Handlers
dispatcher.add_handler(CommandHandler("start", start))
dispatcher.add_handler(conv_handler)
dispatcher.add_handler(CommandHandler("find", find_ride))

# Start the Bot


updater.start_polling()
updater.idle()

if __name__ == '__main__':
main()

You might also like