DEV Community

Rohit Jain
Rohit Jain

Posted on

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 commands
pip install clipboard # For working with clipboard
pip install newsapi # For Getting news
pip install newsapi-python # For News api
pip install psutil # For getting compute info
pip install pyaudio # For working with audio
pip install pyautogui # For performing some GUI operation
pip install pyttsx3 # For Voice Interaction
Enter fullscreen mode Exit fullscreen mode

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 clipboard
import datetime
import os
import psutil
import pyautogui
import pyjokes
import pyttsx3
import pywhatkit
import requests
import smtplib
import speech_recognition as sr
import time as ti
import webbrowser as we
from email.message import EmailMessage
from newsapi import NewsApiClient
from secrets import senderemail, password
from time import sleep
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Then forgetting voice output we need to use pyttsx3

Initialize Pyttsx3 Engine

engine = pyttsx3.init()
voices = engine.getProperty("voices")

# For Mail voice AKA Jarvis
engine.setProperty("voice", voices[0].id)

# For Female voice AKA Friday
# engine.setProperty("voice", voices[1].id)
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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?")
Enter fullscreen mode Exit fullscreen mode

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 = "kingtechnologies2017@gmail.com"
    password = "********"
    email_list = {
        "test1": "bojole7513@httptuan.com", # 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")
Enter fullscreen mode Exit fullscreen mode

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")
Enter fullscreen mode Exit fullscreen mode

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 \nWeather is {format(temp1)}")
Enter fullscreen mode Exit fullscreen mode

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"])
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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 command
while 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"]} \nDeaths: {r["deaths"]} \nRecovered {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:\n{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()

Enter fullscreen mode Exit fullscreen mode

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. I’ll be happy to help you.

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

Thanks for reading

Top comments (0)