Module: TelegramSimpleMessenger

Defined in:
lib/telegram_simple_messenger.rb,
lib/telegram_simple_messenger/version.rb

Overview

Send a message to a Telegram chat with a single line of code.

You can set a default API key and chat ID:

TelegramSimpleMessenger.default_api = "123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11"
TelegramSimpleMessenger.default_chat_id = "123456789"

Example:

TelegramSimpleMessenger.send_message("Hello, world!")

You can also pass the API key and chat ID as arguments:

TelegramSimpleMessenger.send_message("Hello, world!", "123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11", "123456789")

Defined Under Namespace

Classes: Error

Constant Summary collapse

VERSION =
"0.1.2"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.default_api_keyObject

Returns the value of attribute default_api_key.



21
22
23
# File 'lib/telegram_simple_messenger.rb', line 21

def default_api_key
  @default_api_key
end

.default_chat_idObject

Returns the value of attribute default_chat_id.



21
22
23
# File 'lib/telegram_simple_messenger.rb', line 21

def default_chat_id
  @default_chat_id
end

Class Method Details

.send_message(message, api_key = default_api_key, chat_id = default_chat_id) ⇒ Object

Raises:



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/telegram_simple_messenger.rb', line 27

def self.send_message(message, api_key = default_api_key, chat_id = default_chat_id)
  return "API key and chat ID are required" unless api_key && chat_id

  response = HTTParty.post("https://api.telegram.org/bot#{api_key}/sendMessage",
                           body: {
                             chat_id: chat_id,
                             text: message
                           })

  raise Error, "Failed to send message: #{response.body}" unless response.success?

  response
end