Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
May 19, 2021 02:20 pm GMT

How to Create a Virtual Assistant Using Python


Hey Guys Today let's create something interesting, a virtual assistant. A computer is a device that helps everyone to achieve things fast. It does things faster than humans and it did not get bored whenever it performs a repetitive task. We can automate our repetitive tasks using a computer so tasks can perform fast and we just need to convey to a computer either by giving a voice command or by typing the command.

First, we need to think about the task that we want the assistant to perform and how it can automate it like greet us, whenever we want to execute something, tell us the date, time, news, weather, jokes, send emails, send messages, open something in a computer, tell us internal information about computers like cup usage and much more. We can make Functions or write simple codes to make it work.

For making these tasks in python we need to install a couple of packages. You can use the below commands for that.

Open terminal with administration privileges execute these commands either one by one or write the package name together.

pip install SpeechRecognition # for Voice commandspip install clipboard # For working with clipboardpip install newsapi # For Getting newspip install newsapi-python # For News apipip install psutil # For getting compute infopip install pyaudio # For working with audiopip install pyautogui # For performing some GUI operationpip install pyttsx3 # For Voice Interaction

You can use other packages as well and different functionalities with these. Let's jump into the coding

Initially, we need to import the packages as in every Python Program

import clipboardimport datetimeimport osimport psutilimport pyautoguiimport pyjokesimport pyttsx3import pywhatkitimport requestsimport smtplibimport speech_recognition as srimport time as tiimport webbrowser as wefrom email.message import EmailMessagefrom newsapi import NewsApiClientfrom secrets import senderemail, passwordfrom time import sleep

Now I would like to set the variables for user name and assistant name so it can be changed easily if we want.

user = "Rohit"assistant= "Jarvis" # Iron man Fan

Then forgetting voice output we need to use pyttsx3

Initialize Pyttsx3 Engine

engine = pyttsx3.init()voices = engine.getProperty("voices")# For Mail voice AKA Jarvisengine.setProperty("voice", voices[0].id)# For Female voice AKA Friday# engine.setProperty("voice", voices[1].id)

These are the voices of Microsoft David (Male) and Microsoft Zira (Female) Voice for the windows narrator program. You can install other voices as well but I find that a bit laggy and everything is not covered in them.

Input/Output Functions

def output(audio):    # print(audio) # For printing out the output    engine.say(audio)    engine.runAndWait()# For getting the device index you can execute this code So if you want to change the device you can do that.# for index, name in enumerate(sr.Microphone.list_microphone_names()):#     print("Microphone with name \"{1}\" found for `Microphone(device_index={0})`".format(#         index, name))def inputCommand():    # query = input() # For getting input from CLI    r = sr.Recognizer()    query = ""    with sr.Microphone(device_index=2) as source:        print("Listening...")        r.pause_threshold = 1        try:            query = r.recognize_google(r.listen(source), language="en-IN")        except Exception as e:            output("Say that again Please...")    return query

Up to this, we are just setting the things for an assistant. Now we will make functions for our tasks

Greet Function

def greet():    hour = datetime.datetime.now().hour    if (hour >= 6) and (hour < 12):        output(f"Good Morning {user}")    elif (hour >= 12) and (hour < 18):        output(f"Good afternoon {user}")    elif (hour >= 18) and (hour < 21):        output(f"Good Evening {user}")    output("How may I assist you?")

Email Function

# You can also use a secret file and store these variables there as I am doing or If you not going to show this code to anyone that you can it here as well.def sendEmail():    senderemail = "[email protected]"    password = "********"    email_list = {        "test1": "[email protected]", # Temporary Email        "test2": "<Your Friends, family or business email here>"    }    try:        email = EmailMessage()        output("To whom you want to send the mail?")        name = inputCommand().lower()        email['To'] = email_list[name]        output("What is the subject of the mail?")        email["Subject"] = inputCommand()        email['From'] = senderemail        output("What should i Say?")        email.set_content(inputCommand())        s = smtplib.SMTP("smtp.gmail.com", 587)        s.starttls()        s.login(senderemail, password)        s.send_message(email)        s.close()        output("Email has sent")    except Exception as e:        print(e)        output("Unable to send the Email")

