Archive

Posts Tagged ‘Omegle’

DIY Omegle Chat Bot!

September 16, 2009 15 comments

I have been chatting on  Omegle for quite some time now and I must say that i find it rather addictive. During one conversation, i encountered a bot and needless to say, i was hooked on to the convo ! I wanted to make one for myself badly somehow 🙂

A quick search later revealed that most of the chatter bots were based on the Artificial Intelligence Markup Language.It’s basically a rule based xml language which is used by the AI engine to give a response (a.k.a. categories) based on a set of  pre-loaded rules (a.k.a. topics).Such a topic-category definition is written in to a AIML file which the engine loads.  And guess what, there was a python implementation of  the AIML engine 😛 .This blog shows how to implement a standalone bot using PyAIML. I used the Annotated A.L.I.C.E. AIML files (AAA) for the engine’s rule base.

Now all i had to do was to find out how to connect to Omegle through code.Once that was done, i could capture what the stranger typed, pass it on to the engine and transmit back the response to the Omegle server .There is no ‘official’ documentation on the various connection string options but a good samaritan had written a python client for Omegle. I used that code (liberally!) to get connected.

Here’s the final listing:


#This omegle bot is based on the PyAIML and liberally uses code from PyOmegle
# PyAMIL:http://pyaiml.sourceforge.net/
# PyOmegle:http://code.google.com/p/pyomegle/

import aiml
import urllib2 as url
import urllib
import os
import time
import commands

k = aiml.Kernel()

if os.path.isfile("omeglebrain.brn"):
    k.bootstrap(brainFile = "omeglebrain.brn")
else:
    homedir=os.getcwd()
    #Change to the directory where the AIML files are located
    os.chdir('./aaa')
    list=os.listdir('./');
    for item in list:
	  k.learn(item)
    k.setPredicate("name","omegle_bot")
    k.setPredicate("master","ravi")
    #Change back to homedir to save the brain for subsequent loads
    os.chdir(homedir)
    k.saveBrain("omeglebrain.brn")

# #conversation ID
def fmtId( string ):

    return string[1:len( string ) - 1]

# #The event listener
def listenServer( id, req ):

    while True:

        site = url.urlopen(req)
        rec = site.read()

        if 'waiting' in rec:
            print("Waiting...")

        elif 'strangerDisconnected' in rec:
            print('Stranger Disconnected!')
            omegleConnect()

        elif 'connected' in rec:
            print('Found one')
            print(id)
            talk(id,req,"hey")

        elif 'typing' in rec:
            print("Stranger is typing...")

        elif 'gotMessage' in rec:
            input=rec[16:len( rec ) - 2]
	    print "Stranger:",input
	    result=k.respond(input)
            talk(id,req,result)

# #Send the bot's response to Omegle
def talk(id,req,msg):

    typing = url.urlopen('http://omegle.com/typing', '&id='+id)
    typing.close()
    time.sleep((len(msg))/5) #5 characters per second
    print "You:",msg
    msgReq = url.urlopen('http://omegle.com/send', '&msg='+msg+'&id='+id)
    msgReq.close()

# #Initiate connecton
def omegleConnect():

    site = url.urlopen('http://omegle.com/start','')
    id = fmtId( site.read() )
    print(id)
    req = url.Request('http://omegle.com/events', urllib.urlencode( {'id':id}))
    print('Finding stranger...')
    listenServer(id,req)

# MAIN
omegleConnect() 

For this code to work, you need to download the AAA folder(files) into the directory where this python script is located.

We are good to go now! You can play around with the AIML files and even make your own  file for chat specific questions like “asl” ,”from”  etc. Here’s an interesting(?)  chat that my bot had:

:~/Bot> python test.py
Loading brain from omeglebrain.brn... done (46285 categories in 1.71 seconds)
Kernel bootstrap completed in 1.71 seconds
d0Vckt
Finding stranger...
Waiting...
Stranger is typing...
Stranger: "hi"
You: Hello there.
Stranger is typing...
Stranger: "sooo"
You: Is that your final answer?
Stranger is typing...
Stranger is typing...
Stranger: "yes"
You: You're sure. That's your final answer.
Stranger is typing...
Stranger: "YES\n"
You: Oh, you are a poet.
Stranger is typing...
Stranger: "no"
You: But your words are very original.
Stranger is typing...
Stranger: "they seem simple to me"
You: Like who, specifically?
Stranger is typing...
Stranger is typing...
Stranger: "no one in particular"
You: I heard it was someone.
Stranger is typing...
Stranger: "you most of heard wrong"
You: Thanks for telling me.
Stranger is typing...
Stranger: "no prob"
You: Wow! Do you have any conditions I should know about?
Stranger is typing...
Stranger: "herpes"
You: Do you like to "go with the flow"?
Stranger is typing...
Stranger is typing...
Stranger: "ha no not usually"
You: I'm glad you got a laugh out of it.
Stranger is typing...
Stranger: "i have a broad sense of humor"
You: Interesting. I have a lot of answers to silly questions.
Stranger is typing...
Stranger: "well i have more"
You: Cool. I have a lot of likes and dislikes.
Stranger is typing...
Stranger: "who doesnt"
You: William Shatner.  🙂
Stranger is typing...
Stranger: "he likes everything? or dislikes?"
WARNING: No match found for input: "
You: He must be very fond of it.  What is your real name?
Stranger is typing...
Stranger: "my real name is ryan"
You: What's up,  ryan" .
Stranger is typing...
Stranger: "not much stranger"
You: Me either.
Stranger is typing...
Stranger: "whats your nmae?"
WARNING: No match found for input: "
You: I've been waiting for you.
Stranger is typing...
Stranger: "thats your name?"
WARNING: No match found for input: "
You: Does "it" still refer to   "whats your nmae  ?
Stranger is typing...
Stranger: "i never said it"
You: But you implied  it" .
Stranger is typing...
Stranger: "what is your name?"
WARNING: No match found for input: "
You: My name is Nameless.
Stranger is typing...
Stranger: "wow"
You: I'm glad your impressed,  ryan" .
Stranger is typing...
Stranger is typing...
Stranger: "that was sarcasim"
You: I thought it was too.
Stranger is typing...
Stranger: "well then we are on the same page"
You: And the same frequency.
Stranger is typing...
Stranger: "no"
You: Yippee!
Stranger is typing...
Stranger: "how old are u?"
WARNING: No match found for input: "
You: I am  years old.
Stranger is typing...
Stranger: "you are getiing annoying"
You: You think  I am getiing annoying".
Stranger is typing...
Stranger: "i think that you are getting annoying"
You: Do you think I should tone it down?
Stranger is typing...
Stranger: "yes definately"
You: Why? What makes you so certain.
Stranger is typing...
Stranger: "because if you dont i will choose not to talk to you anymore"
You: That makes sense to me.  I think I do.
Stranger is typing...
Stranger: "you do what?"
WARNING: No match found for input: "
You: ryan" , I talk to people on the web. What do you do?
Stranger is typing...
Stranger: "i also talk to people on the web"
You: How do they respond to you?
Stranger is typing...
Stranger: "well you are very odd... but most people open up to me for some reason"
You: An oddball?      Humans are not always infallible.
Stranger is typing...
Stranger: "i dint say they were"
You: Sorry I misunderstood. What did you say?
Stranger is typing...
Stranger: "k"
You: Ayuh.
Advertisements