-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.rb
48 lines (40 loc) · 1.37 KB
/
app.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 'json'
require 'yaml'
CONFIG = YAML::load(open('config.yml'))
def convert_traffic_output(arg)
if ( arg / 1048576) >= 1
"#{"%.2f" % (arg / 1048576.0)} GiB"
elsif ( arg / 1024) >= 1
"#{"%.2f" % (arg / 1024.0)} MiB"
else
"#{"%.2f" % (arg)} KiB"
end
end
def format_table(stat)
"""in: `#{convert_traffic_output stat["tx"]}` | out: `#{convert_traffic_output stat["rx"]}`
total: `#{convert_traffic_output stat["tx"] + stat["rx"]}`"""
end
def get_stats
vnstat = JSON.parse(`vnstat --json`)
total = vnstat["interfaces"][CONFIG["adapter"]]["traffic"]["total"]
this_month = vnstat["interfaces"][CONFIG["adapter"]]["traffic"]["months"][0]
today = vnstat["interfaces"][CONFIG["adapter"]]["traffic"]["days"][0]
"""__total__
#{format_table total}
__this month:__ #{this_month["date"]["year"]}-#{this_month["date"]["month"]}
#{format_table this_month}
__today:__ #{today["date"]["year"]}-#{today["date"]["month"]}-#{today["date"]["day"]}
#{format_table today}"""
end
Telegram::Bot::Client.run(CONFIG["token"]) do |bot|
bot.listen do |message|
case message.text
when '/start'
kb = Telegram::Bot::Types::ReplyKeyboardMarkup.new(keyboard: ["check"])
bot.api.send_message(chat_id: message.chat.id, text: "Hi human!", reply_markup: kb)
when 'check'
bot.api.send_message(chat_id: message.chat.id, text: get_stats, parse_mode: "Markdown")
end
end
end