forked from TeamScheire/MiFlo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtelegram_bot.rb
48 lines (40 loc) · 1.15 KB
/
telegram_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
require 'telegram/bot'
require 'pp'
require_relative 'miflo'
require 'optparse'
options = OpenStruct.new
OptionParser.new do |opts|
opts.on("-t", "--bot-token TOKEN", "Telegram bot token") do |v|
options.token = v
end
opts.on("-h", "--mqtt-host HOST", "MQTT host") do |v|
options.mqtt_host = v
end
opts.on("-p", "--person PERSON", "MiFlo user") do |v|
options.person = v
end
opts.on_tail("-h", "--help", "Show this message") do
puts opts
exit
end
end.parse!
mqtt_connect( options.mqtt_host )
Telegram::Bot::Client.run(options.token) do |bot|
bot.listen do |message|
case message
when Telegram::Bot::Types::Message
case message.text
when /^#{options.person} (.+)/
json, bot_message = parse_event( $1 )
if json != ""
send_mqtt( "/iot/miflo/" + options.person + "/timer", json )
end
if bot != ""
bot.api.send_message(chat_id: message.chat.id, text: bot_message)
end
else
bot.api.send_message(chat_id: message.chat.id, text: "Die naam ken ik niet ... Gebruik bijvoorbeeld 'minne timer 15'")
end
end
end
end