Send Whatsapp Message Function

It is utilizing the web browser package

def sendWhatMsg():    user_name = {        'Jarvis': '+91 95299 16394'    }    try:        output("To whom you want to send the message?")        name = inputCommand()        output("What is the message")        we.open("https://web.whatsapp.com/send?phone=" +                user_name[name]+'&text='+inputCommand())        sleep(6)        pyautogui.press('enter')        output("Message sent")    except Exception as e:        print(e)        output("Unable to send the Message")

Weather Function

For Weather, we can use OpenWeatherMap API

def weather():    city = "jaipur"    res = requests.get(        f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid=16f0afad2fd9e18b7aee9582e8ce650b&units=metric").json()    temp1 = res["weather"][0]["description"]    temp2 = res["main"]["temp"]    output(        f"Temperature is {format(temp2)} degree Celsius 
Weather is {format(temp1)}")

News Function

For News, we can use the News API package

def news():    newsapi = NewsApiClient(api_key='5840b303fbf949c9985f0e1016fc1155')    output("What topic you need the news about")    topic = inputCommand()    data = newsapi.get_top_headlines(        q=topic, language="en", page_size=5)    newsData = data["articles"]    for y in newsData:        output(y["description"])

Idea Function

We can use file handling for that it's quite reliable storage but we need to implement it carefully

def idea():    output("What is your idea?")    data = inputCommand().title()    output("You Said me to remember this idea: " + data)    with open("data.txt", "a", encoding="utf-8") as r:        print(data, file=r)

Now the last part i.e. Function calling based on Command and some inline execution

First of all the greet function

greet()# Then with while true we can make it a infinite loop on commandwhile True:    # Getting input from the user    query = inputCommand().lower()    # According to the query if query have respective word we will execute the respective command    if ("time" in query):        output("Current time is " +               datetime.datetime.now().strftime("%I:%M"))    elif ('date' in query):        output("Current date is " + str(datetime.datetime.now().day)               + " " + str(datetime.datetime.now().month)               + " " + str(datetime.datetime.now().year))    elif ('email' in query):        sendEmail()    elif ('message' in query):        print("Sending...")        sendWhatMsg()    elif ("search" in query):        output("what you want to search?")        we.open("https://www.google.com/search?q="+inputCommand())    elif ("youtube" in query):        output("What you want to search on Youtube?")        pywhatkit.playonyt(inputCommand())    elif ('weather' in query):        weather()    elif ("news" in query):        news()    elif ("read" in query):        output(clipboard.paste())    elif ("covid" in query):        r = requests.get(            'https://coronavirus-19-api.herokuapp.com/all').json()        output(            f'Confirmed Cases: {r["cases"]} 
Deaths: {r["deaths"]}
Recovered {r["recovered"]}') elif ("workspace" in query): output("Which workspace you want to work on") os.startfile("D:\\Work Spaces\\" + inputCommand()+".code-workspace") elif ("joke" in query): output(pyjokes.get_joke()) elif ("idea" in query): idea() elif ("do you know" in query): ideas = open("data.txt", "r") output(f"You said me to remember these ideas:
{ideas.read()}") elif ("screenshot" in query): pyautogui.screenshot(str(ti.time()) + ".png").show() elif "cpu" in query: output(f"Cpu is at {str(psutil.cpu_percent())}") elif "offline" in query: hour = datetime.datetime.now().hour if (hour >= 21) and (hour < 6): output(f"Good Night {user}! Have a nice Sleep") else: output(f"By {user}") quit()

Based on different input we are executing different task is this way our assistant can have so much functionality we can utilize
This is all For this blog, as I mentioned earlier you can customize it according to you and you can check out the GitHub repo for complete code

Let me know if you have any questions or queries. Ill be happy to help you.

Like share, and follow. You can also check my other profiles on King Technologies

Thanks for reading


Original Link: https://dev.to/rohit19060/how-to-create-a-virtual-assistant-using-python-4h7l

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To