class Kybus::Bot::Adapter::Telegram

This adapter is intended to be used on unit tests and development.

Public Class Methods

new(configs) click to toggle source

It receives a hash with the configurations:

  • name: the name of the channel

  • channels a key value, where the key is a name and the value the list of the messages in the channel.

  • echo: a flag to enable debug messages.

# File lib/kybus/bot/adapters/telegram.rb, line 53
def initialize(configs)
  @config = configs
  @client = ::Telegram::Bot::Client.new(@config['token'])
end

Public Instance Methods

mention(id) click to toggle source
# File lib/kybus/bot/adapters/telegram.rb, line 72
def mention(id)
  "[user](tg://user?id=#{id})"
end
read_message() click to toggle source

Interface for receiving message

# File lib/kybus/bot/adapters/telegram.rb, line 59
def read_message
  # take the first message from the first open message,
  loop do
    @client.listen do |message|
      log_info('Received message', message: message.to_h,
                                   from: message.from.to_h)
      return TelegramMessage.new(message)
    end
  rescue Telegram::Bot::Exceptions::ResponseError => e
    log_error('An error ocurred while calling to Telegram API', e)
  end
end
send_audio(channel_name, audio_url) click to toggle source

interface for sending uadio

# File lib/kybus/bot/adapters/telegram.rb, line 92
def send_audio(channel_name, audio_url)
  file = Faraday::UploadIO.new(audio_url, 'audio/mp3')
  @client.api.send_audio(chat_id: channel_name, audio: file)
end
send_image(channel_name, image_url) click to toggle source

interface for sending image

# File lib/kybus/bot/adapters/telegram.rb, line 98
def send_image(channel_name, image_url)
  file = Faraday::UploadIO.new(image_url, 'image/jpeg')
  @client.api.send_photo(chat_id: channel_name, photo: file)
end
send_message(channel_name, contents) click to toggle source

interface for sending messages

# File lib/kybus/bot/adapters/telegram.rb, line 78
def send_message(channel_name, contents)
  puts "#{channel_name} => #{contents}" if @config['debug']
  @client.api.send_message(chat_id: channel_name, text: contents)
rescue Telegram::Bot::Exceptions::ResponseError => err
  return if err[:error_code] == '403'
end
send_video(channel_name, video_url) click to toggle source

interface for sending video

# File lib/kybus/bot/adapters/telegram.rb, line 86
def send_video(channel_name, video_url)
  file = Faraday::UploadIO.new(video_url, 'video/mp4')
  @client.api.send_video(chat_id: channel_name, audio: file)
end