Codementor Events

Simple Voice-Enabled chat-bot in Python.

Published Jun 29, 2018Last updated Oct 30, 2018
Simple Voice-Enabled chat-bot in Python.

I was thinking if there was a way to code a chat bot using just pure Python. Event though this method may not be as "smart" or "fast" as AIML or ChatScript, it is still very easy. This helps beginners understand what a chatbot is and how it works.

First it is important to understand the difference between a ChatBot and a AI personal assistant.
A chat bot is a program that can converse with a human being in a natural way. But that doesn't mean it's smart. A chat bot requires a preset library of answers for a set of question.

An AI Assistant collects information like your browsing data and your interests and makes your experience surfing the web easier. It helps you to do certain tasks like shop easily. One example is the Amazon Assistant, which helps you by giving suggestions based on your interests.

So, here's a tutorial on how to make a voice-enable chat bot using pure Python.
In this tutorial, we are going to use Python 3.
I suggest using PyCharm for coding in Python. The community version is free.
You can still use IDLE or other IDEs.

  1. First install all of the required modules using pip.
    Before we proceed, we will need to install some eternal modules.
pip install pyttsx3
pip install wikipedia
pip install SpeechRecognition
pip install pygame
pip install pyown

Since we are using microphone interface, we will need to install pyaudio.

pip install pyaudio

If you do not know how to use pip, follow this tutorial
https://www.youtube.com/watch?v=zbNczZLP1tA

We are going to use pyttsx3 for converting text to speech, Wikipedia to get data from the web, pyowm to get weather data, and speech_recognition for converting speech to text using the google speech recognition engine.

To use pyowm, you will need an API key. You can get an API key for free at
https://home.openweathermap.org/users/sign_up
2) Import all of the required modules.
We are going to use the random module to generate random replies from our list of replies.
Import the following modules onto a new Python file.

import random
import datetime
import webbrowser
import pyttsx3
import wikipedia
from pygame import mixer
import speech_recognition as sr
  1. Set up and calibrate the text to speech engine.
    Now we need to set the voice rate, engine, etc.
engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
volume = engine.getProperty('volume')
engine.setProperty('volume', 10.0)
rate = engine.getProperty('rate')
engine.setProperty('rate', rate - 25)
  1. Create lists with your preset questions answers.
    The more the lists the better. But too many lists can slowdown the chat bot.
greetings = ['hey there', 'hello', 'hi', 'Hai', 'hey!', 'hey']
question = ['How are you?', 'How are you doing?']
responses = ['Okay', "I'm fine"]
var1 = ['who made you', 'who created you']
var2 = ['I_was_created_by_Edward_right_in_his_computer.', 'Edward', 'Some_guy_whom_i_never_got_to_know.']
var3 = ['what time is it', 'what is the time', 'time']
var4 = ['who are you', 'what is you name']
cmd1 = ['open browser', 'open google']
cmd2 = ['play music', 'play songs', 'play a song', 'open music player']
cmd3 = ['tell a joke', 'tell me a joke', 'say something funny', 'tell something funny']
jokes = ['Can a kangaroo jump higher than a house? Of course, a house doesn’t jump at all.', 'My dog used to chase people on a bike a lot. It got so bad, finally I had to take his bike away.', 'Doctor: Im sorry but you suffer from a terminal illness and have only 10 to live.Patient: What do you mean, 10? 10 what? Months? Weeks?!"Doctor: Nine.']
cmd4 = ['open youtube', 'i want to watch a video']
cmd5 = ['tell me the weather', 'weather', 'what about the weather']
cmd6 = ['exit', 'close', 'goodbye', 'nothing']
cmd7 = ['what is your color', 'what is your colour', 'your color', 'your color?']
colrep = ['Right now its rainbow', 'Right now its transparent', 'Right now its non chromatic']
cmd8 = ['what is you favourite colour', 'what is your favourite color']
cmd9 = ['thank you']
repfr9 = ['youre welcome', 'glad i could help you']
  1. Calibrate the speech recognition engine.
    We are going to use the google speech recognition engine. Set the engine details.
    Also use the while loop to restart the engine in case of any errors.
while True:
    now = datetime.datetime.now()
    r = sr.Recognizer()
    with sr.Microphone() as source:
        print("Tell me something:")
        audio = r.listen(source)
        try:
            print("You said:- " + r.recognize_google(audio))
        except sr.UnknownValueError:
            print("Could not understand audio")
            engine.say('I didnt get that. Rerun the code')
            engine.runAndWait()
            
            
  1. Use a loop code with random and Wikipedia to finish the code.
    We are going to use the above mentioned modules in a loop code to create the main functional code.
