wm: irc-h2o-bot

Download patch

ref: e9c239a3e0c40963003327907c55a03c3ba040d5
parent: cc357037f202611bbbc399f4683a24fe3494ab33
author: 0x4A4D00 <scorpion_rn@yahoo.com>
date: Sat Jul 22 17:33:05 EDT 2023

Async Working (Except in msg Spam) - Config Ability - Push & Send Procedures Added

--- a/.idea/Test-IRC.iml
+++ b/.idea/Test-IRC.iml
@@ -16,17 +16,16 @@
     <orderEntry type="library" scope="PROVIDED" name="async-http (v0.60.2, ruby-3.2.1-p31) [gem]" level="application" />
     <orderEntry type="library" scope="PROVIDED" name="async-io (v1.35.0, ruby-3.2.1-p31) [gem]" level="application" />
     <orderEntry type="library" scope="PROVIDED" name="async-pool (v0.4.0, ruby-3.2.1-p31) [gem]" level="application" />
+    <orderEntry type="library" scope="PROVIDED" name="cinch (v2.3.4, ruby-3.2.1-p31) [gem]" level="application" />
     <orderEntry type="library" scope="PROVIDED" name="console (v1.17.4, ruby-3.2.1-p31) [gem]" level="application" />
     <orderEntry type="library" scope="PROVIDED" name="fiber-annotation (v0.2.0, ruby-3.2.1-p31) [gem]" level="application" />
     <orderEntry type="library" scope="PROVIDED" name="fiber-local (v1.0.0, ruby-3.2.1-p31) [gem]" level="application" />
     <orderEntry type="library" scope="PROVIDED" name="io-event (v1.2.2, ruby-3.2.1-p31) [gem]" level="application" />
     <orderEntry type="library" scope="PROVIDED" name="net-yail (v1.6.2, ruby-3.2.1-p31) [gem]" level="application" />
-    <orderEntry type="library" scope="PROVIDED" name="nokogiri (v1.14.2, ruby-3.2.1-p31) [gem]" level="application" />
     <orderEntry type="library" scope="PROVIDED" name="protocol-hpack (v1.4.2, ruby-3.2.1-p31) [gem]" level="application" />
     <orderEntry type="library" scope="PROVIDED" name="protocol-http (v0.24.4, ruby-3.2.1-p31) [gem]" level="application" />
     <orderEntry type="library" scope="PROVIDED" name="protocol-http1 (v0.15.0, ruby-3.2.1-p31) [gem]" level="application" />
     <orderEntry type="library" scope="PROVIDED" name="protocol-http2 (v0.15.1, ruby-3.2.1-p31) [gem]" level="application" />
-    <orderEntry type="library" scope="PROVIDED" name="racc (v1.6.2, ruby-3.2.1-p31) [gem]" level="application" />
     <orderEntry type="library" scope="PROVIDED" name="timers (v4.3.5, ruby-3.2.1-p31) [gem]" level="application" />
     <orderEntry type="library" scope="PROVIDED" name="traces (v0.11.1, ruby-3.2.1-p31) [gem]" level="application" />
   </component>
--- a/Test_cinch.rb
+++ b/Test_cinch.rb
@@ -1,34 +1,23 @@
 require 'cinch'
-require 'open-uri'
-require 'nokogiri'
-require 'cgi'
 
+hash = { :invalid => :replace, :undef => :replace}
+string = hash.to_s
+
+puts string
+
 bot = Cinch::Bot.new do
   configure do |c|
-    c.server   = "irc.ircnow.org"
-    c.nick     = "moz"
-    c.channels = ["#fpc"]
+    c.server = 'irc.ircnow.org'
+    c.channels = ['#fpc']
+    c.nick = 'moz0'
   end
 
-  helpers do
-    # Extremely basic method, grabs the first result returned by Google
-    # or "No results found" otherwise
-    def google(query)
-      url = "http://www.google.com/search?q=#{CGI.escape(query)}"
-      res = Nokogiri.parse(open(url).read).at("h3.r")
-
-      title = res.text
-      link = res.at('a')[:href]
-      desc = res.at("./following::div").children.first.text
-    rescue
-      "No results found"
-    else
-      CGI.unescape_html "#{title} - #{desc} (#{link})"
-    end
-  end
-
-  on :message, /^!google (.+)/ do |m, query|
-    m.reply google(query)
+  on :connect do
+    # This block will be executed when the bot connects to the IRC server
+    # You can put your async message sending logic here
+    a = 'a'
+    Channel('#channel').send(a.to_s) # Replace '#channel' with the actual channel name
+    bot.quit # Quit the bot after sending the message
   end
 end
 
--- a/ruby-irc.rb
+++ b/ruby-irc.rb
@@ -1,4 +1,3 @@
-
 require 'IRC'
 
 bot = IRC.new("h2o", "irc.ircnow.org", "6667", "H2O")
--- a/test.rb
+++ b/test.rb
@@ -9,12 +9,20 @@
 require 'async/http/endpoint'
 require 'IRC'
 
-executor = Concurrent::ThreadPoolExecutor.new(
-  min_threads: 2,
-  max_threads: 5,
-  max_queue: 10
-)
+# executor = Concurrent::ThreadPoolExecutor.new(
+#   min_threads: 2,
+#   max_threads: 5,
+#   max_queue: 10
+# )
 
+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']
+
 $irc = Net::YAIL.new(
   :address    => 'irc.ircnow.org',
   :username   => 'Moz_Bot',
@@ -22,8 +30,6 @@
   :nicknames  => ['h2o-bot0']
 )
 
