Skip to content
Browse files

initial commit

  • Loading branch information...
0 parents commit 0bded71e58c18b0788233ad983f5cb197b325c0a @robertjwhitney committed
Showing with 147 additions and 0 deletions.
  1. +3 −0 Gemfile
  2. +25 −0 Gemfile.lock
  3. +28 −0 README.md
  4. +16 −0 aiml/happybot.aiml
  5. +75 −0 app.rb
3 Gemfile
@@ -0,0 +1,3 @@
+gem 'thin'
+gem 'sinatra'
+gem 'programr'
25 Gemfile.lock
@@ -0,0 +1,25 @@
+GEM
+ specs:
+ daemons (1.1.8)
+ eventmachine (0.12.10)
+ programr (0.0.1)
+ rack (1.4.1)
+ rack-protection (1.2.0)
+ rack
+ sinatra (1.3.2)
+ rack (~> 1.3, >= 1.3.6)
+ rack-protection (~> 1.2)
+ tilt (~> 1.3, >= 1.3.3)
+ thin (1.3.1)
+ daemons (>= 1.0.9)
+ eventmachine (>= 0.12.6)
+ rack (>= 1.0.0)
+ tilt (1.3.3)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ programr
+ sinatra
+ thin
28 README.md
@@ -0,0 +1,28 @@
+# Happybot
+
+## Preface
+
+I'm not trying to evangelize AIML, far from it actually. I did however, spend a
+day learning and exploring what it was all about, and hence, ended up with some
+tools that make use of it.
+
+Since AIML is so simple maybe someone can use this to convince someone to
+pursue computer programming or something. The initial barrier to entry is really low
+and the novelty factor is pretty decent. Anyway, here comes the rest of
+the README!
+
+## About
+
+This little chat app makes use of the Sinatra Streaming API and the
+programr gem to let you interface with an AIML chatter bot.
+
+The streaming stuff is from https://gist.github.com/1476463 and should
+be pretty straight forward.
+
+AIML INFO: http://www.alicebot.org/aiml.html
+
+You can find a more complext set of ALICE AIML files which have better examples of what AIML can do hosted at http://code.google.com/p/aiml-en-us-foundation-alice
+
+Some of them have thrown errors in programr so a subset is available here: https://github.com/robertjwhitney/alice-programr
+
+### Knock yourself out.
16 aiml/happybot.aiml
@@ -0,0 +1,16 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<aiml version="1.0">
+ <category>
+ <pattern>WHO ARE YOU</pattern>
+ <template>I am HappyBot.</template>
+ </category>
+ <category>
+ <pattern>ARE YOU HAPPY *</pattern>
+ <template>Happy as a HappyBot</template>
+ </category>
+ <category>
+ <pattern>WHAT DO YOU DREAM ABOUT</pattern>
+ <template>Electric Sheep.</template>
+ </category>
+</aiml>
+
75 app.rb
@@ -0,0 +1,75 @@
+# coding: utf-8
+require 'sinatra'
+Bundler.setup :default
+
+require 'programr'
+
+HAPPYBOT = ProgramR::Facade.new
+HAPPYBOT.learn(['./aiml/happybot.aiml'])
+
+set server: 'thin', connections: []
+
+get '/' do
+ halt erb(:login) unless params[:user]
+ erb :chat, locals: { user: params[:user].gsub(/\W/, '') }
+end
+
+get '/stream', provides: 'text/event-stream' do
+ stream :keep_open do |out|
+ settings.connections << out
+ out.callback { settings.connections.delete(out) }
+ end
+end
+
+post '/' do
+ reply = HAPPYBOT.get_reaction(params[:msg])
+ if reply.empty?
+ reply = "I don't know anything about that yet..."
+ end
+ settings.connections.each { |out| out << "data: #{params[:user]}: #{params[:msg]}, #{reply}\n\n"}
+ 204 # response without entity body
+end
+
+__END__
+
+@@ layout
+<html>
+ <head>
+ <title>Super Simple Chat with Sinatra</title>
+ <meta charset="utf-8" />
+ <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
+ </head>
+ <body><%= yield %></body>
+</html>
+
+@@ login
+<form action='/'>
+ <label for='user'>User Name:</label>
+ <input name='user' value='' />
+ <input type='submit' value="GO!" />
+</form>
+
+@@ chat
+<pre id='chat'></pre>
+
+<script>
+ // reading
+ var es = new EventSource('/stream');
+ es.onmessage = function(e) {
+ msg = e.data.split(',')
+ $('#chat').append(msg[0] + "\n")
+ setTimeout(600)
+ $('#chat').append("Happybot:" + msg[1] + "\n")
+ };
+
+ // writing
+ $("form").live("submit", function(e) {
+ $.post('/', {user: "<%= user %>", msg: $('#msg').val()});
+ $('#msg').val(''); $('#msg').focus();
+ e.preventDefault();
+ });
+</script>
+
+<form>
+ <input id='msg' placeholder='type message here...' />
+</form>

0 comments on commit 0bded71

Please sign in to comment.
Something went wrong with that request. Please try again.