-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.rb
executable file
·70 lines (55 loc) · 1.58 KB
/
bot.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env ruby
#
#
require "./lib/greenbot.rb"
require 'mongo'
require 'awesome_print'
Mongo::Logger.logger.level = ::Logger::FATAL
MONGO_URL = ENV['MONGO_URL'] || 'mongodb://127.0.0.1:27017/greenbot'
client = Mongo::Client.new(MONGO_URL)
Bots = client[:Bots]
pin = ask("To get started, please provide your PIN")
pin.upcase!
bot = Bots.find({passcode: pin.strip}).first
if bot.nil?
say("I'm sorry, I cannot find that bot.")
exit
end
ALPHA = "ABCDEFGHIJKLMNOP"
loop do
index = 0
bot['settings'].sort! {|x,y| x['name'] <=> y['name']}
bot['settings'].each do |s|
s['menu_choice'] = ALPHA[index]
index += 1
end
menu_choices = ''
bot['settings'].each do |s|
menu_choices << s['menu_choice'] + ':' + s['name'] + " "
end
menu_choices << "Q:quit"
choice = ask("Please pick the setting to change: #{menu_choices}")
choice.strip!
choice.upcase!
break if choice == 'Q'
setting = bot['settings'].find do |s|
s['menu_choice'].eql?(choice)
end
if setting.nil?
say("I could not find that setting")
else
change = confirm("The current value is: #{setting['value']}. Change it?")
if change
new_val = ask("Please send a single message with the new value.")
new_val.strip!
unless new_val.empty?
Bots.update_one({"$and" => [{ "passcode" => pin.strip}, {"settings.name" => setting['name'] }]}, { '$set' => { "settings.$.value" => new_val }} )
say('Setting updated.')
end
else
say("Not updating value")
end
end
bot = Bots.find({passcode: pin.strip}).first
end
say("Thank you. Session ended.")