Menu based program using python

Overview:

This Python script is designed to perform a wide range of common tasks such as sending emails, SMS, and WhatsApp messages, as well as interacting with web browsers, opening notepad, fetching data from Wikipedia, and using OpenAI's ChatGPT. It provides a simple menu-based interface that allows users to choose from a variety of actions.

Prerequisites:

  • Python 3.x installed on your system.

  • Internet connection for fetching data from APIs and sending messages.

  • Necessary libraries installed using pip:

      pip install requests beautifulsoup4 smtplib twilio pywhatkit openai
    

Menu Options:

The script offers a menu-driven approach to select and execute tasks. The following functionalities are available:

  1. Send an Email: Uses Python's smtplib to send an email to a specified recipient.

  2. Send SMS: Uses Twilio's API to send an SMS message to a phone number.

  3. Send WhatsApp Message: Leverages the pywhatkit library to send a WhatsApp message at a scheduled time.

  4. Open Chrome: Opens the Chrome browser using the os.system command.

  5. Open Notepad: Opens Notepad using the os.system command.

  6. Interact with ChatGPT: Utilizes OpenAI's GPT-3.5 model to interact in a conversational manner.

  7. Open a Web Page: Opens a web page based on the user's input URL.

  8. Get Wikipedia Data: Fetches and displays data from a Wikipedia page on a topic entered by the user.

  9. Exit: Closes the program.

Script Details:

import os
import webbrowser
import requests
from bs4 import BeautifulSoup
import smtplib
from twilio.rest import Client
import pywhatkit

def open_chrome():
    os.system("chrome")

def open_notepad():
    os.system("notepad")

def open_webpage():
    url = input("Enter the URL: ")
    webbrowser.open(url)

def get_wikipedia_data(topic):
    wikipedia_url = f"https://en.wikipedia.org/wiki/{topic}"
    response = requests.get(wikipedia_url)

    if response.status_code == 200:
        soup = BeautifulSoup(response.text, 'html.parser')
        title = soup.title.text
        paragraphs = soup.find_all('p')

        print(f"Title: {title}\n")

        for paragraph in paragraphs:
            print(paragraph.text)
    else:
        print(f"Failed to retrieve data. Status code: {response.status_code}")

def open_chatgpt():
    import openai
    openai.api_key = "YOUR_API_KEY"  # Replace with your OpenAI API key

    messages = [{"role": "user", "content": "You are an intelligent assistant"}]
    print("Type 'exit' to return to the menu")

    while True:
        message = input("User: ")
        if message:
            messages.append({"role": "user", "content": message})

            chat = openai.ChatCompletion.create(
                model="gpt-3.5-turbo", messages=messages
            )
            chat_reply = chat.choices[0].message.content
            print(f"ChatGPT: {chat_reply}\n")

            messages.append({"role": "assistant", "content": chat_reply})
        if message == "exit":
            break

def send_email():
    my_mail = "your_email@example.com"
    passcode = "your_password"  # Replace with your email password or app-specific passcode

    connection = smtplib.SMTP("smtp.gmail.com")
    connection.starttls()
    connection.login(user=my_mail, password=passcode)

    mail_content = "Subject: Trip on this weekend: \n\nHey, aao kbhi haweli pe."

    try:
        connection.sendmail(from_addr=my_mail, to_addrs="recipient@example.com", msg=mail_content)
    except Exception as e:
        print("Something went wrong:", e)

    connection.close()

def send_sms():
    account_sid = 'YOUR_TWILIO_ACCOUNT_SID'  # Replace with your Twilio Account SID
    auth_token = 'YOUR_TWILIO_AUTH_TOKEN'  # Replace with your Twilio Auth Token

    client = Client(account_sid, auth_token)

    message = client.messages.create(
        to='+91XXXXXXXXXX',  # Replace with destination phone number
        from_='+1XXXXXXXXXX',  # Replace with your Twilio phone number
        body='Hello, this is a test message!'
    )

    print(message.sid)

def send_whatsapp_message():
    pywhatkit.sendwhatmsg("+91XXXXXXXXXX", "Test Message!", 18, 30)

while True:
    print("\t\t\t\t\tWelcome to the MENU")

    print(""" 
        1: Send an Email
        2: Send SMS
        3: Send WhatsApp Message
        4: Open Chrome
        5: Open Notepad
        6: Open ChatGPT
        7: Open Web Page
        8: Get Wikipedia Data
        9: Exit 
    """)

    choice = int(input("Enter your choice: "))

    if choice == 1:
        send_email()
    elif choice == 2:
        send_sms()
    elif choice == 3:
        send_whatsapp_message()
    elif choice == 4:
        open_chrome()
    elif choice == 5:
        open_notepad()
    elif choice == 6:
        open_chatgpt()
    elif choice == 7:
        open_webpage()
    elif choice == 8:
        topic = input("Enter the topic for Wikipedia data: ")
        get_wikipedia_data(topic)
    elif choice == 9:
        break
    else:
        print("Incorrect choice")

Key Security Considerations:

  • Email Credentials: The email username and password should be stored securely. You can use environment variables or secure vaults to protect sensitive credentials.

  • Twilio Credentials: Replace the account_sid and auth_token with your Twilio details. As a best practice, store these in environment variables.

  • API Keys: For the ChatGPT integration, use your OpenAI API key, but ensure it is stored securely, not hard-coded.

  • PyWhatKit: This library uses WhatsApp Web, so ensure you have a stable internet connection for the scheduled messages to work.

Enhancements:

  • You can add additional features like error handling and logging to make the script more robust.

  • Consider implementing OAuth 2.0 for email services instead of using password authentication.

  • For larger projects, separating the functions into modules and organizing the code in a more structured way would be beneficial.