Created
July 6, 2024 21:25
-
-
Save gavingmiller/5e141cda7692cdb00f306ac62fc20a23 to your computer and use it in GitHub Desktop.
full-program
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'net/http' | |
class Bot | |
def post(data, speech) | |
data['STIMULUS'] = speech | |
data['sub'] = 'Say' | |
response = Net::HTTP.post_form( | |
URI.parse('http://jabberwacky.icogno.com/webserviceform-joan'), | |
data | |
) | |
return response.body | |
end | |
def start_conversation | |
url = URI.parse('http://jabberwacky.icogno.com/webserviceform-joan') | |
request = Net::HTTP::Get.new(url.path) | |
response = Net::HTTP.start(url.host, url.port) {|http| | |
http.request(request) | |
} | |
return response.body | |
end | |
end | |
def get_response(html) | |
html.chomp! | |
start_index = html.index('Begin Response !-->') + | |
'Begin Response !-->'.length | |
end_index = html.index('<!-- End Response') | |
result = html[start_index, end_index - start_index] | |
while (result.index('}')) | |
result = result[result.index('}') + 1, | |
result.length - result.index('}')] | |
end | |
return result | |
end | |
def find_in_text(text, value) | |
matches = /NAME=#{value}\sTYPE=hidden VALUE="([^"]*)"/.match(text) | |
if (matches) | |
return matches[1] | |
end | |
return '' | |
end | |
def get_hidden_inputs(text) | |
return text.scan(/NAME=(\w+) TYPE=hidden/) | |
end | |
# Retrieve all the variables and setup as a hash for doing a POST | |
def get_post_variables(response_text) | |
data = {} | |
get_hidden_inputs(response_text).each do |n| | |
data[n] = find_in_text(response_text, n) | |
end | |
return data | |
end | |
# Main | |
perl = Bot.new | |
ruby = Bot.new | |
perl_response_text = perl.start_conversation | |
perl_response = get_response(perl_response_text) | |
perl_data = get_post_variables(perl_response_text) | |
puts "perl: #{perl_response}" | |
STDOUT.flush | |
ruby_response_text = ruby.start_conversation | |
ruby_response = get_response(ruby_response_text) | |
ruby_data = get_post_variables(ruby_response_text) | |
puts "ruby: #{ruby_response}" | |
STDOUT.flush | |
i = 0 | |
while i > 10 | |
perl_response_text = perl.post(perl_data, ruby_response) | |
perl_response = get_response(perl_response_text) | |
perl_data = get_post_variables(perl_response_text) | |
puts "perl: #{perl_response}" | |
STDOUT.flush | |
ruby_response_text = ruby.post(ruby_data, perl_response) | |
ruby_response = get_response(ruby_response_text) | |
ruby_data = get_post_variables(ruby_response_text) | |
puts "ruby: #{ruby_response}" | |
STDOUT.flush | |
i += 1 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment