Class: Beeline::Bot

Inherits:
Client show all
Defined in:
lib/beeline/bot.rb

Constant Summary

Constants inherited from Client

Client::INITIAL_LATCH, Client::MAX_LATCH

Instance Attribute Summary collapse

Attributes inherited from Client

#commands, #messages

Instance Method Summary collapse

Methods inherited from Client

#initialize, #ping, #reset_session, #run, #run_loop

Methods included from Config

#friendships, #hive_account, #hive_posting_wif, #hive_public_key

Constructor Details

This class inherits a constructor from Beeline::Client

Instance Attribute Details

#prefixObject (readonly)

Returns the value of attribute prefix.



3
4
5
# File 'lib/beeline/bot.rb', line 3

def prefix
  @prefix
end

Instance Method Details

#command(name, options = {}, &block) ⇒ Object



5
6
7
# File 'lib/beeline/bot.rb', line 5

def command name, options = {}, &block
  commands[name] = {options: options, block: block}
end

#message(pattern, options = {}, &block) ⇒ Object



9
10
11
# File 'lib/beeline/bot.rb', line 9

def message pattern, options = {}, &block
  messages[pattern] = {options: options, block: block}
end

#process_chat_message(payload) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/beeline/bot.rb', line 21

def process_chat_message(payload)
  from = payload['from']
  
  cooldown(from)
  
  conversation_id = payload['conversation_id']
  content = payload['content'].to_s
  command_key = content.split(' ').first.split(prefix).last.to_sym
  reply = if commands.keys.include? command_key
    args = (content.split(' ') - ["#{prefix}#{command_key}"]).join(' ')
    args = args.empty? ? nil : args
    
    commands[command_key][:block].call(args, from, conversation_id)
  elsif (matching_messages = messages.select{|k| Regexp.new(k).match?(content)}).any?
    message = matching_messages.values.first # match in order of declaration
    
    message[:block].call(content, from, conversation_id)
  end
  
  if !!reply
    chat_message(conversation_id, from, reply)
  end
end

#process_friendship_requested(payload) ⇒ Object



45
46
47
48
49
# File 'lib/beeline/bot.rb', line 45

def process_friendship_requested(payload)
  return unless friendships[:accept] == 'auto'
  
  accept_pending_friend_requests
end

#process_status(payload) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/beeline/bot.rb', line 13

def process_status(payload)
  if payload['authenticated']
    puts 'Got acknowledge authenticated.'
  else
    abort 'Unable to authenticate.'
  end
end