nmdc-telegramfrontend/NTFConfig.go

63 lines
1.4 KiB
Go
Raw Permalink Normal View History

2018-06-03 03:27:50 +00:00
package main
import (
"encoding/json"
"io/ioutil"
)
2018-06-03 04:39:45 +00:00
// NTFConfig keeps track of setup properties as well as runtime state.
// FIXME separate compile-time and run-time properties into separate files
2018-06-03 03:27:50 +00:00
type NTFConfig struct {
HubAddr string
HubDescription string
HubIgnoreNicks []string // Nicknames of Hub-Security/bots to exclude (e.g. "PtokaX").
HubNickMinChars int
ContentedURL string
ContentedMaxMB int
BotAPIKey string
GroupChatID int64
2018-06-03 03:27:50 +00:00
// Map of telegram user IDs to NMDC nicks
KnownUsers map[int64]string
2018-06-03 04:39:45 +00:00
// Map of telegram users known to be in the group chat ==> telegram displayname
GroupChatMembers map[int64]string
2018-06-03 06:24:08 +00:00
// Map of telegram users to their direct conversation ID with the bot
DirectMessageChats map[int64]int64
2018-06-03 03:27:50 +00:00
}
func LoadConfig(configFile string) (NTFConfig, error) {
b, err := ioutil.ReadFile(configFile)
if err != nil {
return NTFConfig{}, err
}
ret := NTFConfig{}
err = json.Unmarshal(b, &ret)
if err != nil {
return NTFConfig{}, err
}
2018-06-03 06:24:08 +00:00
if ret.KnownUsers == nil {
ret.KnownUsers = make(map[int64]string)
}
if ret.GroupChatMembers == nil {
ret.GroupChatMembers = make(map[int64]string)
}
if ret.DirectMessageChats == nil {
ret.DirectMessageChats = make(map[int64]int64)
}
2018-06-03 03:27:50 +00:00
return ret, nil
}
func (this *NTFConfig) Save(configFile string) error {
b, err := json.MarshalIndent(this, "", " ")
if err != nil {
return err
}
return ioutil.WriteFile(configFile, b, 0644)
}