COde updates

This commit is contained in:
Astra 2026-02-16 17:40:56 +00:00
parent 432ee62807
commit 03c5ec07c1
4 changed files with 125 additions and 3 deletions

View file

@ -14,6 +14,7 @@ type Config struct {
TargetChatId *int64 `yaml:"target_chat_id"` TargetChatId *int64 `yaml:"target_chat_id"`
EntryMessage string `yaml:"entry_message"` EntryMessage string `yaml:"entry_message"`
ApprovalMessage string `yaml:"approval_message"` ApprovalMessage string `yaml:"approval_message"`
SendApprovalMessage bool `yaml:"send_approval_message"`
DeleteRequestAfterDecision bool `yaml:"delete_request_after_decision"` DeleteRequestAfterDecision bool `yaml:"delete_request_after_decision"`
} }
@ -44,6 +45,7 @@ func (c *Config) CreateConfig() error {
TargetChatId: Int64Ptr(0), TargetChatId: Int64Ptr(0),
EntryMessage: "You have requested to join the group, please write a brief message explaining why you want to join.", EntryMessage: "You have requested to join the group, please write a brief message explaining why you want to join.",
ApprovalMessage: "", ApprovalMessage: "",
SendApprovalMessage: false,
DeleteRequestAfterDecision: false, DeleteRequestAfterDecision: false,
} }
@ -52,6 +54,19 @@ func (c *Config) CreateConfig() error {
return err return err
} }
// SaveConfig writes the current Config back to config.yaml, overwriting the file.
func (c *Config) SaveConfig() error {
f, err := os.OpenFile("config.yaml", os.O_WRONLY|os.O_TRUNC, 0644)
if err != nil {
return err
}
defer f.Close()
encoder := yaml.NewEncoder(f)
err = encoder.Encode(c)
return err
}
// StringPtr returns a pointer to the given string. // StringPtr returns a pointer to the given string.
func StringPtr(s string) *string { return &s } func StringPtr(s string) *string { return &s }

104
handlers/admin.go Normal file
View file

@ -0,0 +1,104 @@
package handlers
import (
"fmt"
"log"
"strings"
utils "git.zio.sh/astra/telegram-approval-join/pkg/utils"
api "github.com/OvyFlash/telegram-bot-api"
)
func (bot *Bot) HandleAdminCommands(update *api.Update) {
switch update.Message.Command() {
case "setentrymessage":
if update.Message.CommandArguments() == "" {
utils.SendMessage(bot.API, update.Message.Chat.ID, update.Message.MessageThreadID,
"Usage: /setentrymessage <entry_message>")
return
}
_, e := utils.SendMessage(bot.API, bot.API.Self.ID, 0, update.Message.CommandArguments())
if e != nil {
if strings.HasPrefix(e.Error(), "Bad Request:") {
utils.SendMessage(bot.API, update.Message.Chat.ID, update.Message.MessageThreadID,
fmt.Sprintf("Unable to set entry message: <code>%s</code>", e))
return
}
}
bot.Config.EntryMessage = update.Message.CommandArguments()
if err := bot.Config.SaveConfig(); err != nil {
log.Printf("Failed to save config: %v", err)
}
utils.SendMessage(bot.API, update.Message.Chat.ID, update.Message.MessageThreadID,
"Entry message updated.")
case "setadmintopic":
if update.Message.CommandArguments() == "" {
utils.SendMessage(bot.API, update.Message.Chat.ID, update.Message.MessageThreadID,
"Usage: <code>/setadmintopic &lt;topic_id/0&gt;</code>")
return
}
topicID, errMsg := utils.ParseIntArg(update.Message.CommandArguments())
if errMsg != "" {
utils.SendMessage(bot.API, update.Message.Chat.ID, update.Message.MessageThreadID,
"Invalid topic ID")
return
}
bot.Config.AdminChatTopicId = &topicID
if err := bot.Config.SaveConfig(); err != nil {
log.Printf("Failed to save config: %v", err)
}
utils.SendMessage(bot.API, update.Message.Chat.ID, topicID,
fmt.Sprintf("Topic ID for admin chat <b>%d</b> set to <b>%d</b>\n",
update.Message.Chat.ID, topicID))
case "togglesendapproval":
if bot.Config.ApprovalMessage != "" {
switch bot.Config.SendApprovalMessage {
case true:
bot.Config.SendApprovalMessage = false
case false:
bot.Config.SendApprovalMessage = true
}
if err := bot.Config.SaveConfig(); err != nil {
log.Printf("Failed to save config: %v", err)
}
utils.SendMessage(bot.API, update.Message.Chat.ID, update.Message.MessageThreadID,
fmt.Sprintf("Send approval message: %v", bot.Config.SendApprovalMessage))
} else {
utils.SendMessage(bot.API, update.Message.Chat.ID, update.Message.MessageThreadID,
"Please set an approval message with <code>/setapprovalmessage</code>")
}
case "setapprovalmessage":
if update.Message.CommandArguments() == "" {
utils.SendMessage(bot.API, update.Message.Chat.ID, update.Message.MessageThreadID,
"Usage: <code>/setapprovalmessage &lt;message&gt;</code>")
return
}
bot.Config.ApprovalMessage = update.Message.CommandArguments()
if err := bot.Config.SaveConfig(); err != nil {
log.Printf("Failed to save config: %v", err)
}
utils.SendMessage(bot.API, update.Message.Chat.ID, update.Message.MessageThreadID,
"Approval message set.")
case "info":
targetChatID := "not set"
if *bot.Config.TargetChatId != 0 {
targetChatID = fmt.Sprintf("%d", *bot.Config.TargetChatId)
}
infoMsg := fmt.Sprintf("%s\n%s\n%s\n%s\n%s",
fmt.Sprintf("Admin Chat ID: <b>%d</b>", update.Message.Chat.ID),
fmt.Sprintf("Admin Topic ID: <b>%d</b>", *bot.Config.AdminChatTopicId),
fmt.Sprintf("Target Chat ID: <b>%s</b>", targetChatID),
fmt.Sprintf("Entry Message: %s", utils.EscapeHTML(bot.Config.EntryMessage)),
fmt.Sprintf("Approval Message: %s", bot.Config.ApprovalMessage))
utils.SendMessage(bot.API, update.Message.Chat.ID, update.Message.MessageThreadID, infoMsg)
}
}

View file

@ -91,7 +91,7 @@ func (bot *Bot) handleApproveRequest(query *api.CallbackQuery, user *ExtendedCha
userString, user.From.ID, user.JoinReason, adminUserString, userString, user.From.ID, user.JoinReason, adminUserString,
time.Now().Format("2006-01-02 15:04:05"))) time.Now().Format("2006-01-02 15:04:05")))
if bot.Config.ApprovalMessage != "" { if bot.Config.SendApprovalMessage {
utils.SendMessage(bot.API, user.From.ID, 0, bot.Config.ApprovalMessage) utils.SendMessage(bot.API, user.From.ID, 0, bot.Config.ApprovalMessage)
} }

View file

@ -56,8 +56,11 @@ func main() {
} }
} }
if update.Message.Chat.ID == *b.Config.AdminChatId && update.Message.ReplyToMessage != nil { if update.Message.Chat.ID == *b.Config.AdminChatId {
if update.Message.ReplyToMessage != nil {
b.HandleDeclineReason(&update) b.HandleDeclineReason(&update)
} }
b.HandleAdminCommands(&update)
}
} }
} }