Skip to content

Customization

Roman Samoilov edited this page Jul 21, 2023 · 4 revisions

Logger

This allows to customize the gem's log output.

TelegramWorkflow.configure do |config|
  config.logger = Logger.new("log/telegram.log")
end

Session Store

This allows to customize the place where all the session data is being saved.

TelegramWorkflow.configure do |config|
  config.session_store = TelegramWorkflow::Stores::InMemoryStore
end

It accepts any object that responds to read(key) and write(key, value) methods. Refer to default InMemoryStore class definition.

Client

Use this customization to abstract your action's code from the Telegram API implementation details.

Create a customized client:

class MyClient < TelegramWorkflow::Client
  def send_prize_location(user)
    # this is an example call
    prize = user.find_last_prize

    send_venue latitude: prize.latitude,
      longitude: prize.longitude,
      address: prize.address
      title: "Collect the last prize here!",
      reply_markup: { keyboard: [[{ text: "Give me a hint" }], [{ text: "Give me anohter hint" }]] }
  end
end

Now, configure the gem to use the customized client:

TelegramWorkflow.configure do |config|
  config.client = MyClient
end

Then, in your action:

class FindPrize < TelegramWorkflow::Action
  def initial
    on_redirect do
      client.send_prize_location(current_user)
    end
  end
end

Client can be any class that responds to new(chat_id) method.

Previous: Getting Updates Next: Params

Clone this wiki locally