Add parser for users that left channel Add semi-stub (partial) censor functionality Add bot name cut-out in public chat Add sending functionality for public chats
116 lines
2.4 KiB
Go
116 lines
2.4 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"os/signal"
|
|
"path/filepath"
|
|
"syscall"
|
|
|
|
"github.com/go-redis/redis/v8"
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
// init производит некоторую инициализацию перед запуском main().
|
|
func init() {
|
|
log.SetFormatter(&log.TextFormatter{
|
|
DisableQuote: true,
|
|
DisableLevelTruncation: false,
|
|
DisableColors: true,
|
|
FullTimestamp: true,
|
|
TimestampFormat: "2006-01-02 15:04:05",
|
|
})
|
|
|
|
// no panic, no trace
|
|
switch config.Loglevel {
|
|
case "fatal":
|
|
log.SetLevel(log.FatalLevel)
|
|
case "error":
|
|
log.SetLevel(log.ErrorLevel)
|
|
case "warn":
|
|
log.SetLevel(log.WarnLevel)
|
|
case "info":
|
|
log.SetLevel(log.InfoLevel)
|
|
case "debug":
|
|
log.SetLevel(log.DebugLevel)
|
|
default:
|
|
log.SetLevel(log.InfoLevel)
|
|
}
|
|
|
|
var err error
|
|
|
|
executablePath, err := os.Executable()
|
|
|
|
if err != nil {
|
|
log.Errorf("Unable to get current executable path: %s", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
configJSONPath := fmt.Sprintf("%s/data/config.json", filepath.Dir(executablePath))
|
|
|
|
locations := []string{
|
|
"~/.aleesa-telegram-go.json",
|
|
"~/aleesa-telegram-go.json",
|
|
"/etc/aleesa-telegram-go.json",
|
|
configJSONPath,
|
|
}
|
|
|
|
for _, location := range locations {
|
|
config, err = parseConfig(location)
|
|
|
|
if err == nil {
|
|
break
|
|
}
|
|
|
|
log.Errorf("Unable to parse config at %s: %s", location, err)
|
|
}
|
|
|
|
if err != nil {
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
func main() {
|
|
// Main context
|
|
var ctx = context.Background()
|
|
|
|
// Откроем лог и скормим его логгеру.
|
|
if config.Log != "" {
|
|
logfile, err := os.OpenFile(config.Log, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
|
|
|
|
if err != nil {
|
|
log.Fatalf("Unable to open log file %s: %s", config.Log, err)
|
|
}
|
|
|
|
log.SetOutput(logfile)
|
|
}
|
|
|
|
// Инициализируем redis-клиента.
|
|
redisClient = redis.NewClient(&redis.Options{
|
|
Addr: fmt.Sprintf("%s:%d", config.Redis.Server, config.Redis.Port),
|
|
})
|
|
|
|
log.Debugf("Lazy connect() to redis at %s:%d", config.Redis.Server, config.Redis.Port)
|
|
subscriber = redisClient.Subscribe(ctx, config.Redis.MyChannel)
|
|
redisMsgChan := subscriber.Channel()
|
|
|
|
// Самое время поставить траппер сигналов.
|
|
signal.Notify(sigChan,
|
|
syscall.SIGINT,
|
|
syscall.SIGTERM,
|
|
syscall.SIGQUIT)
|
|
|
|
go sigHandler()
|
|
go telega(config)
|
|
|
|
// Обработчик событий от редиски.
|
|
for msg := range redisMsgChan {
|
|
if shutdown {
|
|
continue
|
|
}
|
|
|
|
redisMsgParser(msg.Payload)
|
|
}
|
|
}
|