0% found this document useful (0 votes)
7 views3 pages

Python codes to deign a chatbot

The document is a Python script for a virtual assistant named Jarvis that can perform various tasks such as sending WhatsApp messages, opening websites, playing music, and providing information from Wikipedia. It uses libraries like pyttsx3 for text-to-speech, pytube for downloading YouTube videos, and speech_recognition for voice commands. The script includes functions for greeting the user, taking voice commands, and executing specific queries based on user input.

Uploaded by

vishnudevadiga24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

Python codes to deign a chatbot

The document is a Python script for a virtual assistant named Jarvis that can perform various tasks such as sending WhatsApp messages, opening websites, playing music, and providing information from Wikipedia. It uses libraries like pyttsx3 for text-to-speech, pytube for downloading YouTube videos, and speech_recognition for voice commands. The script includes functions for greeting the user, taking voice commands, and executing specific queries based on user input.

Uploaded by

vishnudevadiga24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

import math

import pytube
import os
import easygui
import random
import mymodule
import pyttsx3
import datetime
import webbrowser
import wikipedia
import speech_recognition as sr
import pywhatkit
import turtle
import pyjokes
# import modname

engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
#print(voices[1].id)
engine.setProperty('voice', voices[0].id)
def speak(audio):
engine.say(audio)
engine.runAndWait()

# speak("What is yout name:\n")


name = "Vishnuprasad"
def wishMe():
hour = int(datetime.datetime.now().hour)
if(hour>=0 and hour<12):
speak("Good Morning, ")
elif(hour>=12 and hour<18):
speak("Good Afternoon ")
else:
speak("Good Evening")
speak("I am Jarvis. Please tell me how may i help you")

def sendQuery():
if 'whatsapp' in query:
speak("To whom")
num = takeCommand().lower()
num = mymodule.contacts[num].strip()
speak("What is the message")
msg = takeCommand()
if "now" in query:
pywhatkit.sendwhatmsg_instantly(num, msg, 10, True)
else:
speak("At what time")
speak("Hour")
hr = int(takeCommand())
speak("Minute")
mn = int(takeCommand())
pywhatkit.sendwhatmsg(num, msg, hr, mn, 10, True)

def openQuery(query):
if "open youtube" in query:
webbrowser.open("youtube.com", 2)
elif "open google" in query:
webbrowser.open("google.com", 2)
elif "open w3schools python" in query:
webbrowser.open("https://www.w3schools.com/python/default.asp")
elif 'open whatsapp' in query:
webbrowser.open("web.whatsapp.com", 2)
elif "open stack overflow" in query:
webbrowser.open("stackoverflow.com", 2)
elif "open telegram" in query:
os.startfile("C:\\Users\\Hp\\AppData\\Roaming\\Telegram Desktop\\
Telegram.exe")
else:
content = query.replace("open", "")
pywhatkit.playonyt(content)

def takeCommand():
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
r.energy_threshold = 200
r.pause_threashold = 1 # To adjust gap between speaking
audio = r.listen(source)
try:
print("Recognising....")
query = r.recognize_google(audio, language = 'en-in')
print(f"User said: {query}\n")
except Exception as e:

#print(e)

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


return "None"
return query

if __name__ == "__main__":
wishMe()
while 1:
query = takeCommand().lower()
# query = 'whatsapp'
try:
if "wikipedia" in query:
query = query.replace("wikipedia", "")
result = wikipedia.summary(query, sentences=2)
speak("According to wikipedia")
speak(result)
elif 'send' in query:
sendQuery()
elif "open" in query:
openQuery(query)
elif "whatsapp" in query:
print("THis")
speak("To whom")
try:
num = mymodule.contacts[takeCommand().lower()].strip()
except Exception:
speak("Sorry it is not in the contacts")
# num = mymodule.contacts['vishnu'.strip()]
speak("What is the message")
msg = takeCommand()
# hr = int(datetime.datetime.now().hour)
# mi = int(datetime.datetime.now().minute) + 1
# pywhatkit.sendwhatmsg(num, msg, hr, mi, 10, True, 5)
pywhatkit.sendwhatmsg_instantly(num, msg, 10, True)
# pywhatkit.
#
pywhatkit.sendwhatmsg_to_group_instantly("Fd16ka2QtoI6BVP35gcsol", "Hi", 10, True)
elif "exit" in query:
break
elif "play music" in query:
music_dir = "C:\\New folder (2)\\LOST.DIR"
songs = os.listdir(music_dir)
os.startfile(os.path.join(music_dir, songs[random.randint(0,
len(songs))]))
elif "the time" in query:
strTime = datetime.datetime.now().strftime("%H:%M:%S")
speak(strTime)

elif "shutdown" in query:


pywhatkit.shutdown(50)
if "cancel shutdown" in query:
pywhatkit.cancel_shutdown()
elif "search" in query:
query = query.replace("search", "")
pywhatkit.search(query)
elif 'restart' in query:
os.system("shutdown /r /t 1")
elif "download youtube videos" in query:
url = input("Enter the url:")
path = "C:\\New folder (2)\\Videos"

pytube.YouTube(url).streams.get_by_resolution("360p").download(path)
except Exception:
speak("We can't perform that")

You might also like