Class: BotPlatform::Cli

Inherits:
Object
  • Object
show all
Defined in:
lib/bot_platform/cli.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(instance) ⇒ Cli

Returns a new instance of Cli.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/bot_platform/cli.rb', line 12

def initialize(instance)
  @bot_name = "bot"
  @user_name = "user"
  @bot_instance = instance
  ENV['BOT_CHANNELS'] = 'console'
  
  Signal.trap(:INT) do
    puts "\nbye."
    exit 0
  end
end

Instance Attribute Details

#bot_idObject

Returns the value of attribute bot_id.



10
11
12
# File 'lib/bot_platform/cli.rb', line 10

def bot_id
  @bot_id
end

#bot_instanceObject

Returns the value of attribute bot_instance.



10
11
12
# File 'lib/bot_platform/cli.rb', line 10

def bot_instance
  @bot_instance
end

#bot_nameObject

Returns the value of attribute bot_name.



10
11
12
# File 'lib/bot_platform/cli.rb', line 10

def bot_name
  @bot_name
end

#room_idObject

Returns the value of attribute room_id.



10
11
12
# File 'lib/bot_platform/cli.rb', line 10

def room_id
  @room_id
end

#user_nameObject

Returns the value of attribute user_name.



10
11
12
# File 'lib/bot_platform/cli.rb', line 10

def user_name
  @user_name
end

Instance Method Details

#helpObject



76
77
78
79
80
81
82
83
84
# File 'lib/bot_platform/cli.rb', line 76

def help
  "usage:\nhistory\nexit, quit, \\\\q - close shell and exit\nhelp, \\\\h, \\\\? - print this usage\nclear, \\\\c - clear the terminal screen\n  USAGE\nend\n"

#process(cmd) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/bot_platform/cli.rb', line 52

def process(cmd)
  headers = {"X-Bot-Platform-Bot":@bot_name}
  body = {bot_id:@bot_id, room_id:@room_id}
  if cmd.start_with? '/'
    body[:type] = "cmd_back"
    cmd.slice!(0)
    body[:cmd] = cmd
  else
    body[:type] = "message"
    body[:text] = cmd
  end

  instance = @bot_instance

  BotPlatform::Adapter.instance.process_activity headers,body do |context|
    instance.on_turn context
  end

end

#promptObject



72
73
74
# File 'lib/bot_platform/cli.rb', line 72

def prompt
  return "#{@user_name}> "
end

#runObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/bot_platform/cli.rb', line 24

def run
  puts welcome_message

  opts = GetoptLong.new(
    ['--name', GetoptLong::OPTIONAL_ARGUMENT]
  )
  opts.each do |opt, arg|
    case opt
    when '--name' then @bot_name = arg
    end
  end

  while cmd = Readline.readline(prompt, true)
    case cmd
    when "exit", "quit", '\q' then
      exit
    when "help", '\h', '\?' then
      puts help
    when 'clear', '\cc' then
      system 'clear'
    else # bot incoming activity
      unless cmd.empty?
        process cmd
      end
    end
  end
end

#welcome_messageObject



86
87
88
89
90
91
92
93
94
95
# File 'lib/bot_platform/cli.rb', line 86

def welcome_message
  "Welcome to Bot Platform Command Line Interface (v0.1)\n   \\\\------/\n_ [| o \u03C9 o|] _\n   / |==\\\\__-__/=| \\\\\n\n   powered by Ningfeng Yang\n     WELCOME\nend\n"