Refactoring codes for more tg message types

This commit is contained in:
2026-03-07 22:16:14 +08:00
parent a396a381b5
commit 912f9fd0ad
26 changed files with 771 additions and 472 deletions

View File

@@ -1,96 +1,100 @@
package utils
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"path/filepath"
"strings"
"time"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"bytes"
"encoding/json"
"fmt"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"io"
"log"
"path/filepath"
"strings"
"time"
)
var JSON_SETTINGS string = filepath.Join( WORKDIR, "settings.json" )
var JSON_SETTINGS string = filepath.Join(WORKDIR, "settings.json")
var settingsTime = time.Unix( 0, 0 )
var settingsTime = time.Unix(0, 0)
type SysSettings struct {
IgnoredChats map[int64] bool `json:"IgnoredChats"`
IgnoredChats map[int64]bool `json:"IgnoredChats"`
}
var Settings = SysSettings{ IgnoredChats: map[int64] bool{} }
var Settings = SysSettings{IgnoredChats: map[int64]bool{}}
func rwSettings() {
buff, err := ChangedStream( JSON_SETTINGS, writeSettings, settingsTime )
if err != nil {
log.Panic( err )
return
}
conf := SysSettings{}
err = json.Unmarshal( buff.Bytes(), &conf )
if err != nil {
log.Panic( err )
return
}
Settings = conf
}
func writeSettings() ( io.Reader, error ) {
b, err := json.Marshal( Settings )
if err != nil {
return nil, err
}
return bytes.NewBuffer( b ), nil
}
func SystemControl( tgMesg *tgbotapi.Message ) ( string, bool ) {
rwSettings()
processed := false
mesg := ""
if tgMesg.Text[0] == '/' {
processed = true
}
chatId := tgMesg.Chat.ID
if Settings.IgnoredChats == nil {
Settings.IgnoredChats = map[int64] bool{}
}
if strings.Contains( tgMesg.Text, "/golifehk disable" ) {
mesg = fmt.Sprintf( "OK" )
Settings.IgnoredChats[ chatId ] = true
processed = true
}
if strings.Contains( tgMesg.Text, "/golifehk enable" ) {
mesg = fmt.Sprintf( "OK" )
Settings.IgnoredChats[ chatId ] = false
processed = true
}
if processed {
settingsTime = time.Now()
rwSettings()
}
//// Begin processing settings
// ignore chats if enabled
if ignore, ok := Settings.IgnoredChats[ chatId ]; ok {
processed = processed || ignore;
} else {
// default ignore
processed = true;
buff, err := ChangedStream(JSON_SETTINGS, writeSettings, settingsTime)
if err != nil {
log.Panic(err)
return
}
return mesg, processed
conf := SysSettings{}
err = json.Unmarshal(buff.Bytes(), &conf)
if err != nil {
log.Panic(err)
return
}
Settings = conf
}
func writeSettings() (io.Reader, error) {
b, err := json.Marshal(Settings)
if err != nil {
return nil, err
}
return bytes.NewBuffer(b), nil
}
func SystemControl(tgMesg *tgbotapi.Message) (string, bool) {
rwSettings()
processed := false
mesg := ""
if tgMesg.Text == "" {
return "", false
}
if tgMesg.Text[0] == '/' {
processed = true
}
chatId := tgMesg.Chat.ID
if Settings.IgnoredChats == nil {
Settings.IgnoredChats = map[int64]bool{}
}
if strings.Contains(tgMesg.Text, "/golifehk disable") {
mesg = fmt.Sprintf("OK")
Settings.IgnoredChats[chatId] = true
processed = true
}
if strings.Contains(tgMesg.Text, "/golifehk enable") {
mesg = fmt.Sprintf("OK")
Settings.IgnoredChats[chatId] = false
processed = true
}
if processed {
settingsTime = time.Now()
rwSettings()
}
//// Begin processing settings
// ignore chats if enabled
if ignore, ok := Settings.IgnoredChats[chatId]; ok {
processed = processed || ignore
} else {
// default ignore
processed = true
}
return mesg, processed
}