Ruby로 Telegram 인용 봇 만들기
Source: Dev.to
Requirements
- IDE가 반드시 필요합니다
- 프로젝트 구조:
project_folder/
├── bot.rb
├── quote.rb
├── help.rb
├── start.rb
└── Gemfile
Note: .env 파일은 git에 커밋되지 않습니다.
start.rb
# start.rb
class Start
def initialize(bot)
@bot = bot
start_handler
end
def start_handler
@bot.command("start") do |ctx|
ctx.reply("welcome to quote_bot use /quote to get astonishing quotes")
end
end
end
telegem은 / 명령어(예: /help, /start, /quote)를 처리하기 위한 .command 메서드를 제공합니다.
ctx.reply는 현재 채팅에 답장을 보내며 reply_markup와 parse_mode 같은 옵션을 받을 수 있습니다.
help.rb
# help.rb
class Help
def initialize(bot)
@bot = bot
send_help_message
end
def send_help_message
@bot.command("help") do |ctx|
help_message = 3.0.4"
Gemfile
gem "httparty", "~> 0.21.0"
gem "dotenv", "~> 2.8"
.env file
.env 파일을 생성하고(커밋 하지 말 것) 토큰을 입력하세요:
BOT_TOKEN=
Installation & Running
# 필요하면 bundler 설치
gem install bundler
# 의존성 설치
bundle install
# 봇 실행
bundle exec ruby bot.rb
보다 자세한 예시는 저장소를 참고하세요.