if r.recognize_google(audio) in greetings:
       random_greeting = random.choice(greetings)
       print(random_greeting)
       engine.say(random_greeting)
       engine.runAndWait()
   elif r.recognize_google(audio) in question:
       engine.say('I am fine')
       engine.runAndWait()
       print('I am fine')
   elif r.recognize_google(audio) in var1:
       engine.say('I was made by edward')
       engine.runAndWait()
       reply = random.choice(var2)
       print(reply)
   elif r.recognize_google(audio) in cmd9:
       print(random.choice(repfr9))
       engine.say(random.choice(repfr9))
       engine.runAndWait()
   elif r.recognize_google(audio) in cmd7:
       print(random.choice(colrep))
       engine.say(random.choice(colrep))
       engine.runAndWait()
       print('It keeps changing every micro second')
       engine.say('It keeps changing every micro second')
       engine.runAndWait()
   elif r.recognize_google(audio) in cmd8:
       print(random.choice(colrep))
       engine.say(random.choice(colrep))
       engine.runAndWait()
       print('It keeps changing every micro second')
       engine.say('It keeps changing every micro second')
       engine.runAndWait()
   elif r.recognize_google(audio) in cmd2:
       mixer.init()
       mixer.music.load("C:\\Users\Edward Zion SAJI\Downloads\Mighty_God_-_Martin__Colleen_Rebeiro.55145718.wav")
       mixer.music.play()
   elif r.recognize_google(audio) in var4:
       engine.say('I am edza your personal AI assistant')
       engine.runAndWait()
   elif r.recognize_google(audio) in cmd4:
       webbrowser.open('www.youtube.com')
   elif r.recognize_google(audio) in cmd6:
       print('see you later')
       engine.say('see you later')
       engine.runAndWait()
       exit()
   elif r.recognize_google(audio) in cmd5:
       owm = pyowm.OWM('YOUR_API_KEY')
       observation = owm.weather_at_place('Bangalore, IN')
       observation_list = owm.weather_around_coords(12.972442, 77.580643)
       w = observation.get_weather()
       w.get_wind()
       w.get_humidity()
       w.get_temperature('celsius')
       print(w)
       print(w.get_wind())
       print(w.get_humidity())
       print(w.get_temperature('celsius'))
       engine.say(w.get_wind())
       engine.runAndWait()
       engine.say('humidity')
       engine.runAndWait()
       engine.say(w.get_humidity())
       engine.runAndWait()
       engine.say('temperature')
       engine.runAndWait()
       engine.say(w.get_temperature('celsius'))
       engine.runAndWait()
   elif r.recognize_google(audio) in var3:

       print("Current date and time : ")
       print(now.strftime("The time is %H:%M"))
       engine.say(now.strftime("The time is %H:%M"))
       engine.runAndWait()
   elif r.recognize_google(audio) in cmd1:
       webbrowser.open('www.google.com')
   elif r.recognize_google(audio) in cmd3:
       jokrep = random.choice(jokes)
       engine.say(jokrep)
       engine.runAndWait()
   else:
       engine.say("please wait")
       engine.runAndWait()
       print(wikipedia.summary(r.recognize_google(audio)))
       engine.say(wikipedia.summary(r.recognize_google(audio)))
       engine.runAndWait()
       userInput3 = input("or else search in google")
       webbrowser.open_new('www.google.com/search?q=' + userInput3)
  1. Customize the code as you wish and create the final code.
    Now join all the pieces of code. You should have something like this.
import random
import datetime
import webbrowser
import pyttsx3
import wikipedia
from pygame import mixer
import speech_recognition as sr
from speech_recognition.__main__ import r, audio

engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
volume = engine.getProperty('volume')
engine.setProperty('volume', 10.0)
rate = engine.getProperty('rate')

engine.setProperty('rate', rate - 25)

greetings = ['hey there', 'hello', 'hi', 'Hai', 'hey!', 'hey']
question = ['How are you?', 'How are you doing?']
responses = ['Okay', "I'm fine"]
var1 = ['who made you', 'who created you']
var2 = ['I_was_created_by_Edward_right_in_his_computer.', 'Edward', 'Some_guy_whom_i_never_got_to_know.']
var3 = ['what time is it', 'what is the time', 'time']
var4 = ['who are you', 'what is you name']
cmd1 = ['open browser', 'open google']
cmd2 = ['play music', 'play songs', 'play a song', 'open music player']
cmd3 = ['tell a joke', 'tell me a joke', 'say something funny', 'tell something funny']
jokes = ['Can a kangaroo jump higher than a house? Of course, a house doesn’t jump at all.', 'My dog used to chase people on a bike a lot. It got so bad, finally I had to take his bike away.', 'Doctor: Im sorry but you suffer from a terminal illness and have only 10 to live.Patient: What do you mean, 10? 10 what? Months? Weeks?!"Doctor: Nine.']
cmd4 = ['open youtube', 'i want to watch a video']
cmd5 = ['tell me the weather', 'weather', 'what about the weather']
cmd6 = ['exit', 'close', 'goodbye', 'nothing']
cmd7 = ['what is your color', 'what is your colour', 'your color', 'your color?']
colrep = ['Right now its rainbow', 'Right now its transparent', 'Right now its non chromatic']
cmd8 = ['what is you favourite colour', 'what is your favourite color']
cmd9 = ['thank you']

