Class: Telepost

Inherits:
Object
  • Object
show all
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 © 2018-2026 Yegor Bugayenko

License

MIT

Defined Under Namespace

Classes: CantPost, Fake

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token, chats: []) ⇒ Telepost

Makes a new object. To obtain a token you should talk to the @BotFather in Telegram.

Parameters:

  • token (String)

    Telegram bot token

  • chats (Array<Integer>) (defaults to: [])

    Optional list of chat IDs



68
69
70
71
72
# File 'lib/telepost.rb', line 68

def initialize(token, chats: [])
  @token = token
  @chats = chats
  @bot = Telegram::Bot::Client.new(@token)
end

Instance Attribute Details

#clientTelegram::Bot::Client (readonly)

To make it possible to get the client.

Returns:

  • (Telegram::Bot::Client)

    The Telegram bot client



61
62
63
# File 'lib/telepost.rb', line 61

def client
  @client
end

Instance Method Details

#attach(chat, file, caption: nil, parse_mode: 'Markdown') ⇒ Telegram::Bot::Types::Message

Attach a file (as a Telegram document) 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.

Parameters:

  • chat (Integer, String)

    Chat ID or channel name

  • file (String, File, IO)

    Path to the file or an open IO

  • caption (String, nil) (defaults to: nil)

    Optional caption for the attachment

  • parse_mode (String) (defaults to: 'Markdown')

    Parse mode used for the caption

Returns:

  • (Telegram::Bot::Types::Message)

    The sent message object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/telepost.rb', line 128

def attach(chat, file, caption: nil, parse_mode: 'Markdown')
  document =
    if file.is_a?(String)
      Faraday::UploadIO.new(file, 'application/octet-stream', File.basename(file))
    else
      file
    end
  @bot.api.send_document(
    chat_id: chat,
    document:,
    caption:,
    parse_mode:
  )
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.

Parameters:

  • chat (Integer, String)

    Chat ID or channel name

  • lines (Array<String>)

    Message lines to send

Returns:

  • (Telegram::Bot::Types::Message)

    The sent message object



110
111
112
113
114
115
116
117
# File 'lib/telepost.rb', line 110

def post(chat, *lines, parse_mode: 'Markdown')
  @bot.api.send_message(
    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.

Yields:

  • (Integer, String)

    Yields chat ID and message text

Raises:

  • (RuntimeError)

    If no block is given



80
81
82
83
84
85
86
87
# File 'lib/telepost.rb', line 80

def run
  raise 'Block must be given' unless block_given?
  @bot.listen do |message|
    yield(message.chat.id, message.respond_to?(:text) ? message.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.

Parameters:

  • lines (Array<String>)

    Message lines to send



95
96
97
98
99
# File 'lib/telepost.rb', line 95

def spam(*lines)
  @chats.each do |chat|
    post(chat, *lines)
  end
end