forked from maxbrodi/firstbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pairingsecond.rb
182 lines (149 loc) · 7.23 KB
/
pairingsecond.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
require 'mongo'
require 'slack-ruby-client'
require 'dotenv'
require 'json'
Dotenv.load
def pairingroulette
Slack.configure do |config|
config.token = ENV['SLACK_API_TOKEN']
end
clientyannis = Mongo::Client.new('mongodb://heroku_9lfp2dtb:[email protected]:33734/heroku_9lfp2dtb')
database = clientyannis.database
users = database.collection('users')
mangrovers = users.find.distinct(:id)
hastoredo = true
while hastoredo do
hastoredo = false
shuffler = mangrovers.shuffle
newbuddies = []
number_of_mangrovers = mangrovers.count
if number_of_mangrovers.even?
(1..(number_of_mangrovers-1)).each do |number|
if number % 2 != 0
newbuddies << [shuffler[(number-1)], shuffler[number]]
end
end
else
(1..(number_of_mangrovers-1)).each do |number|
if number % 2 != 0
newbuddies << [shuffler[(number-1)], shuffler[number]]
end
end
newbuddies << [shuffler[(number_of_mangrovers-3)], shuffler[number_of_mangrovers-1]]
end
# check if unique
pairingdb = database.collection('pairing')
lastweekbuddies = pairingdb.find.first[:thisweek]
newbuddies.each do |newarray|
lastweekbuddies.each do |oldarray|
if ( (newarray.sort[0] == oldarray.sort[0]) && (newarray.sort[1] == oldarray.sort[1]) ) then
hastoredo = true
end
end
end
if newbuddies.last[0] == lastweekbuddies.last[0] && !number_of_mangrovers.even? then
hastoredo = true
end
end
# write new buddies in DB
pairingdb.find(:thisweek => lastweekbuddies).replace_one(:thisweek => newbuddies)
# post new buddies to slack
# get messages
file = File.read('pairingmessages.json')
pairingmessages = JSON.parse(file)
newclient = Slack::Web::Client.new
p newclient
newclient.auth_test
newclient.chat_postMessage(token: nil, channel: "#general", text: pairingmessages["hello"].sample, as_user: true)
newclient.chat_postMessage(token: nil, channel: "#general", text: pairingmessages["announcement"].sample, as_user: true)
announce = ""
newbuddies.each do |couple|
announce += "> :point_right: " + newclient.users_info(user: couple[0]).user.name + " and " + newclient.users_info(user: couple[1]).user.name + "\n"
end
newclient.chat_postMessage(token: nil, channel: "#general", text: announce, as_user: true)
newclient.chat_postMessage(token: ENV["SLACK_API_TOKEN"], channel: "#general", text: pairingmessages["goodbye"].sample, as_user: true)
end
def reminder
Slack.configure do |config|
config.token = ENV['SLACK_API_TOKEN']
end
# get buddies
clientyannis = Mongo::Client.new('mongodb://heroku_9lfp2dtb:[email protected]:33734/heroku_9lfp2dtb')
database = clientyannis.database
pairingdb = database.collection('pairing')
buddies = pairingdb.find.first[:thisweek]
# get mangrovers
users = database.collection('users')
# get messages
file = File.read('pairingmessages.json')
pairingmessages = JSON.parse(file)
newclient = Slack::Web::Client.new
allbuddies = []
buddies.each do |buddy|
if allbuddies.include? buddy[0]
sleep(rand(60))
newclient.chat_postMessage(token: ENV["SLACK_API_TOKEN"], channel: buddy[0], text: "Oh I almost forgot! Don\'t forget to call " + newclient.users_info(user: buddy[1]).user.name + " as well! :smile_cat:" , as_user: true)
else
allbuddies << buddy[0]
newclient.chat_postMessage(token: ENV["SLACK_API_TOKEN"], channel: buddy[0], text: "Hi " + newclient.users_info(user: buddy[0]).user.name + "!", as_user: true)
newclient.chat_postMessage(token: ENV["SLACK_API_TOKEN"], channel: buddy[0], text: pairingmessages["greetings"].sample, as_user: true)
newclient.chat_postMessage(token: ENV["SLACK_API_TOKEN"], channel: buddy[0], text: "Just to tell you that your buddy this week is " + newclient.users_info(user: buddy[1]).user.name + " :dancers:", as_user: true)
buddymood = users.find( { id: buddy[1] } ).first["feedback"].last["status"].to_i
if buddymood < 4
newclient.chat_postMessage(token: ENV["SLACK_API_TOKEN"], channel: buddy[0], text: pairingmessages["feelingbad"].sample, as_user: true)
elsif buddymood > 8
if rand > 0.3
newclient.chat_postMessage(token: ENV["SLACK_API_TOKEN"], channel: buddy[0], text: pairingmessages["feelinggreat"].sample, as_user: true)
end
end
if rand > 0.4
newclient.chat_postMessage(token: ENV["SLACK_API_TOKEN"], channel: buddy[0], text: pairingmessages["besttimes"].sample, as_user: true)
end
newclient.chat_postMessage(token: ENV["SLACK_API_TOKEN"], channel: buddy[0], text: pairingmessages["goodbyepm"].sample, as_user: true)
end
end
allbuddies = []
buddies.each do |buddy|
if allbuddies.include? buddy[1]
sleep(rand(60))
newclient.chat_postMessage(token: ENV["SLACK_API_TOKEN"], channel: buddy[1], text: "Oh I almost forgot! Don\'t forget to call " + newclient.users_info(user: buddy[0]).user.name + " as well! :smile_cat:" , as_user: true)
else
allbuddies << buddy[1]
newclient.chat_postMessage(token: ENV["SLACK_API_TOKEN"], channel: buddy[1], text: "Hi " + newclient.users_info(user: buddy[1]).user.name + "!", as_user: true)
newclient.chat_postMessage(token: ENV["SLACK_API_TOKEN"], channel: buddy[1], text: pairingmessages["greetings"].sample, as_user: true)
newclient.chat_postMessage(token: ENV["SLACK_API_TOKEN"], channel: buddy[1], text: "Just to tell you that your buddy this week is " + newclient.users_info(user: buddy[0]).user.name + " :dancers:", as_user: true)
buddymood = users.find( { id: buddy[1] } ).first["feedback"].last["status"].to_i
if buddymood < 4
newclient.chat_postMessage(token: ENV["SLACK_API_TOKEN"], channel: buddy[1], text: pairingmessages["feelingbad"].sample, as_user: true)
elsif buddymood > 8
if rand > 0.3
newclient.chat_postMessage(token: ENV["SLACK_API_TOKEN"], channel: buddy[1], text: pairingmessages["feelinggreat"].sample, as_user: true)
end
end
if rand > 0.4
newclient.chat_postMessage(token: ENV["SLACK_API_TOKEN"], channel: buddy[1], text: pairingmessages["besttimes"].sample, as_user: true)
end
newclient.chat_postMessage(token: ENV["SLACK_API_TOKEN"], channel: buddy[1], text: pairingmessages["goodbyepm"].sample, as_user: true)
end
end
end
def surprise
Slack.configure do |config|
config.token = ENV['SLACK_API_TOKEN']
end
# get buddies
clientyannis = Mongo::Client.new('mongodb://heroku_9lfp2dtb:[email protected]:33734/heroku_9lfp2dtb')
database = clientyannis.database
pairingdb = database.collection('pairing')
buddies = pairingdb.find.first[:thisweek]
# get messages
file = File.read('pairingmessages.json')
pairingmessages = JSON.parse(file)
newclient = Slack::Web::Client.new
buddies.each do |buddy|
if rand < 0.2
newclient.chat_postMessage(token: ENV["SLACK_API_TOKEN"], channel: buddy[0], text: "Hi " + newclient.users_info(user: buddy[0]).user.name + "!", as_user: true)
newclient.chat_postMessage(token: ENV["SLACK_API_TOKEN"], channel: buddy[0], text: pairingmessages["randomlove"].sample, as_user: true)
end
end
end