AI Assistant From Basic to Advance - Part - 2

# ADDING FEATURES

#Function/Features : 

  • Click to preview!

#Feature 1/2 is in Part-1

#Feature-3: Open chrome/website
# Import library : webbrowser


#How to get Chrome path: 
  • Right-click on chrome and open file location --> right-click on chrome.exe
  • Or simply Press shift right-click and copy path location
  • Remember to use forward-slash (/) in Pycharm
  • Refer to the below image
CLICK TO VIEW!
NOTE:  I made a  function of open_chrome here instead of directly implementing in elif!
  1. def open_chrome():
  2. # url u want to open
  3. url = 'http://docs.python.org/'

  4. # Windows
  5. chrome_path = 'C:/Program Files/Google/Chrome/Application/chrome.exe %s'
  6. # replace chrome_path with the correct path for your platform.

  7. # MacOS
  8. # chrome_path = 'open -a /Applications/Google\ Chrome.app %s'

  9. # Linux
  10. # chrome_path = '/usr/bin/google-chrome %s'
  11. webbrowser.get(chrome_path).open(url)
  12. # call the function
Add in Main --> Nested_elif
  1. elif "chrome" in query: # quit to end the program
  2. speak("opening the Chrome")
  3. open_chrome()

#How to add our New Function/Feature in our Main_Function 
The function should be defined before the Main_Function and in Main_Function add another Nested if-else and call the function
#Refer to the image

CLICK TO VIEW!
#Feature-4: Launch Application/software

# Import library: subprocess

The subprocess the module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. This module intends to replace several older modules and functions:

Add in Main --> Nested_elif
  1. elif "open notepad" in query: # if open notepad in statement
  2. speak("opening notepad") # speak
  3. location = "C:/WINDOWS/system32/notepad.exe" # location
  4. notepad = subprocess.Popen(location) # location of a software you want tot opem
  5. elif "close notepad" in query:
  6. speak("closing notepad")
  7. notepad.terminate() # terminate

#Feature-5: Wikipedia search

# Import library: wikipedia
  • pip install wikipedia

#Add in Main --> Nested_elif
  1. elif "wikipedia" in query:
  2. speak("Searching...")
  3. query = query.replace("wikipedia", "")
  4. result = wikipedia.summary(query, sentences = 2)
  5. speak(result)
#Feature-6: Chrome search
# Import library: webbrowser  as wb

#Add in Main --> Nested_elif
  1. elif "search" in query:
  2. speak("what should i search?")
  3. chromepath = "C:/Program Files/Google/Chrome/Application/chrome.exe %s" # location
  4. search = takeCommand().lower()
  5. wb.get(chromepath).open_new_tab(search + ".com")
#Feature-7: Jokes
# Import library: pyjokes
  • pip install pyjokes
#Add in Main --> Nested_elif
  1. elif "joke" in query:
  2. speak(pyjokes.get_jokes())
#Feature-8: Logout/Shutdown/Restart
# library needed
    • import os
    • from time import sleep
    #Add in Main --> Nested_elif
    1. # Logout/Shutdown/Restart
    2. elif "logout" in query:
    3. speak('logging out in 5 second')
    4. sleep(5)
    5. os.system("shutdown - l")

    6. elif "shutdown" in query:
    7. speak('shutting down in 5 second')
    8. sleep(5)
    9. os.system("shutdown /s /t 1")

    10. elif "restart" in query:
    11. speak('restarting in 5 second')
    12. sleep(5)
    13. os.system("shutdown /r /t 1")
    #Full code of part-2

    #PART-2-CODE

    COMMENT IF YOU FACE ANY PROBLEM!

    9 Comments

    If you have any doubts, please let me know

    Previous Post Next Post