A Python Chatbot

Rohit Kumar Dubey
3 min readAug 20, 2020

I have tried creating a python code which interacts with human written statements and acts accordingly for running the system applications of your machine.

The prerequisite of this code is that every system application you desire to run should have its path set in the Environment variables setup.

Here is how the code begins….

The first mentioned piece of code has a while loop above it only because the menu gets popped up again and again until or unless the user ask it to stop. then after a couple of libraries are used which would help the entire code.

# making the entire code under a while loop so that the code dies not shuts down until or unless askedwhile True:    

# importing two libraries
import os
import pyttsx3

Then a text message is displayed infront of the user

a=input("How can I help you? : ")

An if-elseif block is used for matching the conditions used by the user in the code. The option launches the ‘notepad’, as well as audio, is played which says “launching notepad for you”.

# for launching notepad

if(("notepad" in a) or ("editor" in a))and(("run" in a) or ("execute" in a) or ("launch" in a)or ("open" in a)):
os.system("notepad")
pyttsx3.speak("Launching notepad for you")

The option launches the ‘Google Chrome’ browser as well as an audio is played which says “launching google chrome for you”.

# for launching google chrome

elif(("chrome" in a) or ("google chrome" in a) or ("browser" in a))and(("run" in a) or ("execute" in a) or ("launch" in a)or ("open" in a)):
os.system("start chrome")
pyttsx3.speak("Launching Google chrome for you")

The option launches the ‘Microsoft Edge’ as well as an audio is played which says “launching google chrome for you”.

# for launching microsoft edgeelif(("edge" in a) or ("microsoft edge" in a) or ("browser" in a))and(("run" in a) or ("execute" in a) or ("launch" in a)or ("open" in a)):
os.system("msedge")
pyttsx3.speak("Launching Google chrome for you")

The option launches the ‘Visual Studio Code’ as well as an audio is played which says “launching google chrome for you”.

# for launching visual studio codeelif(("code" in a) or ("code editor" in a) or ("ide" in a) or ("vs code" in a) or ("visual studio code" in a))quitand(("run" in a) or ("execute" in a) or ("launch" in a)or ("open" in a)):        
os.system("Code")
pyttsx3.speak("Launching visual studio code for you")

The option launches the ‘VLC’ as well as an audio is played which says “launching vlc player for you”.

# for launching vlc media playerelif(("vlc" in a) or ("audio player" in a) or ("video player" in a) or ("audio video player" in a) or ("vlc player" in a))quitand(("run" in a) or ("execute" in a) or ("launch" in a)or ("open" in a)):
os.system("vlc")
pyttsx3.speak("Launching vlc player for you")

The option launches the ‘Windows Media Player’ as well as an audio is played which says “launching Windows Media Player for you”.

# for launching windows media playerelif(("windows media player" in a) or ("audio player" in a) or ("video player" in a) or ("audio video player" in a))quitand(("run" in a) or ("execute" in a) or ("launch" in a)or ("open" in a)):
os.system("wmplayer")
pyttsx3.speak("Launching windows media player for you")

The option quits the program as we have used break keyword which terminated the while loop in which it is placed as well as an audio is played which says “shutting down the program for you, have a good day”.

# for stoping the codeelif(("quit" in a)or("stop" in a)):        
pyttsx3.speak("shutting down the program for you, have a good day")
break

The option is executed only when an invalid input is made in the program it displays and plays an audio which says “Command is not recognized, Sorry”.

# for an invalid statement entered    
else:
print("Command is not recognized, Sorry!")
pyttsx3.speak("Command is not recognized, Sorry")

With this we come to the end of code, though it needs a lot of improvement but its’ good to give it a start.

Attaching my GitHub URL here.

Feel free to ping in case of any suggestions or anything of that sort.

Happy reading!

--

--

Rohit Kumar Dubey

Cloud ☁️ & DevOps 🐳 Enthusiast | Learner 📚 | Explorer 👨🏻‍💻