class Pug::TelegramClient

The client for Telegram interactions

Public Class Methods

new(token, chat_id) click to toggle source

@param token [String] API token for Telegram bot @param chat_id [String] Chat id for Telegram bot

# File lib/pug/telegram_client.rb, line 10
def initialize(token, chat_id)
  @token = token
  @chat_id = chat_id
end

Public Instance Methods

configure_keyboard(keyboard_markup) click to toggle source

Configures keyboard with provided markup This can be useful to make shortcuts for Commands @param keyboard_markup [Array<Array<String>>]

A 2D array of strings used to populate on the keyboard
# File lib/pug/telegram_client.rb, line 19
def configure_keyboard(keyboard_markup)
  @keyboard_markup = keyboard_markup || []
end
listen() { |text| ... } click to toggle source

Override of {Interfaces::Client#listen} @yieldparam [String] text

# File lib/pug/telegram_client.rb, line 25
def listen
  perform_with_bot do |bot|
    bot.listen do |message|
      next if message.nil?
      text = message.text
      next if text.nil?
      yield text
    end
  end
end
send_message(message) click to toggle source

Override of {Interfaces::Client#send_message} @return [void]

# File lib/pug/telegram_client.rb, line 38
def send_message(message)
  return if message.to_s.empty?
  send_telegram_message(message)
end

Private Instance Methods

client() click to toggle source
# File lib/pug/telegram_client.rb, line 62
def client
  return @client if @client
  raise 'No Telegram token provided' if @token.to_s.empty?
  @client = Telegram::Bot::Client.new(@token)
end
perform_with_bot() { |client| ... } click to toggle source
# File lib/pug/telegram_client.rb, line 68
def perform_with_bot
  yield client
rescue StandardError => ex
  puts 'Error performing task with Telegram'
  puts ex
  puts ex.backtrace
end
reply_markup() click to toggle source
# File lib/pug/telegram_client.rb, line 56
def reply_markup
  Telegram::Bot::Types::ReplyKeyboardMarkup.new(
    keyboard: @keyboard_markup || []
  )
end
send_telegram_message(message) click to toggle source
# File lib/pug/telegram_client.rb, line 45
def send_telegram_message(message)
  perform_with_bot do |bot|
    bot.api.send_message(
      chat_id: @chat_id,
      text: message,
      reply_markup: reply_markup,
      disable_web_page_preview: true
    )
  end
end