class Rikutelebot::Bot
Public Class Methods
new() { |conf| ... }
click to toggle source
# File lib/rikutelebot.rb, line 31 def initialize @conf = Config.new @state = false @commands = [] yield @conf @conf.offset = @conf.io.get_key 'offset' end
Public Instance Methods
commands()
click to toggle source
# File lib/rikutelebot.rb, line 98 def commands end
get_command(text)
click to toggle source
# File lib/rikutelebot.rb, line 44 def get_command text c = text.chars return nil if c.first != '/' c.shift buf = '' while c.first != ' ' and c.first != '@' and !c.empty? buf += c.shift end return buf end
on(command, &block)
click to toggle source
# File lib/rikutelebot.rb, line 39 def on(command, &block) @commands << Command.new(command, block) self end
on_message() { |message(usr, chat, update['text'])| ... }
click to toggle source
# File lib/rikutelebot.rb, line 70 def on_message updates = [] j = JSON.parse open(@conf.getupdatesurl).read() return if j['result'].length == 0 or j.nil? for update in j['result'] begin fn = update['message']['from']['first_name'] ln = update['message']['from']['last_name'] id = update['message']['from']['id'] un = update['message']['from']['username'] usr = User.new(fn, ln, id, un) id = update['message']['chat']['id'] title = update['message']['chat']['title'] un = update['message']['chat']['username'] || "(#{id}:#{title})" chat = Chat.new(id, title, un) yield Message.new(usr, chat, update['message']['text']) unless update['message']['text'].nil? rescue next end end @conf.offset = j['result'].last['update_id'] + 1 @conf.io.set_key 'offset', @conf.offset.to_s end
run!()
click to toggle source
# File lib/rikutelebot.rb, line 55 def run! loop do on_message do |message| a = get_command(message.text) next if a.nil? Many.new( @commands.select do |cmd| cmd.command == a end ).block.call(message).values end sleep 0.1 end end