Make your own Voice Assistant

Code of Voice Assistant 


Namaste Python Users,
    Here we learn about how to make Voice Assistant at Home by using python code
    In this Assistant, you can change commands as your demand.
    In the file, you get day wishes ("Good morning", "Good Evening", or "Good night") and callable functions like ("play music", "open Google", etc.) also can send mails by your account. Just speak send mail to "receiver".
    
And much more you can add to this file.

Before using it Please install Python's latest version.

then, save these codes as save as (.py) is meant to save as.

❤❤❤👀👀


import pyttsx3      #pip install pyttsx3
import webbrowser   #pip install webbrowser
import datetime
import wikipedia    #pip install wikipedia
import os
import smtplib
import speech_recognition as sr #pip intall speechRecongnition
from wikipedia import exceptions

engine = pyttsx3.init('sapi5'# Voice Engine that speak
voices = engine.getProperty('voices'# voices are 3 type [0, 1, 2]
# print(voices[1].id)
engine.setProperty('voice'voices[0].id)


def speak(audio):
    engine.say(audio)
    engine.runAndWait()

def wish(): # Wish as time.
    hour = int(datetime.datetime.now().hour)
    if hour>=0 and hour<12:
        speak("Good Morning! Piyush") #(piyush) Here you can enter your Name.

    elif hour>12 and hour<16:
        speak("Good Afternoon! Piyush")
    
    else:
        speak("Good Evening! Piyush")

    speak("I am Heera. Please tell me how may I help you"# Speak Command those start. you can change name of Heera, you can choose Name you want.
def mycmd():
    #It takes microphone input from the users and return string output

    r = sr.Recognizer()
    with sr.Microphone() as source:
        print("Listening...")
        r.pause_threshold = 1
        audio = r.listen(source)

    try:
        print("recognizing...")     # Understanding command those I Say.
        query = r.recognize_google(audiolanguage='en-in')
        print(f"Piyush said: {query}\n")

    except Exception as e# If Command not Found or Not Reachable
        # print(e)

        print("Say that again please...")

        return "none"
    return query

def sendme(tocontent):   # Send Email to Address by Speak.
    mails smtplib.SMTP('smtp.gmail.com'587)
    mails.ehlo()
    mails.starttls()
    mails.login('sender@gmail.com''sender-pswd')  # Save your Email Id, pswd.
    mails.sendme('reciveremail@gmail.com'tocontent# Reciver Email Address.
    mails.close()

if __name__ == "__main__":
    wish()
    while True:
        query = mycmd().lower() # Command will be in lower case.

        #logic for executing tasks based on query.
        if 'wikipedia' in query:  # Query you find in wikipedia.
            speak('searching Wikipedia...')
            query = query.replace("wikipedia""")
            results = wikipedia.summary(querysentences=2)
            speak("According to wikipedia")
            print(results)  # Results in Writen.
            speak(results)  # Results in Speaken.

        elif 'open youtube' in query# Command to open Browser.
            webbrowser.open("youtube.com")

        elif 'open google' in query:
            webbrowser.open("google.com")

        elif 'open stackoverflow' in query:
            webbrowser.open("stackoverflow.com")


        elif 'play music' in query# Command to open 'Query' in Disk.
            music_dir = 'C:\\Users\\Music'
            songs = os.listdir(music_dir)
            print(songs)
            os.startfile(os.path.join(music_dirsongs[0]))

        elif 'the time' in query:
            timer = datetime.datetime.now().strftime("%H:%M:%S")
            print(f"{timer}")
            speak(f"Sir, the time is {timer}")

        elif 'open code' in query:
            vspath = "" #Put the Location of the VS code.
            os.startfile(vspath)

        elif 'open camera' in query:
            camra = "C:\\Windows\\Camera\\Camera.exe"
            os.startfile(camra)

        elif 'send mail to Piyush' in query# Command to Send Mail
            try:
                speak("What should I send, Sir?")
                content = mycmd()
                to = "reciveremail.gmail.com"
                sendme(tocontent)
                speak("email has been sent! Sir.")
            except Exception as e# If Email not able to send
                print(e)
                speak("Sorry Sir, I am not able to send Your Email")



If you like this Post,
Please Comment and Share it.

If you have any doubt or query:

Visit to my Page:
Facebook : https://www.facebook.com/piyush.baghel.7583/
Link In : https://www.linkedin.com/in/piyush-baghel-4127aa220/