百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
T

telegram_bot

> 编程语言
开源

一个充满魅力的 Ruby 客户端,用于 Telegram 的 Bot API

296 stars0 点赞3 次浏览
访问官网GitHub

工具介绍

一个充满魅力的 Ruby 客户端,用于 Telegram 的 Bot API

This gem has been archived

Hi everyone, I haven't touched this code in a long time and I decided it may be best to archive the repo and put it to rest.
I hope this project helped many people get started in bot development, I had fun working on it.

I would recommend using https://github.com/atipugin/telegram-bot-ruby.

TelegramBot

A charismatic ruby client for Telegram's Bot API.

Write your own Telegram Bot using Ruby! Yay!

Currently under heavy development. Please collaborate with your questions, ideas or problems!

Installation

Add this line to your application's Gemfile (currently under development):

ruby
gem 'telegram_bot'

And then execute:

$ bundle

Usage

Here's an example:

ruby
require 'telegram_bot'

bot = TelegramBot.new(token: '[YOUR TELEGRAM BOT TOKEN GOES HERE]')
bot.get_updates(fail_silently: true) do |message|
  puts "@#{message.from.username}: #{message.text}"
  command = message.get_command_for(bot)

  message.reply do |reply|
    case command
    when /greet/i
      reply.text = "Hello, #{message.from.first_name}!"
    else
      reply.text = "#{message.from.first_name}, have no idea what #{command.inspect} means."
    end
    puts "sending #{reply.text.inspect} to @#{message.from.username}"
    reply.send_with(bot)
  end
end

Here's a sample output:

$ bundle exec ruby bot.rb
@eljojo: greet
sending "Hello, José!" to @eljojo
@eljojo: heeeeeeeeya!
sending "José, have no idea what \"heeeeeeeeya!\" means." to @eljojo

How do I get a Bot Token

Talk to the @BotFather. You can find more info here.

What else can it do?

you can pass options to the bot initializer:

ruby
bot = TelegramBot.new(token: 'abc', logger: Logger.new(STDOUT), offset: 123, timeout: 20)

if you don't want to start the loop, don't pass a block to #get_updates and you'll get an array with the latest messages:

ruby
messages = bot.get_updates(timeout: 30, offset: 123)

Because things can go wrong sometimes with the API, there's a fail_silently option that you can pass to #get_updates like this:

ruby
bot.get_updates(fail_silently: true) do |message|
  puts message.text
end

A message has several attributes:

ruby
message = bot.get_updates.last

# message data
message.text # "hello moto"
message.date # Wed, 01 Jul 2015 09:52:54 +0200 (DateTime)

# reading user
message.from # TelegramBot::User
message.from.first_name # "Homer"
message.from.last_name  # "Simpson"
message.from.username   # "mr_x"

# channel
message.channel.id # 123123123 (telegram's id)

# reply
message.reply do |reply|
  reply.text = "homer please clean the garage"
  reply.send_with(bot)
end
# or
reply = message.reply
reply.text = "i'll do it after going to moe's"
bot.send_message(reply)

To send message to specific channel you could do following:

ruby
bot = TelegramBot.new(token: '[YOUR TELEGRAM BOT TOKEN GOES HERE]')
channel = TelegramBot::Channel.new(id: channel_id)
message = TelegramBot::OutMessage.new
message.chat = channel
message.text = 'Some message'

message.send_with(bot)

Also you may pass additional options described in API Docs

ruby
message.parse_mode = 'Markdown'

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/eljojo/telegram_bot. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Issues· 23 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言