wm: soxangoo-bot

ref: 74ca96eacd016d656157660a289c9d68fcb18db1
dir: /main.rb/

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

BOTID = "Soxangoo_bot"
ADMIN = 331358784

$silent = false
$token = "Your-Telegram-Bot-Token"
$client = OpenAI::Client.new(access_token: "Your-OpenAI-Key")

Signal.trap('INT') do
  bot.stop
end

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 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
                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
              # Tagged Message
              if message.text.start_with?("@#{BOTID} ") then
                received_message.slice! "@#{BOTID} "
                if (CheckAdmin?(message.from.id, received_message))
                  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
              # 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
          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