repfr9 = ['youre welcome', 'glad i could help you']

while True:
    now = datetime.datetime.now()
    r = sr.Recognizer()
    with sr.Microphone() as source:
        print("Tell me something:")
        audio = r.listen(source)
        try:
            print("You said:- " + r.recognize_google(audio))
        except sr.UnknownValueError:
            print("Could not understand audio")
            engine.say('I didnt get that. Rerun the code')

            engine.runAndWait()
    if r.recognize_google(audio) in greetings:
        random_greeting = random.choice(greetings)
        print(random_greeting)
        engine.say(random_greeting)
        engine.runAndWait()
    elif r.recognize_google(audio) in question:
        engine.say('I am fine')
        engine.runAndWait()
        print('I am fine')
    elif r.recognize_google(audio) in var1:
        engine.say('I was made by edward')
        engine.runAndWait()
        reply = random.choice(var2)
        print(reply)
    elif r.recognize_google(audio) in cmd9:
        print(random.choice(repfr9))
        engine.say(random.choice(repfr9))
        engine.runAndWait()
    elif r.recognize_google(audio) in cmd7:
        print(random.choice(colrep))
        engine.say(random.choice(colrep))
        engine.runAndWait()
        print('It keeps changing every micro second')
        engine.say('It keeps changing every micro second')
        engine.runAndWait()
    elif r.recognize_google(audio) in cmd8:
        print(random.choice(colrep))
        engine.say(random.choice(colrep))
        engine.runAndWait()
        print('It keeps changing every micro second')
        engine.say('It keeps changing every micro second')
        engine.runAndWait()
    elif r.recognize_google(audio) in cmd2:
        mixer.init()
        mixer.music.load("song.wav")
        mixer.music.play()
    elif r.recognize_google(audio) in var4:
        engine.say('I am a bot, silly')
        engine.runAndWait()
    elif r.recognize_google(audio) in cmd4:
        webbrowser.open('www.youtube.com')
    elif r.recognize_google(audio) in cmd6:
        print('see you later')
        engine.say('see you later')
        engine.runAndWait()
        exit()
    elif r.recognize_google(audio) in cmd5:
        owm = pyowm.OWM('YOUR_API_KEY')
        observation = owm.weather_at_place('Bangalore, IN')
        observation_list = owm.weather_around_coords(12.972442, 77.580643)
        w = observation.get_weather()
        w.get_wind()
        w.get_humidity()
        w.get_temperature('celsius')
        print(w)
        print(w.get_wind())
        print(w.get_humidity())
        print(w.get_temperature('celsius'))
        engine.say(w.get_wind())
        engine.runAndWait()
        engine.say('humidity')
        engine.runAndWait()
        engine.say(w.get_humidity())
        engine.runAndWait()
        engine.say('temperature')
        engine.runAndWait()
        engine.say(w.get_temperature('celsius'))
        engine.runAndWait()
    elif r.recognize_google(audio) in var3:

        print("Current date and time : ")
        print(now.strftime("The time is %H:%M"))
        engine.say(now.strftime("The time is %H:%M"))
        engine.runAndWait()
    elif r.recognize_google(audio) in cmd1:
        webbrowser.open('www.google.com')
    elif r.recognize_google(audio) in cmd3:
        jokrep = random.choice(jokes)
        engine.say(jokrep)
        engine.runAndWait()
    else:
        engine.say("please wait")
        engine.runAndWait()
        print(wikipedia.summary(r.recognize_google(audio)))
        engine.say(wikipedia.summary(r.recognize_google(audio)))
        engine.runAndWait()
        userInput3 = input("or else search in google")
        webbrowser.open_new('www.google.com/search?q=' + userInput3)

In case you get a win32 related error run ```
pip install pypiwin32

If you have more questions, leave a comment.


Thats all! Now test out your code and have fun!
Happy Coding!!!
Discover and read more posts from Edward Zion Saji
get started
post commentsBe the first to share your opinion
isaac danjuma
3 years ago

I want to know if it’s possible to change the voice and accent spoken with, considering the libraries used in this code.

Shivam Thakur
3 years ago

Pl solve my Error Plz

Shivam Thakur
3 years ago

I have run the code Also Installed All Required Modules. An Extra Module named Sound device. But i couldn’t Hear any voice. Just it is showing text i want it to speak and Recognize.

Show more replies