This repository has been archived by the owner on Apr 13, 2018. It is now read-only.
/
ranssibot.go
executable file
·135 lines (112 loc) · 3.14 KB
/
ranssibot.go
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
package main
import (
"fmt"
flag "github.com/ogier/pflag"
"github.com/tucnak/telebot"
"io/ioutil"
log "maunium.net/go/maulogger"
"maunium.net/go/ranssibot/config"
"maunium.net/go/ranssibot/lang"
"maunium.net/go/ranssibot/laundry"
"maunium.net/go/ranssibot/posts"
"maunium.net/go/ranssibot/timetables"
"maunium.net/go/ranssibot/util"
"os"
"os/signal"
"strings"
"syscall"
"time"
)
// VersionLong is the human-readable form of the version.
const VersionLong = "0.1 Beta 5"
// Version is the computer-readable form of the version.
const Version = "0.1.0-B5"
const (
onoffspamSetting = "onoff-notifications"
onoffValueDebugOnly = "debug-only"
onoffValueNormalOnly = "normal-only"
onoffValueBoth = "true"
)
var startedAt time.Time
var hostname string
var token = flag.StringP("token", "t", "", "The Telegram bot token to use.")
var debug = flag.BoolP("debug", "d", false, "Enable debug mode")
var disableSafeShutdown = flag.Bool("no-safe-shutdown", false, "Disable Interrupt/SIGTERM catching and handling.")
var bot *telebot.Bot
func init() {
flag.Parse()
log.PrintDebug = *debug
log.Fileformat = func(now string, i int) string { return fmt.Sprintf("logs/%[1]s-%02[2]d.log", now, i) }
log.Init()
lang.Load()
config.IndentConfig = *debug
config.Load()
if !*disableSafeShutdown {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
Shutdown("Interrupt/SIGTERM")
}()
}
data, err := ioutil.ReadFile("/etc/hostname")
if err != nil {
log.Fatalln("Failed to read hostname: %s", err)
return
}
hostname = strings.TrimSpace(string(data))
}
func main() {
start := util.TimestampMS()
// Connect to Telegram
var err error
bot, err = telebot.NewBot(*token)
if err != nil {
log.Fatalf("Error connecting to Telegram: %[1]s", err)
return
}
messages := make(chan telebot.Message)
// Enable message listener
bot.Listen(messages, 1*time.Second)
// Print "connected" message
log.Infof("Successfully connected to Telegram!")
// Update timetables
timetables.Update()
go posts.Loop(bot, *debug)
go laundry.Loop(bot)
go listen(bot)
startedAt = time.Now()
var startup = "Ranssibot started up"
if *debug {
startup = fmt.Sprintf("Ranssibot started up in %[1]dms @ %[2]s (Debug mode)", util.TimestampMS()-start, startedAt.Format("15:04:05 02.01.2006"))
}
log.Infof(startup)
onoffspam(startup)
// Listen to messages
for message := range messages {
go handleCommand(bot, message)
}
}
// Shutdown shuts down the Ranssibot.
func Shutdown(by string) {
log.Infof("Ranssibot cleaning up and exiting...")
config.Save()
log.Shutdown()
var shutdown = "Ranssibot shut down"
if *debug {
shutdown = fmt.Sprintf("Ranssibot shut down by %[2]s @ %[1]s", time.Now().Format("15:04:05 02.01.2006"), by)
}
log.Infof(shutdown)
onoffspam(shutdown)
os.Exit(0)
}
func onoffspam(msg string) {
sendMsg := func(user config.User) {
bot.SendMessage(user, msg, util.Markdown)
}
if *debug {
config.GetUsersWithSettingAndRun(sendMsg, onoffspamSetting, onoffValueDebugOnly, onoffValueBoth)
} else {
config.GetUsersWithSettingAndRun(sendMsg, onoffspamSetting, onoffValueNormalOnly, onoffValueBoth)
}
}