Implement method to process updates.

This commit is contained in:
Danila Fedorin 2018-01-12 20:56:04 -08:00
parent cb1c4f2e54
commit d02b25d026

View File

@ -8,8 +8,8 @@ module Telepathy
@this_user = get_me @this_user = get_me
@last_update_id = uninitialized Int64? @last_update_id = uninitialized Int64?
@last_update_id = nil @last_update_id = nil
@command_hooks = {} of String => Update, Array(String) -> String @command_hooks = {} of String => Update, Array(String) -> Void
@message_hooks = [] of Update -> String @message_hooks = [] of Update -> Void
end end
def get_me def get_me
@ -29,11 +29,29 @@ module Telepathy
return updates return updates
end end
def command(command_name, &block: Update, Array(String) -> String) def process_updates(updates)
updates.each do |update|
if message = update.message
@message_hooks.each &.call(update)
if entity = message.entities.try { |it| it.first? }
text = message.text.as String
if entity.offset == 0 && entity.type == "bot_command"
divider_index = (text.index /\s|@/) || text.size
command = text[1...divider_index]
remaining = text[divider_index..text.size]
params = remaining.empty? ? ([] of String) : (remaining[1...remaining.size].split ' ')
@command_hooks[command]?.try { |command| command.call(update, params) }
end
end
end
end
end
def command(command_name, &block: Update, Array(String) -> Void)
@command_hooks[command_name] = block @command_hooks[command_name] = block
end end
def message(&block: Update -> String) def message(&block: Update -> Void)
@message_hooks.push(block) @message_hooks.push(block)
end end
end end