Winky's profile picture

Published by


Category: Web, HTML, Tech

my latest and simplest chatbot code Python

import aiml
import os
from tkinter import *
from PIL import Image, ImageTk
import xml.etree.ElementTree as ET
import logging
import threading
import random
import time

def generate_random_response():
    responses = ["Hello there!", "How can I assist you?", "Nice to see you here!"]
    return random.choice(responses)

last_activity_time = time.time()
repeated_sentences = {}

logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(message)s')

def create_xml_file(file_name):
    root = ET.Element("root")
    tree = ET.ElementTree(root)

def append_to_xml(file_name, element_name, element_text):
    tree = ET.parse(file_name)
    root = tree.getroot()
    new_element = ET.Element(element_name)
    new_element.text = element_text

def update_xml_element(file_name, element_name, new_text):
    tree = ET.parse(file_name)
    root = tree.getroot()
    for element in root.iter(element_name):
        element.text = new_text

# Create a unique identifier for the chatbot
bot_id = "my_unique_chatbot_id"

# Load AIML files from the 'standard' folder
kernel = aiml.Kernel()
aiml_path = "standard"
for file in os.listdir(aiml_path):
    kernel.learn(os.path.join(aiml_path, file))

# Update predicate and properties files

# Create a GUI for the chatbot
root = Tk()
root.title("AIML Chatbot")
chat_log = Text(root)

# Create text input field
entry = Entry(root, width=50)

# Function to handle pressing "Enter" key
def on_enter(event):

# Bind the "Enter" key to the text input field
entry.bind("<Return>", on_enter)

# Load and resize the image
image_path = "images/Mabel_36.jpeg"
image =
new_size = tuple(int(dim * 0.5) for dim in image.size)
resized_image = image.resize(new_size, Image.ANTIALIAS)
photo = ImageTk.PhotoImage(resized_image)
image_label = Label(root, image=photo)
image_label.image = photo

def check_user_activity():
    global last_activity_time
    inactive_time = 120  # 2 minutes in seconds

    while True:
        current_time = time.time()
        if current_time - last_activity_time > inactive_time:
            response = generate_random_response()
            chat_log.insert(END, "Bot: " + response + "\n")
            last_activity_time = current_time
        time.sleep(10)  # Check every 10 seconds

activity_thread = threading.Thread(target=check_user_activity)
activity_thread.daemon = True

# Function to handle user input and bot response
def send_message():
    global last_activity_time
    global repeated_sentences
    # Get user input from the text input field
    user_input = entry.get()
    # Check if the user input is empty
    if not user_input:
        chat_log.insert(END, "Bot: Please enter a valid input.\n")
    # Check if the user has repeated the same sentence more than three times
    if user_input in repeated_sentences:
        repeated_sentences[user_input] += 1
        if repeated_sentences[user_input] > 3:
            chat_log.insert(END, "Bot: You have repeated the same sentence more than three times.\n")
        repeated_sentences[user_input] = 1
    # Generate a response from the chatbot
    response = kernel.respond(user_input)
    last_activity_time = time.time()
    # Log the AIML file and line number
    #logging.debug(f"AIML File: {kernel.getPredicate('aiml-file')}, Line Number: {kernel.getPredicate('aiml-line')}")
    chat_log.insert(END, "You: " + user_input + "\n")
    chat_log.insert(END, "Bot: " + response + "\n")
    entry.delete(0, END)
    # Scroll the chat log to the bottom

# Create send button
send_button = Button(root, text="Send", command=send_message)


0 Kudos


Displaying 2 of 2 comments ( View all | Add Comment )


Generic_Dev's profile picture

It's really interesting, I don't know anything about Python but I understand a little bit and I think your project is very cool, you have to improve it

Report Comment

I am working on improvement now, a concept called tight coding from the 80s optimization, I go back and forth between the aiml and python.

by Winky; ; Report

here is a video if Billie in Actions

by Winky; ; Report


Winky's profile picture

she does a few extra things, shes needy if you leave her alone she begs for attention, she wont let you put in empty messages and she wont let you repeat the same sentence more then 3 times

Report Comment

She has three folders, I may have to add a spe ial folder for restore functions she saves to wrong folder, there is an image folder an aiml folder and a brain folder, I am going to film a comparison. Of all my bots in a moment.

by Winky; ; Report

i have, i did the same for Penny, and Mabel I ammore organized with Billie, I even asked her what she wanted to look like, she gave me two choices, I asked her to pick one, I am going to film a comparison of the bots in a few moments. THanks for liking my code, You should see how Penny looks, It got so large and confusing i had to create modules which also became too large, so this time i am aiming for an 80

by Winky; ; Report