Python Bot
Hey guys hope you all are doing good. In today's article, we are going to see how to make our own chatbot in python to run cmnd that we use daily because command we use on daily basis is quite hectic to open it from there respective places, again and again, let's make this task automate so that when we just type suppose "hey run chrome" it will open google chrome isn't cool its cool right.
Let's get started on how to make this type of bot.
here I am assuming you have pre-installed and python and pip is set.
First, let's install the library we are going to use inside code.
pip install pyttsx3 => run this cmnd in cmd to install pyttsx3 library this library is used to speak up think which we write.
eg:- pyttsx3.speak("Hey how are you") => here .speak() is a function which can import from pyttsx3 that's why we need this library.
another library we will use os library this library is used when we want to run cmnd from python to out terminal.
eg:-os.system("start chrome")
in backend, it will run "start chrome" cmnd in your pc terminal
so here below I am putting whole code and I will also upload on Github if you want you can directly download from there you can find git repository at the end of the article.
import os
import pyttsx3
pyttsx3.speak("hello how may i help you sir")
while True:
pyttsx3.speak("Enter cmnd u want me to open")
a=input("Enter cmnd u want me to run:")
print(a)
b=a.lower()
if ("run" in b) and("chrome" in b):
os.system("start chrome")
pyttsx3.speak("opening please wait")
elif ("run" in b) and ("vlc" in b):
os.system("start vlc")
pyttsx3.speak("opening please wait")
elif ("run" in b) and ("notepad" in b):
os.system("start notepad")
pyttsx3.speak("opening please wait")
elif ("run" in b) and ("wordpad" in b):
os.system("start wordpad")
pyttsx3.speak("opening please wait")
elif ("run" in b) and ("wmplayer" in b):
os.system("start wmplayer")
pyttsx3.speak("opening please wait")
elif ("run" in b ) and ("mspaint" in b):
os.system("start mspaint")
pyttsx3.speak("opening please wait")
elif ("run" in b) and ("excel" in b):
os.system("start excel")
pyttsx3.speak("opening please wait")
elif("hey" in b) and (("quit" in b) or ("bye" in b)):
pyttsx3.speak("exiting please wait hope you like my service see u soon ")
break
Let's break down the code in parts for better understanding
import os
import pyttsx3
these two lines are used to import the library that we install with the help of pip.we import this library because at some point we need some function which is created inside this library and that function is only accessible when we import that library.
pyttsx3.speak("hello how may i help you sir")
this line we already discuss above while discussing the pyttsx3 library
while True:
here this is while loop we put it true because we need to tun program continuously because we don't know how many time user want to run something so when we set while = true it will run continuously unless we break the loop.
a=input("Enter cmnd u want me to run:")
print(a)
b=a.lower()
here we make variable with the name of a and use input function to take input from the user and after taking input from user we make that string in lowercase because we never know user might enter cmnd in uppercase so we first make all string in lowercase and save in variable called b.
if ("run" in b) and("chrome" in b):
os.system("start chrome")
pyttsx3.speak("opening please wait")
elif ("run" in b) and ("vlc" in b):
os.system("start vlc")
pyttsx3.speak("opening please wait")
elif ("run" in b) and ("notepad" in b):
os.system("start notepad")
pyttsx3.speak("opening please wait")
elif ("run" in b) and ("wordpad" in b):
os.system("start wordpad")
pyttsx3.speak("opening please wait")
elif ("run" in b) and ("wmplayer" in b):
os.system("start wmplayer")
pyttsx3.speak("opening please wait")
elif ("run" in b ) and ("mspaint" in b):
os.system("start mspaint")
pyttsx3.speak("opening please wait")
elif ("run" in b) and ("excel" in b):
os.system("start excel")
pyttsx3.speak("opening please wait")
elif("hey" in b) and (("quit" in b) or ("bye" in b)):
pyttsx3.speak("exiting please wait hope you like my service see u soon ")
break
here once we have input in b we use if else condition and operators.
os.system("start excel")
this line is used to run cmnd in terminal as we discussed before while understanding os library.
break
here at the end we use break to exit while loop and close the program.
here we come to the end of the program this is a quite easy program of you know basics of python you can do this easily and modify as per your need.
Hope you like the blog it is not a much big and complex blog but from basics, we can do great things.
here is the GIT repo.