Class: Lita::Handlers::Kintai

Inherits:
Handler
  • Object
show all
Defined in:
lib/lita/handlers/kintai.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.kintai_from_text(text) ⇒ Object



150
151
152
153
154
# File 'lib/lita/handlers/kintai.rb', line 150

def self.kintai_from_text(text)
  reason = kintai_reason(text)
  time = kintai_time(text)
  "#{reason}のため、#{time}です。"
end

.kintai_reason(text) ⇒ Object



170
171
172
173
174
175
176
177
178
179
# File 'lib/lita/handlers/kintai.rb', line 170

def self.kintai_reason(text)
  if text.match(/電車|列車/)
    return "電車遅延"
  elsif text.match(/体調|痛/)
    return "体調不良"
  elsif text.match(/健康診断|検診|健診/)
    return "健康診断"
  end
  return  "私用"
end

.kintai_time(text) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/lita/handlers/kintai.rb', line 181

def self.kintai_time(text)
  if hm = text.match(/([0-1][0-9]|[2][0-3]):[0-5][0-9]/)
    return "#{hm}頃出社予定"
  elsif min = text.match(/(([0-5])*[0-9])分/)
    return "10:#{min[1].rjust(2, "0")}頃出社予定"
  elsif half = text.match(/([0-1][0-9]|[2][0-3])時半/)
    return "#{half[1]}:30頃出社予定"
  elsif half = text.match(/(\d)時間半/)
    return "#{10+half[1].to_i}:30頃出社予定"
  elsif text.match(/おやすみ|休み|有給|休暇/)
    return "本日お休み"
  end
  return "出社時刻未定"
end

Instance Method Details

#authenticate_infoObject



196
197
198
199
200
201
202
203
# File 'lib/lita/handlers/kintai.rb', line 196

def authenticate_info
  <<-EOS
Authenticate your Google account.
Then tell me the code as follows: `code \#{your_code}`

#{Gmail.authorization_url}
  EOS
end

#code(response) ⇒ Object



45
46
47
48
49
50
# File 'lib/lita/handlers/kintai.rb', line 45

def code(response)
  code = response.matches[0][0]
  Gmail.credentials_from_code(code)

  response.reply("Confirmed")
end

#create_kintai_mail(info) ⇒ Object



102
103
104
105
106
107
108
109
# File 'lib/lita/handlers/kintai.rb', line 102

def create_kintai_mail(info)
  create_mail(
    to: config.mail_to,
    cc: config.mail_cc,
    subject: kintai_subject,
    body: kintai_body(info)
  )
end

#create_mail(to: to, cc: cc, subject: subject, body: body) ⇒ Object



120
121
122
123
124
125
126
127
# File 'lib/lita/handlers/kintai.rb', line 120

def create_mail(to: to, cc: cc, subject: subject, body: body)
  Mail.new do
    to to
    cc cc
    subject subject
    body body
  end
end

#draft(response) ⇒ Object



33
34
35
36
# File 'lib/lita/handlers/kintai.rb', line 33

def draft(response)
  info = response.matches[0][0]
  register_draft(response, info)
end

#kintai(response) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/lita/handlers/kintai.rb', line 25

def kintai(response)
  if Gmail.authorized?
    register_draft(response, kintai_info)
  else
    response.reply(authenticate_info)
  end
end

#kintai_body(info) ⇒ Object



160
161
162
163
164
165
166
167
168
# File 'lib/lita/handlers/kintai.rb', line 160

def kintai_body(info)
  <<-EOS
#{config.template_header}

#{info}

#{config.template_footer}
  EOS
end

#kintai_infoObject



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/lita/handlers/kintai.rb', line 133

def kintai_info
  texts = ""

  mails = Gmail.find_mail(config.query)
  # query の `newer:#{Date.today.strftime("%Y/%m/%d")}` 昨日のも一部返ってくる
  # `newer_than:1d` だと24h以内になるので、ここで今日のだけにする
  mails.select{ |m| m[:date] > Date.today.to_time }.each do |m|
    name = m[:from].split("\"")[1]

    text = m[:subject] + m[:body]
    info = kintai_from_text(text)

    texts << "#{name}さん: #{info}\n"
  end
  texts << config.template_info
end

#kintai_subjectObject



156
157
158
# File 'lib/lita/handlers/kintai.rb', line 156

def kintai_subject
  "#{Date.today.strftime("%m/%d")} (#{%w(      )[Date.today.wday]})#{config.template_subject}"
end

#load_on_start(_payload) ⇒ Object



52
53
54
# File 'lib/lita/handlers/kintai.rb', line 52

def load_on_start(_payload)
  schedule
end

#mail_to_message(mail) ⇒ Object



111
112
113
114
115
116
117
118
# File 'lib/lita/handlers/kintai.rb', line 111

def mail_to_message(mail)
  <<-EOS
To: #{mail.to}
Cc: #{mail.cc}
Subject: #{mail.subject}
#{mail.body.to_s}
  EOS
end

#reaction_added(_payload) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/lita/handlers/kintai.rb', line 76

def reaction_added(_payload)
  case _payload[:name]
  when '+1' then
    if !@@draft.nil? &&
      _payload[:item]["type"] == "message" &&
      _payload[:item]["channel"] == @@draft[:channel] &&
      _payload[:item]["ts"] == @@draft[:ts]
      if Gmail.authorized?
        # TODO: 成功失敗
        send_mail(@@draft[:mail])
        @@draft = nil
        send_message(room: _payload[:item]["channel"],
          message: 'Sent email.')
      else
        send_message(room: _payload[:item]["channel"],
          message: authenticate_info)
      end
    end
  end
end

#register_draft(response, info) ⇒ Object



38
39
40
41
42
43
# File 'lib/lita/handlers/kintai.rb', line 38

def register_draft(response, info)
  mail = create_kintai_mail(info)
  reply = response.reply(mail_to_message(mail))
  @@draft = { channel: reply["channel"], ts: reply["ts"], mail: mail }
  reply
end

#scheduleObject



56
57
58
59
60
61
62
63
# File 'lib/lita/handlers/kintai.rb', line 56

def schedule
  return if config.schedule_cron.nil?
  return if config.schedule_room.nil?
  scheduler = Rufus::Scheduler.new
  scheduler.cron config.schedule_cron do
    send_kintai(room: config.schedule_room)
  end
end

#send_kintai(user: user, room: room) ⇒ Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/lita/handlers/kintai.rb', line 65

def send_kintai(user: user, room: room)
  if Gmail.authorized?
    mail = create_kintai_mail(kintai_info)
    reply = send_message(user: user, room: room, message: mail)
    @@draft = { channel: reply["channel"], ts: reply["ts"], mail: mail }
    reply
  else
    send_message(user: user, room: room, message: authenticate_info)
  end
end

#send_mail(mail) ⇒ Object



129
130
131
# File 'lib/lita/handlers/kintai.rb', line 129

def send_mail(mail)
  return Gmail.send_message(mail)
end

#send_message(user: user, room: room, message: message) ⇒ Object



97
98
99
100
# File 'lib/lita/handlers/kintai.rb', line 97

def send_message(user: user, room: room, message: message)
  target = Source.new(user: user, room: room)
  robot.send_message(target, message)
end