# Somewhere in src/handlers ...
require "tourmaline"

module Tourmaline  
  # Sample bot
  class EchoBot < Tourmaline::Client
    @[Command("echo")]
    def echo_command(ctx)
      ctx.message.reply(ctx.text)
    end
  end

  # Tourmaline Handler
  class TMBotHandler
    include HTTP::Handler

    property bot : Tourmaline::Client
    property path : String

    # Handle requests with given path 
    def initialize(path = nil)
      @bot = EchoBot.new(bot_token: ENV["BOT_TOKEN"])
      @path = path || "/webhook/#{bot.bot.username}"

      # See original Kemal handler for details
      # check_config if Lucky::Env.production?
    end

    # Just match path and method
    def only_match?(context)
      context.request.path == @path && context.request.method == "POST"
    end

    def call(context)
      return call_next(context) unless only_match?(context)
      if body = context.request.body
        # Extract data from Tgm
        update = Tourmaline::Update.from_json(body)
        # Send update to your bot 
        @bot.handle_update(update)
      end
    end
  end
end