Class: BotPlatform::Channels::Console

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/bot_platform/channels/console.rb

Instance Method Summary collapse

Instance Method Details

#as_command(headers, body) ⇒ Object



54
55
56
57
# File 'lib/bot_platform/channels/console.rb', line 54

def as_command(headers, body)
  return body[:cmd] if body[:type] == "cmd_back"
  return false
end

#channel_idObject



7
8
9
# File 'lib/bot_platform/channels/console.rb', line 7

def channel_id
  "console"
end

#keyObject



11
12
13
# File 'lib/bot_platform/channels/console.rb', line 11

def key
  "X-Bot-Platform-Bot".intern
end

#match_request(headers, body) ⇒ Object



15
16
17
18
# File 'lib/bot_platform/channels/console.rb', line 15

def match_request(headers, body)
  return false if headers.nil?
  return !(headers[key].nil? || headers[key].empty?)
end

#parse_incoming_to_activity(headers, body) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/bot_platform/channels/console.rb', line 40

def parse_incoming_to_activity(headers, body)
  user_id = body[:bot_id] || ""
  room_id = body[:room_id] || ""
  activity = nil
  cmd = as_command(headers, body)
  if cmd
    activity = BotPlatform::Activity.new ::BotPlatform::Activity::TYPES[:command], {from: {user_id: user_id, room_id: room_id}, text: cmd, channel_id: channel_id}
  else
    activity = BotPlatform::Activity.new ::BotPlatform::Activity::TYPES[:message], {from: {user_id: user_id, room_id: room_id}, text: body[:text], channel_id: channel_id}
  end
  
  return activity
end

#send_activity(activity) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/bot_platform/channels/console.rb', line 20

def send_activity(activity)
  case activity.type
  when BotPlatform::Activity::TYPES[:message] then
    puts "bot> #{activity.text}"
  when BotPlatform::Activity::TYPES[:carousel] then
    puts "bot> select from the list:"
    content = activity.content
    content[:columns].each_with_index do |col, idx|
      puts "#{idx+1}: #{col[:title]}(#{col[:text]}) [/#{col[:defaultAction][:data]}]"
    end
  when BotPlatform::Activity::TYPES[:options] then
    puts "bot> #{activity.text}"
    activity.options.each_with_index{|opt, idx| puts "#{idx+1}: #{opt} [/#{activity.prefix}-opt-#{idx}]"}
  when BotPlatform::Activity::TYPES[:image] then
    `open -a '/Applications/Google Chrome.app' #{activity.resource_url}`
  else
    puts "bot[debug]> activity.inspect"
  end
end