Python Virtual Assistant Source Code For Beginners

 Hello guys, what do you think? How difficult is it to make your own A.I. virtual assistant Google Assistant and Alexa? If you are a teenager and if you do not know about A.I. I will definitely say, "Welcome to planet EARTH".  And if you are an adult. You can check out the video:



After reading this article, you will be confident enough to make a virtual assistant. And if you face any problem, scroll down, click on responses, and comment down your problem?


But what if someone asks you about any line in the code. Well, do not worry. I will definitely explain the code in the next post as soon as I will reach 500 views on this post. So, share it now!


To make this assistant you need to install python(Python 3.8.0 recommended) and some python modules.


Link for Windows users: Download (other can download from official website python.org)


After installing python. Install the modules which are typed after import in the code. Many videos and articles already told about it.


What can it do?

  1. It can tell you the time. When you will say, "Time"
  2. Greet you according to time
  3. Search in Wikipedia. When you will say, "according to Wikipedia" after your query
  4. Send email. But it is not recommended to use because of your security.
  5. Send WhatsApp messages. Whenever you say, for example, "message to mom"
  6. Type whenever you say, "type" before you want to get typed.
  7. Close any app when you say, "close"
  8. Show you a magic trick when you say, "show magic"
  9. Open youtube whenever you say, "open youtube"
  10. Repeat your words when you say, "speak" before what you want him to speak.
  11. Search in YouTube, whenever you say, "in YouTube" after your query.
  12. Open Google whenever you say, "open google"
  13. Open Hotstar whenever you say, "open Hotstar"
  14. Search in Hotstar whenever you say, "in Hotstar after your query"
  15. Check your internet connection whenever you say, "check network"
  16. Show you Earth 3D model whenever you say, "show earth model"
  17. Check temperature whenever you say, "temperature"
  18. Ask him anything, and he will answer you according to his intelligence or if he does not know the answer. He will search it on Google.


DEMO





SOURCE CODE


import pyttsx3

import datetime

import speech_recognition as sr

import wikipedia

import webbrowser

import os

import wolframalpha

import smtplib

import pywhatkit

import rotatescreen

import time

import pyautogui

from plyer import notification


try:

    app = wolframalpha.Client("V4273X-52LQ53V3K9")


except Exception:

    print('Sorry, some features are not working')


myName = 'Naariyal Annnaa'


engine = pyttsx3.init('sapi5')

voices = engine.getProperty('voices')

engine.setProperty('voice', voices[0].id)



def speak(audio):

    engine.say(audio)

    engine.runAndWait()

    pass



def sendEmail(tocontent):

    server = smtplib.SMTP('smntp.gmail.com', 587)

    server.ehlo()

    server.starttls()

    server.login('chahatgravit@gmail.com', 'one1two2three3four4')

    server.sendmail('chahatgravit@gmail.com', to, content)

    server.close()



def wishme():

    hour = datetime.datetime.now().hour

    min = datetime.datetime.now().minute

    date = datetime.datetime.now().date

    month = datetime.datetime.now().month

    sec = datetime.datetime.now().second


    if hour >= 0 and hour <= 12:

        speak('Good Morning, user')


    elif hour > 12 and hour < 18:

        speak('Good Afternoon, user')


    else:

        speak('Good Evening, user')


    speak(f'Time {hour}, bajakar {min} aur. {sec} second hua hai')

    speak('Main aapki kaisay madad kar sakta hoon?')



def hearMe():

    r = sr.Recognizer()

    with sr.Microphone() as source:

        print('Listening...')

        r.pause_threshold = 1

        audio = r.listen(source)

    try:

        print('Recognizing...')

        query = r.recognize_google(audio, language='en-in')

        print(f'You said:{query}\n')


    except Exception as e:

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

        return 'NONE'

    return query

    speak('PLEASE GIVE ANY COMMAND')



pass


if __name__ == "__main__":

    wishme()

    while True:

        hour = datetime.datetime.now().hour

        min = datetime.datetime.now().minute

        date = datetime.datetime.now().date

        month = datetime.datetime.now().month

        sec = datetime.datetime.now().second

        query = hearMe().lower()

        papa = 'papa email'

        mom = 'mom email'


        if 'wikipedia' in query:

            speak('Searching Wikipedia....')

            query = query.replace('wikipedia', '')

            results = wikipedia.summary(query, sentences=2)

            speak('According to wikipedia')

            print(results)

            speak(results)


        elif 'time' in query:

            sec = datetime.datetime.now().second

            speak(f'{hour} hour, {min} minute and {sec} second')


        elif 'email to mom' in query:

            try:

                speak('mujhe kya kahana chahiye?')

                content = hearMe()

                to = ('mom email address')

                sendEmail(to, content)

                speak('Email bhejdiya')

            except Exception as e:

                print(e)

                speak('Email nahi bheja gaya')


        elif 'message to papa' in query:

            hour = datetime.datetime.now().hour

            min = datetime.datetime.now().minute

            try:

                speak('what should I say?')

                content = hearMe()

                pywhatkit.sendwhatmsg('+91 papa whatsapp no.', content, hour, min + 2)

                speak('Done')

            except Exception as e:

                print(e)

                speak('sorry')


