anuragak021/leetcode_potd_bot
0
1import requests2from telegram import Update3from telegram.ext import Updater, CommandHandler, CallbackContext4 5# Define the command handler for the /start command6def start(update: Update, context: CallbackContext) -> None:7 update.message.reply_text('Wanna solve today\'s POTD ? uWu :) ')8 9# Define the command handler for the /POTD command10def potd(update: Update, context: CallbackContext) -> None:11 # Fetch the LeetCode Problem of the Day12 response = requests.get('https://leetcode.com/graphql', json={13 "query": """14 query questionOfToday {15 activeDailyCodingChallengeQuestion {16 date17 question {18 title19 titleSlug20 difficulty21 }22 }23 }24 25 """26 })27 28 if response.status_code == 200:29 data = response.json()30 question = data['data']['activeDailyCodingChallengeQuestion']['question']31 title = question['title']32 title_slug = question['titleSlug']33 difficulty = question['difficulty']34 url = f"https://leetcode.com/problems/{title_slug}/"35 36 # Send the problem details37 update.message.reply_text(38 f"LeetCode Problem of the Day:\n\n"39 f"Title: {title}\n"40 f"Difficulty: {difficulty}\n"41 f"URL: {url}"42 )43 else:44 update.message.reply_text('Sorry, I could not fetch the LeetCode Problem of the Day.')45 46def main() -> None:47 # Replace 'YOUR_API_TOKEN' with your actual bot API token48 updater = Updater("7378573823:AAE9Nyb0LnfzHnDAetH3U-Ja1QAR8h4LWek")49 50 # Get the dispatcher to register handlers51 dispatcher = updater.dispatcher52 53 # Register the /start and /POTD command handlers54 dispatcher.add_handler(CommandHandler("start", start))55 dispatcher.add_handler(CommandHandler("POTD", potd))56 57 # Start the Bot58 updater.start_polling()59 60 # Run the bot until you press Ctrl-C or the process receives SIGINT, SIGTERM or SIGABRT61 updater.idle()62 63if __name__ == '__main__':64 main()65 