wm: soxangoo-bot

Download patch

ref: c7c39ce29db8631d72f3d5c5b73c04751e0d67e5
parent: 4e8108cf1aabfc56fa294863a385453ddff093b4
author: jm <scorpion_rn@yahoo.com>
date: Sat Dec 30 23:37:15 EST 2023

Cleaned

--- a/main.rb
+++ b/main.rb
@@ -1,121 +1,89 @@
 require "openai"
 require 'telegram/bot'
 
-token = "your-bot-token"
-client = OpenAI::Client.new(access_token: "your-openai-key")
+BOTID = "Soxangoo_bot"
+ADMIN = 331358784
 
-id = "@Soxangoo_bot"
-admin = 331358784
-silent = false
+@silent = false
+@token = "6449806349:AAER7n4sp9fZSlUfb_d4g3m-ZziNTWL25Fk"
+@client = OpenAI::Client.new(access_token: "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")
 
 Signal.trap('INT') do
   bot.stop
 end
 
-Telegram::Bot::Client.run(token) do |bot|
+def SendChat(message)
+  response = @client.chat(
+    parameters: {
+      model: "gpt-3.5-turbo",
+      messages: [{ role: "system", content: message}],
+      temperature: 0.7,
+    })
+  return response
+end
+
+def CheckAdmin?(id, message)
+  if (id == ADMIN) then
+    if (message.eql?("off"))
+      @silent = true
+      return true
+    end
+    if (message.eql?("on"))
+      @silent = false
+      return true
+    end
+  end
+  return false
+end
+
+Telegram::Bot::Client.run(@token) do |bot|
   bot.listen do |message|
     case message
     when Telegram::Bot::Types::Message
-      if (!silent) || message.from.id == admin then
-        if message.chat.type.eql?("supergroup") then
-          if message.reply_to_message.is_a?(Telegram::Bot::Types::Message) && message.reply_to_message.from.username.eql?("Soxangoo_bot") then
-            begin
-              a = message.text;
-
-              if (message.from.id == admin) then
-                if (a.eql?("off"))
-                  silent = true
+      if message.text.is_a?(String) then
+        received_message = message.text;
+        begin
+          if (!@silent) || message.from.id == ADMIN then
+            # GP Message
+            if message.chat.type.eql?("supergroup") then
+              # Replied Message
+              if message.reply_to_message.is_a?(Telegram::Bot::Types::Message) && message.reply_to_message.from.username.eql?(BOTID) then
+                if (CheckAdmin?(message.from.id, received_message))
                   return
                 end
-                if (a.eql?("on"))
-                  silent = false
-                  return
-                end
+                response = SendChat(received_message)
+                bot.api.send_message(chat_id: message.chat.id, reply_to_message_id: message.message_id, text: "#{response['choices'][0]['message']['content']}", parse_mode: 'Markdown')
               end
-
-              response = client.chat(
-                parameters: {
-                  model: "gpt-3.5-turbo", # Required.
-                  messages: [{ role: "system", content: a}],
-                  temperature: 0.7,
-                })
-
-              bot.api.send_message(chat_id: message.chat.id, reply_to_message_id: message.message_id, text: "#{response['choices'][0]['message']['content']}", parse_mode: 'Markdown')
-            rescue => error
-              sleep(1);
-              bot.api.send_message(chat_id: message.chat.id, reply_to_message_id: message.message_id, text: error.message);
-            rescue SyntaxError => se
-              bot.api.send_message(chat_id: message.chat.id, reply_to_message_id: message.message_id, text: se.message);#"Syntax Error Dash.");
-            rescue NoMemoryError => nme
-              bot.api.send_message(chat_id: message.chat.id, reply_to_message_id: message.message_id, text: nme.message);
-            end
-          end
-          if message.text.is_a?(String) && message.text.start_with?("#{id} ") then
-            begin
-              a = message.text;
-              a.slice! "#{id} "
-
-              if (message.from.id == admin) then
-                if (a.eql?("off"))
-                  silent = true
+              # Tagged Message
+              if message.text.start_with?("@#{BOTID} ") then
+                received_message.slice! "@#{BOTID} "
+                if (CheckAdmin?(message.from.id, received_message))
                   return
                 end
-                if (a.eql?("on"))
-                  silent = false
-                  return
-                end
-              end
+                response = SendChat(received_message)
+                bot.api.send_message(chat_id: message.chat.id, reply_to_message_id: message.message_id, text: "#{response['choices'][0]['message']['content']}", parse_mode: 'Markdown');
 
-              response = client.chat(
-                parameters: {
-                  model: "gpt-3.5-turbo", # Required.
-                  messages: [{ role: "system", content: a}],
-                  temperature: 0.7,
-                });
-
-              bot.api.send_message(chat_id: message.chat.id, reply_to_message_id: message.message_id, text: "#{response['choices'][0]['message']['content']}", parse_mode: 'Markdown');
-            rescue => error
-              sleep(1);
-              bot.api.send_message(chat_id: message.chat.id, reply_to_message_id: message.message_id, text: error.message);
-            rescue SyntaxError => se
-              bot.api.send_message(chat_id: message.chat.id, reply_to_message_id: message.message_id, text: se.message);#"Syntax Error Dash.");
-            rescue NoMemoryError => nme
-              bot.api.send_message(chat_id: message.chat.id, reply_to_message_id: message.message_id, text: nme.message);
-            end
-          end
-        elsif message.chat.type.eql?("private") then
-          begin
-            if (message.chat.id == admin) then
-              if (message.text.eql?("off"))
-                silent = true
-                return
               end
-              if (message.text.eql?("on"))
-                silent = false
+            # PV Message
+            elsif message.chat.type.eql?("private") then
+              if (CheckAdmin?(message.chat.id, received_message))
                 return
               end
+              response = SendChat(received_message)
+              bot.api.send_message(chat_id: message.chat.id, text: "#{response['choices'][0]['message']['content']}", parse_mode: 'Markdown')
             end
-
-            a = message.text;
-            response = client.chat(
-              parameters: {
-                model: "gpt-3.5-turbo", # Required.
-                messages: [{ role: "system", content: a}],
-                temperature: 0.7,
-              })
-
-            bot.api.send_message(chat_id: message.chat.id, text: "#{response['choices'][0]['message']['content']}", parse_mode: 'Markdown')
-          rescue => error
-            sleep(1);
-            bot.api.send_message(chat_id: message.chat.id, text: error.message);
-          rescue SyntaxError => se
-            bot.api.send_message(chat_id: message.chat.id, text: se.message);#"Syntax Error Dash.");
-          rescue NoMemoryError => nme
-            bot.api.send_message(chat_id: message.chat.id, text: nme.message);
           end
+        # Exceptions
+        rescue => error
+          bot.api.send_message(chat_id: message.chat.id, reply_to_message_id: message.message_id, text: error.message);
+        rescue NoMemoryError => nme
+          bot.api.send_message(chat_id: message.chat.id, reply_to_message_id: message.message_id, text: nme.message);
+        rescue ResponseError => re # !
+          bot.api.send_message(chat_id: ADMIN, text: re.message);
         end
       end
     end
   end
 end
+