# Anna sending messages on whatsapp

        elif 'message to mom' in query:

            hour = datetime.datetime.now().hour

            min = datetime.datetime.now().minute

            try:

                speak('what should I say?')

                content = hearMe()

                pywhatkit.sendwhatmsg('+91 mom whatsapp no.', content, hour, min + 2)

                speak('Done')

            except Exception as e:

                print(e)

                speak('sorry')


        elif 'message to bua' in query:

            hour = datetime.datetime.now().hour

            min = datetime.datetime.now().minute

            try:

                speak('what should I say?')

                content = hearMe()

                pywhatkit.sendwhatmsg('+91 bua whatsapp no.', content, hour, min + 2)

                speak('Done')

            except Exception as e:

                print(e)

                speak('sorry')


        elif 'message to mosi' in query:

            hour = datetime.datetime.now().hour

            min = datetime.datetime.now().minute

            try:

                speak('what should I say?')

                content = hearMe()

                pywhatkit.sendwhatmsg('+91 mosi whatsapp no.', content, hour, min + 2)

                speak('Done')

            except Exception as e:

                print(e)

                speak('sorry')


# Anna entering keys automatically

        elif 'pause' in query:

            pyautogui.press('space')


        elif 'cut' in query:

            pyautogui.press('backspace')


        elif 'enter' in query:

            pyautogui.press('enter')


# Typing automatically

        elif 'type' in query:

            query = query.replace('type', '')

            pyautogui.write(query)


# Automate shortcut keys

        elif 'close' in query:

            speak('ok')

            pyautogui.hotkey('alt', 'f4')


# Anna showing magic

        elif 'show magic' in query:

            screen = rotatescreen.get_primary_display()

            for i in range(5):

                time.sleep(1)

                screen.rotate_to(i*90 % 360)


        elif 'open youtube' in query:

            speak('Opening YouTube')

            webbrowser.open('youtube.com')


        elif 'speak' in query:

            query = query.replace('speak', '')

            speak(query)


        elif 'in youtube' in query:

            query = query.replace('in youtube', '')

            speak(f'searching{query} in youtube..')

            pywhatkit.playonyt(query)


        elif 'open google' in query:

            speak('Opening Google')

            webbrowser.open('google.com')


        elif 'open hotstar' in query:

            speak('Opening Hotstar')

            webbrowser.open('https://www.hotstar.com/in/disneyplus')


        elif 'in hotstar' in query:

            query = query.replace('in hotstar', '')

            speak(f'starting {query} in hotstar...')

            webbrowser.open('https://www.hotstar.com/search?q='+query)


        elif 'bye' in query:

            speak('Deactivating Annaa')

            exit()


        elif 'network' in query:

            speak('Checking network...')

            webbrowser.open(

                'https://www.jio.com/dashboard/?root=login#loginOTP')


        elif 'hello' in query:

            speak('Hey there!')


        elif 'who are you' in query:

            speak('Myself, Nariyal Anna. You can call me Anna. I am an A.I. or Aartificial Intelligence programe developed by Garvit Biloniya.')


        elif 'how are you' in query:

            speak('I am fine. What about you?')


        elif 'I am also fine.' in query:

            speak('good to hear this.')


        elif 'show earth model' in query:

            speak('showing earth')

            webbrowser.open('https://earth.google.com/web/')


        elif 'temperature' in query:

            try:

                res = app.query(query)

                print(next(res.results).text)

                speak(next(res.results).text)

            except:

                print('Internet connection error!')


        elif 'i am not well' in query:

            speak('sorry to hear this')


        elif 'NONE' in query:

            print('PLEASE GIVE ANY COMMAND')


        else:

            try:

                res = app.query(query)

                print(next(res.results).text)

                speak(next(res.results).text)

            except:

                speak('out of my range of intelligence. searching in webbrowser')

                webbrowser.open('https://www.google.com/search?q='+query)