Commit 22d6c880 authored by Jyrki's avatar Jyrki 🐾

Refactor slash commands handling

parent fbec82db
......@@ -2,24 +2,16 @@ require "telegram_bot"
require "./application"
require "./errors"
require "./use_case/slash_commands/start"
require "./use_case/slash_commands/help"
require "./use_case/slash_commands"
class Bot < TelegramBot::Bot
include TelegramBot::CmdHandler
macro register_command(name)
cmd "{{ name }}" do |msg, params|
UseCase::SlashCommands::{{ name.name.capitalize }}.call(self, msg, params)
end
end
def initialize
@logger = Application.logger
super("DragonDickWatchbot", ENV["TELEGRAM_API_TOKEN"])
register_command start
register_command help
register_commands
end
def start
......@@ -29,4 +21,12 @@ class Bot < TelegramBot::Bot
logger.info "Switching to polling mode"
polling
end
private def register_commands
{{ UseCase::SlashCommands::Base.subclasses }}.each do |klass|
cmd klass.command_name do |msg, params|
klass.call(self, msg, params)
end
end
end
end
require "./slash_commands/start"
require "./slash_commands/help"
......@@ -5,6 +5,10 @@ require "../base"
module UseCase
module SlashCommands
abstract class Base < UseCase::Base
def self.command_name
raise NotImplementedError.new("Implement #{self.class}.command_name!")
end
abstract def call(bot : TelegramBot::Bot, msg : TelegramBot::Message, params : Array(String)?)
end
end
......
......@@ -5,6 +5,10 @@ require "./base"
module UseCase
module SlashCommands
class Help < SlashCommands::Base
def self.command_name
"help"
end
def call(bot, msg, params)
bot.reply msg, <<-EOF
Right now, I only know about this one command here. Sorry to disappoint.
......
......@@ -3,6 +3,10 @@ require "./base"
module UseCase
module SlashCommands
class Start < SlashCommands::Base
def self.command_name
"start"
end
def call(bot, msg, params)
bot.reply msg, <<-EOF
Welcome to the Dragon Dick Watchbot!
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment