wm: soxangoo-bot

ref: 4e8108cf1aabfc56fa294863a385453ddff093b4
dir: /main.rb/

View raw version
require "openai"
require 'telegram/bot'

token = "your-bot-token"
client = OpenAI::Client.new(access_token: "your-openai-key")

id = "@Soxangoo_bot"
admin = 331358784
silent = false

Signal.trap('INT') do
  bot.stop
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
                  return
                end
                if (a.eql?("on"))
                  silent = false
                  return
                end
              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
                  return
                end
                if (a.eql?("on"))
                  silent = false
                  return
                end
              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
        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
                return
              end
            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
        end
      end
    end
  end
end