-Server = irc.ircnow.org
-BotNick = "h2o"
 
 $bot = IRC.new(
   BotNick,
@@ -52,8 +58,16 @@
 $irc.on_invite     { |e| $irc.join(e.channel) }
 
 #:if => {:message => "bah"}
+@stack = []
 
+def Push(a,b)
+  @stack << {:peer => a, :message => b}
 
+end
+
+def Send
+  $bot.send_message(@stack[0][':peer'], @stack[0][':message'])
+end
 class Sender
   include Concurrent::Async
   $irc0 = Net::YAIL.new(
@@ -67,7 +81,7 @@
   end
 
   def SendReq(peer, input, c)
-    uri = URI('https://gpt-gm.h2o.ai/conversation/64ac1344245bd909c99ed909')
+    uri = URI($URL)
     https = Net::HTTP.new(uri.host, uri.port)
     https.use_ssl = true
 
@@ -75,10 +89,11 @@
 
     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'] = 'h2ogpt-chat=a4fc9f08-6c67-466c-8d36-dd425702d6b4'
+    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":""}}'
 
+
     # Async do |task|
 
       # Async do
@@ -117,48 +132,56 @@
     begin
       response = https.request(request)
 
+      #$irc.msg('#fpc', e.message)
 
+      json_strings = response.body.split('data:')
 
-    #$irc.msg('#fpc', e.message)
+      parsed_json_objects = []
 
-    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)
+      json_strings.each do |json_str|
+        if json_str != ''
+          parsed_json_objects << JSON.parse(json_str)
+        end
       end
-    end
 
-    b = ''
-    parsed_json_objects.each do |json_obj|
-      result = json_obj['token']['text']
-      if result == '<|endoftext|>' then
-        break
-      elsif result.include?("\n")
+      counts = 0
+
+      b = ''
+      parsed_json_objects.each do |json_obj|
+        result = json_obj['token']['text']
+        if result == '<|endoftext|>' then
+          break
+        elsif counts >= 20
         begin
+          $bot.send_message(peer, 'limit reached!')
+          break
+        end
+        elsif result.include?("\n")
+        begin
           $bot.send_message(peer, b)
-          # $irc.msg(peer, b)
+          counts += 1
+          #$irc.msg(peer, b)
           b = ''
         end
-      elsif (1) then
-        b += result
+        else
+          b += result
+        end
       end
-    end
 
-    $bot.send_message(peer, b)
-    # $irc.msg(peer, b)
+      $bot.send_message(peer, b)
+      #$irc.msg(peer, b)
 
     rescue StandardError => e
       $bot.send_message(peer, e)
+      #$irc.msg(peer, e)
     end
   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
@@ -168,29 +191,30 @@
 # IRCEvent.add_callback('join') { |event|
 #   bot.send_message(event.channel, "Hello #{event.from}")
 IRCEvent.add_callback('privmsg') {|event|
-  puts event.channel
-  puts event.from
-
   if event.channel.eql?(BotNick)
   begin
     if (event.message.start_with?(".invite"))
-    begin
-      event.message.slice! ".invite "
-      $bot.add_channel(event.message)
-    end
+      begin
+        event.message.slice! ".invite "
+        $bot.add_channel(event.message)
+      end
     else
       $bot.send_message(event.from, "I'm thinking...")
-      sender.SendReq(event.from, event.message, '')
+      sender.async.SendReq(event.from, event.message, '')
     end
   end
   else
     if (event.message.start_with?("h2o"))
+      Push(event.channel, 'Im')
+      Send()
+      # IRCConnection.output_push("PRIVMSG #{event.channel} : moz")
       $bot.send_message(event.channel, "#{event.from}, I'm thinking...")
-      sender.SendReq(event.channel, event.message, '')
+      sender.async.SendReq(event.channel, event.message, '')
     end
   end
 
 }
+
 # }
 
 $bot.connect
@@ -226,79 +250,4 @@
 
 futures.each(&:wait)
 executor.shutdown
-
-
-
-def Tra
-  $irc.heard_msg(:if => {:pm? => true}) do |e|
-    if ($currentid != e.nick)
-      $currentid = e.nick
-      SendReq(e.nick, e.message, $irc)
-    end
-    $currentid = ''
-  end
-
-  $irc.heard_msg(:if => {:pm? => false}) do |e|
-    if (e.message.start_with?("h2o") && $currentid != e.channel)
-      $currentid = e.channel
-      SendReq(e.channel, e.message, $irc)
-    end
-    $currentid = ''
-  end
-end
-
-def Tra2
-  $irc.heard_msg(:if => {:pm? => true}) do |e|
-    SendReq(e.nick, e.message, $irc)
-  end
-
-  $irc.heard_msg(:if => {:pm? => false}) do |e|
-    if (e.message.start_with?("h2o"))
-      SendReq(e.channel, e.message, $irc)
-    end
-  end
-end
-
-def Tra3
-  $irc.heard_msg(:if => {:pm? => true}) do |e|
-    SendReq(e.nick, e.message, $irc)
-  end
-
-  $irc.heard_msg(:if => {:pm? => false}) do |e|
-    if (e.message.start_with?("h2o"))
-      SendReq(e.channel, e.message, $irc)
-    end
-  end
-end
-
-#threads = []
-#threads << Thread.new { Tra() }
-#threads << Thread.new { Tra() }
-#threads << Thread.new { Tra() }
-
-# Wait for all threads to complete
-#threads.each(&:join)
-
-
-
-# $irc.heard_msg() do |e|
-#
-#
-#
-# end
-
-
-
-
-
-$irc.heard_msg(:if => {:pm? => true}) do |e|
-  SendReq(e.nick, e.message, $irc)
-end
-
-$irc.heard_msg(:if => {:pm? => false}) do |e|
-  if (e.message.start_with?("h2o"))
-    SendReq(e.channel, e.message, $irc)
-  end
-end
-