Commit d76afe94 authored by Jyrki's avatar Jyrki 🐾

Initial Telegram bot skeleton

parent c1cef038
version: 1.0
shards:
telegram_bot:
github: hangyas/telegram_bot
commit: 722bab24876d13a661f513b09c4569916f7a81c1
......@@ -6,7 +6,11 @@ authors:
targets:
bd_notifier:
main: src/bd_notifier.cr
main: src/main.cr
dependencies:
telegram_bot:
github: hangyas/telegram_bot
crystal: 0.26.1
......
require "./commands/start_bot"
require "logger"
class Application
COMMANDS = {
"start" => Commands::StartBot
}
def self.run(argv)
new(argv.dup).run
end
def self.logger
@@logger ||= Logger.new(
STDOUT, level: Logger::Severity.parse(ENV.fetch("LOG_LEVEL", "INFO"))
)
end
def initialize(@argv : Array(String))
end
def run
exit_with_usage if @argv.empty?
command = @argv.shift.downcase.strip
exit_with_usage unless COMMANDS.has_key?(command)
COMMANDS[command].call(@argv)
end
private def exit_with_usage(status = 1)
usage
exit status
end
private def usage
puts <<-EOF
Usage: #{PROGRAM_NAME} [command]
Commands:
EOF
filler_size = COMMANDS.keys.map(&.size).max + 3
COMMANDS.each do |command, klass|
filler = " " * (filler_size - command.size)
puts [" ", command, filler, klass.description].join("")
end
puts <<-EOF
Configuration is handled via the following environment variables:
ENABLE_WEBHOOK If set, start up a server for the webhook.
LOG_LEVEL The log level.
Allowed values: DEBUG, INFO, WARN, ERROR, FATAL
Default: INFO
TELEGRAM_API_TOKEN The API token for accessing Telegram's bot API.
WEBHOOK_HOST The host address for the webhook server to bind to.
Default: 0.0.0.0
WEBHOOK_PORT The port for the webhook server to bind to.
Default: 8080
EOF
end
end
# TODO: Write documentation for `BdNotifier`
module BdNotifier
VERSION = "0.1.0"
# TODO: Put your code here
end
require "telegram_bot"
require "./application"
require "./errors"
require "./use_case/slash_commands/start"
require "./use_case/slash_commands/help"
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
end
def start
logger.info "Bot is starting up"
raise Errors::GenericError.new("Webhook is not supported yet") if ENV.has_key?("ENABLE_WEBHOOK")
logger.info "Switching to polling mode"
polling
end
end
require "colorize"
require "../errors"
module Commands
abstract class Base
def self.call(argv)
new(argv).call
rescue e : Errors::Base
STDERR.print "#{e.human_name}: ".colorize(:light_red)
STDERR.puts e.message
exit 2
end
def self.description
""
end
def initialize(@argv : Array(String))
end
abstract def call
end
end
require "./base"
require "../bot"
require "../errors"
module Commands
class StartBot < Base
def self.description
"Starts the telegram bot."
end
def call
check_sanity!
Bot.new.start
end
private def check_sanity!
unless ENV.has_key?("TELEGRAM_API_TOKEN")
raise Errors::ConfigurationError.new("TELEGRAM_API_TOKEN is not set")
end
end
end
end
module Errors
class Base < Exception
def human_name
self.class.name.sub("Errors::", "").scan(/([A-Z][a-z]*)/).map(&.[1]).join(" ")
end
end
class GenericError < Base; end
class ConfigurationError < Base; end
end
require "./application"
Application.run(ARGV)
module UseCase
abstract class Base
def self.call(*args)
new.call(*args)
end
abstract def call(*args)
end
end
require "telegram_bot"
require "../base"
module UseCase
module SlashCommands
abstract class Base < UseCase::Base
abstract def call(bot : TelegramBot::Bot, msg : TelegramBot::Message, params : Array(String)?)
end
end
end
require "telegram_bot"
require "./base"
module UseCase
module SlashCommands
class Help < SlashCommands::Base
def call(bot, msg, params)
bot.reply msg, <<-EOF
Right now, I only know about this one command here. Sorry to disappoint.
EOF
end
end
end
end
require "./base"
module UseCase
module SlashCommands
class Start < SlashCommands::Base
def call(bot, msg, params)
bot.reply msg, <<-EOF
Welcome to the Dragon Dick Watchbot!
I periodically monitor Bad Dragon's clearance section and can notify you if a toy you want is in there.
To get started, type /help for a list of commands.
EOF
end
end
end
end
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