Class: Telepost
- Inherits:
-
Object
- Object
- Telepost
- Defined in:
- lib/telepost.rb
Overview
Telepost is a simple gateway to Telegram, which can post messages and respond to primitive requests:
require 'telepost' tp = Telepost.new('... secret token ...') tp.run do |chat, msg|
Reply to the message via tp.post(msg, chat)
end
For more information read README file.
- Author
Yegor Bugayenko (yegor256@gmail.com)
- Copyright
Copyright (c) 2018-2026 Yegor Bugayenko
- License
MIT
Defined Under Namespace
Constant Summary collapse
- PHOTO_EXTENSIONS =
%w[.jpg .jpeg .png .gif .webp].freeze
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
Instance Method Summary collapse
-
#attach(chat, file, caption: nil, parse_mode: 'Markdown') ⇒ Telegram::Bot::Types::Message
Attach a file to the chat.
-
#initialize(token, chats: []) ⇒ Telepost
constructor
Makes a new object.
-
#post(chat, *lines, parse_mode: 'Markdown') ⇒ Telegram::Bot::Types::Message
Post a single message to the designated chat room.
-
#run {|Integer, String| ... } ⇒ void
You can run a chat bot to listen to the messages coming to it, in a separate thread.
-
#spam(*lines) ⇒ void
Send the message (lines will be concatenated with a space between them) to the chats provided in the constructor and encapsulated.
Constructor Details
#initialize(token, chats: []) ⇒ Telepost
Makes a new object. To obtain a token you should talk to the @BotFather in Telegram.
76 77 78 79 80 |
# File 'lib/telepost.rb', line 76 def initialize(token, chats: []) @token = token @chats = chats @bot = Telegram::Bot::Client.new(@token) end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
69 70 71 |
# File 'lib/telepost.rb', line 69 def client @client end |
Instance Method Details
#attach(chat, file, caption: nil, parse_mode: 'Markdown') ⇒ Telegram::Bot::Types::Message
Attach a file to the chat. The file argument can either be a
path (String) or an open IO/File. The filename shown in Telegram
comes from the basename of the path. A file with an image
extension goes out as a photo via sendPhoto, so Telegram shows
it inline, and everything else as a document via sendDocument.
When file is an Array, all of its items are posted as a single
grouped message (a Telegram "album") via sendMediaGroup. Items
with an image extension are wrapped in InputMediaPhoto, the rest
in InputMediaDocument. The caption is attached to the first
item only, so the album shows one caption.
141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/telepost.rb', line 141 def attach(chat, file, caption: nil, parse_mode: 'Markdown') return album(chat, file, caption:, parse_mode:) if file.is_a?(Array) io = upload(file) if photo?(file) @bot.api.send_photo(chat_id: chat, photo: io, caption:, parse_mode:) else @bot.api.send_document(chat_id: chat, document: io, caption:, parse_mode:) end ensure io.close if file.is_a?(String) && io.respond_to?(:close) end |
#post(chat, *lines, parse_mode: 'Markdown') ⇒ Telegram::Bot::Types::Message
Post a single message to the designated chat room. The chat argument can either be an integer, if you know the chat ID, or the name of the channel (your bot has to be the admin there). The lines provided will be concatenated with a space between them.
120 121 122 |
# File 'lib/telepost.rb', line 120 def post(chat, *lines, parse_mode: 'Markdown') @bot.api.(chat_id: chat, parse_mode:, disable_web_page_preview: true, text: lines.join(' ')) end |
#run {|Integer, String| ... } ⇒ void
This method returns an undefined value.
You can run a chat bot to listen to the messages coming to it, in a separate thread.
88 89 90 91 92 93 94 95 96 97 |
# File 'lib/telepost.rb', line 88 def run raise(RuntimeError, 'Block must be given') unless block_given? @bot.listen do || next unless .respond_to?(:chat) next if .chat.nil? yield(.chat.id, .respond_to?(:text) ? .text : '') end rescue Net::OpenTimeout retry end |
#spam(*lines) ⇒ void
This method returns an undefined value.
Send the message (lines will be concatenated with a space between them) to the chats provided in the constructor and encapsulated.
105 106 107 108 109 |
# File 'lib/telepost.rb', line 105 def spam(*lines) @chats.each do |chat| post(chat, *lines) end end |