ref: c7c39ce29db8631d72f3d5c5b73c04751e0d67e5
dir: /main.rb/
require "openai"
require 'telegram/bot'
BOTID = "Soxangoo_bot"
ADMIN = 331358784
@silent = false
@token = "6449806349:AAER7n4sp9fZSlUfb_d4g3m-ZziNTWL25Fk"
@client = OpenAI::Client.new(access_token: "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")
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