This is a fully working example of a bot which allows you to book an appointment with a doctor.
The following diagram will help you understand the bot's flow:
First, open main.rb and configure the bot with your API token:
TelegramWorkflow.configure do |config|
- config.api_token = <YOUR_TOKEN>
+ config.api_token = "123456780:ABCDE_my-token"
endNext, run the bot:
bundle
ruby main.rb
Every Bot workflow begins with on_message callback in a start_action.
There's no need to store current user data in session in this example, so we simply redirect to the ListActions action, which will be our "main" action.
class Actions::Start < TelegramWorkflow::Action
def initial
on_message do
redirect_to Actions::ListActions
end
end
endNext, the Telegram client can be customized. We want to use Telegram's InlineKeyboard to provide a user with a list of available actions.
Let's encapsulate this inside our custom client class:
class Client < TelegramWorkflow::Client
def send_actions(message, actions)
send_message text: message, reply_markup: { inline_keyboard: actions }
end
endNow, let's configure the gem:
TelegramWorkflow.configure do |config|
config.start_action = Actions::Start
config.client = Client
config.session_store = TelegramWorkflow::Stores::File.new
endThe last configuration parameter here is session_store. We are using TelegramWorkflow::Stores::File - a built-in implementation of persistent file store.
getUpdates method is used in this example to receive the updates:
TelegramWorkflow.updates.each do |params|
TelegramWorkflow.process(params)
endAfter a user has sent a message, TelegramWorkflow.process will initialize the last processed action and step and then call on_message callback on it.
Check out the bot's code under actions folder.
