This commit is contained in:
wood chen 2024-09-18 02:10:42 +08:00
parent d0af4542f7
commit 800a60c659
3 changed files with 31 additions and 7 deletions

View File

@ -7,7 +7,7 @@ import (
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
func RegisterCommands(bot *tgbotapi.BotAPI, adminID int64) error {
func RegisterCommands(bot *tgbotapi.BotAPI) error {
commands := []tgbotapi.BotCommand{
{Command: "add", Description: "添加新的关键词"},
{Command: "delete", Description: "删除现有的关键词"},
@ -18,10 +18,11 @@ func RegisterCommands(bot *tgbotapi.BotAPI, adminID int64) error {
{Command: "listwhite", Description: "列出白名单域名"},
}
scope := tgbotapi.NewBotCommandScopeChatAdministrators(adminID)
config := tgbotapi.NewSetMyCommands(commands...)
config.Scope = &scope // 注意这里使用 &scope 来获取指针
// 不设置 Scope这将使命令对所有用户可见
// config.Scope = nil
config.LanguageCode = "" // 空字符串表示默认语言
_, err := bot.Request(config)

View File

@ -26,7 +26,12 @@ func init() {
var err error
botToken = os.Getenv("BOT_TOKEN")
chatID = mustParseInt64(os.Getenv("CHAT_ID"))
symbols = strings.Split(os.Getenv("SYMBOLS"), ",")
symbolsRaw := strings.Split(os.Getenv("SYMBOLS"), ",")
symbols = make([]string, len(symbolsRaw))
for i, s := range symbolsRaw {
symbols[i] = strings.ReplaceAll(s, "/", "")
}
// 初始化 singaporeTZ
singaporeTZ = time.FixedZone("Asia/Singapore", 8*60*60) // UTC+8

View File

@ -106,7 +106,25 @@ func messageHandler(bot *tgbotapi.BotAPI, update tgbotapi.Update, linkFilter *co
return
}
if update.Message.Chat.Type != "private" || update.Message.From.ID != adminID {
// 检查是否是管理员发送的私聊消息
if update.Message.Chat.Type == "private" && update.Message.From.ID == adminID {
command := update.Message.Command()
args := update.Message.CommandArguments()
switch command {
case "add", "delete", "list", "deletecontaining":
linkFilter.HandleKeywordCommand(bot, update.Message, command, args)
case "addwhite", "delwhite", "listwhite":
linkFilter.HandleWhitelistCommand(bot, update.Message, command, args)
default:
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "未知命令")
bot.Send(msg)
}
return
}
// 处理非管理员消息或群组消息
if update.Message.Chat.Type != "private" {
if rateLimiter.Allow() {
processMessage(bot, update.Message, linkFilter)
}
@ -145,7 +163,7 @@ func StartBot() error {
log.Printf("Authorized on account %s", bot.Self.UserName)
err = core.RegisterCommands(bot, 0)
err = core.RegisterCommands(bot)
if err != nil {
return fmt.Errorf("error registering commands: %w", err)
}