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 ([email protected])
- Copyright
-
Copyright © 2018-2025 Yegor Bugayenko
- License
-
MIT
Defined Under Namespace
Instance Attribute Summary collapse
-
#client ⇒ Telegram::Bot::Client
readonly
To make it possible to get the client.
Instance Method Summary collapse
-
#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.
62 63 64 65 66 |
# File 'lib/telepost.rb', line 62 def initialize(token, chats: []) @token = token @chats = chats @bot = Telegram::Bot::Client.new(@token) end |
Instance Attribute Details
#client ⇒ Telegram::Bot::Client (readonly)
To make it possible to get the client.
55 56 57 |
# File 'lib/telepost.rb', line 55 def client @client end |
Instance Method Details
#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.
104 105 106 107 108 109 110 111 |
# File 'lib/telepost.rb', line 104 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.
74 75 76 77 78 79 80 81 |
# File 'lib/telepost.rb', line 74 def run raise 'Block must be given' unless block_given? @bot.listen do || 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.
89 90 91 92 93 |
# File 'lib/telepost.rb', line 89 def spam(*lines) @chats.each do |chat| post(chat, *lines) end end |