Ruby로 Telegram 인용 봇 만들기

발행: (2026년 1월 17일 오후 05:53 GMT+9)
2 min read
원문: Dev.to

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_markupparse_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

보다 자세한 예시는 저장소를 참고하세요.

Back to Blog

관련 글

더 보기 »

Ruby 예외 처리와 정규 표현식

예외 처리: Ruby에서 예외를 발생시키고 처리하는 기본적인 방법을 살펴봅니다. ```ruby def raise_exception puts 'I am before the raise.' raise 'An error has occured' puts 'I am after the raise.' end ``` 위 코드를 실행하면…

C# 조건문 (if, else, switch)

원본은 if Statement에 처음 게시되었습니다. if 문은 조건이 true일 때 블록을 실행합니다. csharp int number = 10; if number > 5 { Console.WriteLine'Num...