wm: irc-h2o-bot

Download patch

ref: 28d9e6925a69a7628b3c483214e7170d92b5ed0e
parent: b087007630e4e073ffca2a3be249645dc4db8713
author: 0x4A4D00 <scorpion_rn@yahoo.com>
date: Tue Jul 25 17:51:43 EDT 2023

Cleaned - Some Fixes

--- /dev/null
+++ b/bot.rb
@@ -1,0 +1,127 @@
+require 'net/http'
+require 'uri'
+require 'json'
+require 'IRC'
+
+config = File.read('CONFIG')
+
+parsed_config = JSON.parse(config)
+Server = parsed_config['IRCServer']
+BotNick = parsed_config['BotNickName']
+$URL = parsed_config['SessionUrl']
+$Cookie = parsed_config['SessionCookie']
+
+$bot = IRC.new(
+  BotNick,
+  Server,
+  "6667",
+  "H2O")
+
+class Sender
+
+  def Wait(peer, from = nil)
+    if from != nil
+      $bot.send_message(peer, "#{from}, Im thinking...")
+    else
+      $bot.send_message(peer, "Im thinking...")
+    end
+  end
+
+  def SendReq(peer, input, c)
+    uri = URI($URL)
+    https = Net::HTTP.new(uri.host, uri.port)
+    https.use_ssl = true
+
+    request = Net::HTTP::Post.new(uri.path)
+
+    request['Content-Type'] = 'application/json; charset=UTF-8'
+    request['User-Agent'] = 'Pedaret Mozilla/5.0 Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.5672.127 Safari/537.36'
+    request['Cookie'] = $Cookie
+
+    request.body = '{"inputs":"'+ input.to_s() +'","parameters":{"temperature":0.1,"truncate":2048,"max_new_tokens":1024,"do_sample":true,"repetition_penalty":1.2,"return_full_text":false},"stream":true,"options":{"id":"6543eb99-e311-4f4e-b791-7af4fcbe556b","response_id":"b194833d-e13c-4390-9d7f-3cf4e4d95430","is_retry":false,"use_cache":false,"web_search_id":""}}'
+
+    begin
+      response = https.request(request)
+
+
+      json_strings = response.body.split('data:')
+
+      parsed_json_objects = []
+
+      json_strings.each do |json_str|
+        if json_str != ''
+          parsed_json_objects << JSON.parse(json_str)
+        end
+      end
+
+      counts = 0
+
+      b = ''
+      parsed_json_objects.each do |json_obj|
+        result = json_obj['token']['text']
+        if result == '<|endoftext|>' then
+          break
+        elsif counts >= 50
+        begin
+          $bot.send_message(peer, 'limit reached!')
+          break
+        end
+        elsif result.include?("\n")
+        begin
+          $bot.send_message(peer, b)
+          sleep(0.3)
+          counts += 1
+          b = ''
+        end
+        else
+          b += result
+        end
+      end
+
+      $bot.send_message(peer, b)
+
+    rescue StandardError => e
+      if (e.to_s.start_with?("SSL_read"))
+        retry
+      end
+      $bot.send_message(peer, e)
+    end
+  end
+end
+
+sender = Sender.new
+
+IRCConnection.add_IO_socket(STDIN)
+IRCEvent.add_callback('endofmotd') { |event| $bot.add_channel('#fpc') }
+
+IRCEvent.add_callback('invite') do |event|
+  puts event.message
+  puts event.channel
+  $bot.add_channel(event.message)
+end
+
+IRCEvent.add_callback('privmsg') {|event|
+  if event.channel.eql?(BotNick)                      # pv message
+  begin
+    if (event.message.start_with?(".invite"))         # invite message
+    begin
+        event.message.slice! ".invite "
+        $bot.add_channel(event.message)
+    end
+    else
+      sender.Wait(event.from)
+      #$bot.send_message(event.from, "I'm thinking...")
+      sender.SendReq(event.from, event.message, '')
+    end
+  end
+  else                                                # channel message
+    if (event.message.start_with?("h2o"))
+      sender.Wait(event.channel, event.from)
+      #$bot.send_message(event.channel, "#{event.from}, I'm thinking...")
+      sender.SendReq(event.channel, event.message.gsub(/#{"h2o "}\b/, ""), '')
+    end
+  end
+
+}
+
+$bot.connect