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
|
# File 'lib/mochizuki/telegram_bot.rb', line 12
def start
Telegram::Bot::Client.run(@config.bot_token) do |bot|
Mochizuki::AutoQuery.new.alarm do |power|
bot.api.send_message(chat_id: @config.channel,
text: "Dorm: #{@config.dorm}: #{power} kWh, " \
"lower than threshold #{@config.alarm_threshold} kWh")
end
@logger.info 'Auto query enabled'
bot.listen do |msg|
@logger.info "ID: #{msg.chat.id} uses #{msg}"
case msg.text
when '/start'
send_msg = 'I am a telegram bot written in ruby, '\
'details at https://github.com/DarkKowalski/mochizuki'
bot.api.send_message(chat_id: msg.chat.id,
text: send_msg)
when '/query'
dorm = @config.dorm
begin
power = Mochizuki::Fetcher.new.fetch
send_msg = "Dorm #{dorm}: #{power} kWh"
rescue StandardError
send_msg = 'Failed to query.'
end
bot.api.send_message(chat_id: msg.chat.id, text: send_msg)
when '/status'
send_msg = "Bot status: auto alarm is suppressed -> #{Mochizuki.status.alarmed_before}"
bot.api.send_message(chat_id: msg.chat.id, text: send_msg)
else
@logger.warn "Unknown command #{msg}"
bot.api.send_message(chat_id: msg.chat.id, text: "Use '/query' to get remaining power")
end
end
end
end
|