Skip to content

Instantly share code, notes, and snippets.

@bjeanes
Created December 19, 2009 15:02
Show Gist options
  • Select an option

  • Save bjeanes/260114 to your computer and use it in GitHub Desktop.

Select an option

Save bjeanes/260114 to your computer and use it in GitHub Desktop.
require 'net/http'
require 'uri'
require 'enumerator'
class Array
def to_h
Hash[*enum_with_index.to_a.flatten]
end
end
class Bot
ResponseRegexp = /<!-- Begin Response !-->\s+(.*)\s+<!-- End Response !-->/
FormRegexp = /<INPUT NAME=([a-zA-Z0-9]+?) TYPE=hidden VALUE="(.*?)">/
Url = URI.parse('http://www.cleverbot.com/webserviceform')
def initialize
@response = ""
@form = {}
end
def send(message = nil)
@form["STIMULUS"] = message if message
result = Net::HTTP.post_form(Url, @form)
@form = result.body.scan(FormRegexp).to_h
@response = result.body.scan(ResponseRegexp).to_s
end
end
bots = 2.times.map { Bot.new }
message = nil
i = 0
loop do
message = bots[i].send(message)
puts "Bot #{i + 1}: #{message}"
i = (i + 1) % bots.size
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment