IrisRb
Iris(https://github.com/dolidolih/Iris) 를 기반으로 루비로 포팅한 레포
설치
- Ruby ~>3.4.0
How use
gem install bundler
bundle install
빠른 시작
기본 사용법
require_relative 'lib/iris_rb/client'
def (chat)
if msg == ".hi"
reply("Hello #{chat[:sender][:name]}")
elsif msg == ".이미지"
send_image(chat[:room][:id], "./image/example.jpg")
end
end
def on_newmem(chat)
reply("#{chat[:sender][:name]}님 환영합니다!")
end
def on_delmem(chat)
reply("#{chat[:sender][:name]}님 안녕히 가세요!")
end
client = IrisRb::Client.new(
url: "http://localhost:3000",
hot_reload: true
)
sleep
API 문서
Client 초기화
client = IrisRb::Client.new(
url: "http://localhost:3000", # Iris 서버 URL
hot_reload: true # 개발 모드 (파일 변경 시 자동 재시작)
)
파라미터
url(필수): Iris 서버의 HTTP URLhot_reload(선택): 파일 변경 감지 및 자동 재시작 활성화 (기본값: false)
이벤트 핸들러
on_message(chat)
일반 메시지를 받았을 때 호출됩니다.
def (chat)
# chat 객체 구조:
# {
# room: { id: "...", name: "...", type: "..." },
# sender: { id: "...", name: "..." },
# message: { id: "...", type: "...", content: "...", attachment: {}, v: {} }
# }
end
on_newmem(chat)
새로운 멤버가 입장했을 때 호출됩니다.
def on_newmem(chat)
reply("환영합니다!")
end
on_delmem(chat)
멤버가 퇴장했을 때 호출됩니다.
def on_delmem(chat)
reply("안녕히 가세요!")
end
메시지 전송
reply(message)
현재 방에 텍스트 메시지를 전송합니다.
reply("안녕하세요!")
send_image(room_id, image_path_or_base64)
이미지를 전송합니다.
# 파일 경로로 전송
send_image(chat[:room][:id], "./image/photo.jpg")
# Base64 문자열로 전송
send_image(chat[:room][:id], "iVBORw0KGgoAAAANSUhEUgAA...")
send_image_multiple(room_id, image_base64s)
여러 이미지를 한 번에 전송합니다.
client.send_image_multiple(room_id, [base64_image1, base64_image2])
데이터베이스 쿼리
query(query_str, bind = [])
SQL 쿼리를 실행합니다.
# 단순 쿼리
result = client.query("SELECT * FROM users")
# Prepared statement
result = client.query("SELECT * FROM users WHERE id = ?", [user_id])
헬퍼 함수
msg
현재 메시지의 내용을 반환합니다 (전역 변수).
def (chat)
if msg == ".help"
reply("도움말...")
end
end
Chat 객체 구조
{
room: {
id: "방 ID",
name: "방 이름",
type: "방 타입"
},
sender: {
id: "발신자 ID",
name: "발신자 닉네임"
},
message: {
id: "메시지 ID",
type: "메시지 타입",
content: "메시지 내용",
attachment: {}, # 첨부파일 정보
v: {} # 추가 메타데이터
},
raw: "원본 JSON 문자열"
}
예제
에코 봇
def (chat)
reply("Echo: #{msg}")
end
명령어 봇
def (chat)
case msg
when ".안녕"
reply("안녕하세요!")
when ".시간"
reply(Time.now.strftime("%Y-%m-%d %H:%M:%S"))
when /^.계산 (.+)/
expression = $1
begin
result = eval(expression)
reply("결과: #{result}")
rescue
reply("계산 오류")
end
end
end
이미지 전송 봇
def (chat)
if msg == ".고양이"
send_image(chat[:room][:id], "./image/cat.jpg")
end
end
개발
Hot Reload
개발 중에는 hot_reload: true 옵션을 사용하여 파일 변경 시 자동으로 재시작할 수 있습니다:
client = IrisRb::Client.new(
url: "http://localhost:3000",
hot_reload: true
)
.rb 파일이 변경되면 자동으로 스크립트가 재시작됩니다.
버전
현재 버전: 0.0.1
라이선스
이 프로젝트는 개인 프로젝트입니다.