258 lines
7.4 KiB
Go
258 lines
7.4 KiB
Go
package lib
|
|
|
|
import (
|
|
"fmt"
|
|
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
|
"log"
|
|
"math/rand"
|
|
"net/url"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
var (
|
|
bot *tgbotapi.BotAPI
|
|
err error
|
|
)
|
|
var userState = make(map[string]int)
|
|
var linkState = make(map[int]string)
|
|
|
|
func init() {
|
|
// 7632434434:AAG-Ac9RlLTKHnGtOSUe8_SUD8j_nDWz_AU
|
|
// bot, err = tgbotapi.NewBotAPI("7322066653:AAG3qYWH5jzAW8ZtdFEWg0Fjy0Il5FLOsg8")
|
|
bot, err = tgbotapi.NewBotAPI("7455883553:AAEwoqr6EAUZehhpJUWkhPiu4r3W-fngelE")
|
|
if err != nil {
|
|
log.Panic(err)
|
|
}
|
|
bot.Debug = true
|
|
log.Printf("Authorized on account %s", bot.Self.UserName)
|
|
//db.Test()
|
|
}
|
|
|
|
func number1() int {
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
// 生成一个0到100之间的随机整数
|
|
randomNumber := rand.Intn(100) // 生成一个 [0, 100) 范围内的随机数
|
|
return randomNumber
|
|
}
|
|
func number2() int {
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
// 生成一个0到100之间的随机整数
|
|
randomNumber := rand.Intn(10) // 生成一个 [0, 10) 范围内的随机数
|
|
return randomNumber
|
|
}
|
|
|
|
func Start() {
|
|
u := tgbotapi.NewUpdate(0)
|
|
u.Timeout = 60
|
|
updates := bot.GetUpdatesChan(u)
|
|
|
|
for update := range updates {
|
|
if update.Message != nil { // If we got a message7227826365
|
|
//if update.Message
|
|
if update.Message.Chat.ID == 6918349523 || update.Message.Chat.ID == 7227826365 {
|
|
//if update.Message.Chat.ID == 6918349523 {
|
|
chatID := update.Message.Chat.ID
|
|
userInput := update.Message.Text
|
|
chatIDString := strconv.FormatInt(chatID, 10)
|
|
|
|
// 定义底部菜单
|
|
menu := tgbotapi.NewReplyKeyboard(
|
|
tgbotapi.NewKeyboardButtonRow(
|
|
tgbotapi.NewKeyboardButton("链接列表"),
|
|
tgbotapi.NewKeyboardButton("增加链接"),
|
|
),
|
|
tgbotapi.NewKeyboardButtonRow(
|
|
tgbotapi.NewKeyboardButton("删除链接"),
|
|
tgbotapi.NewKeyboardButton("清空链接"),
|
|
),
|
|
)
|
|
|
|
var responseText string
|
|
switch userInput {
|
|
case "链接列表":
|
|
linkList, _ := GetAllLinks()
|
|
if len(linkList) == 0 {
|
|
responseText = "No links available"
|
|
|
|
} else {
|
|
for v, link := range linkList {
|
|
vs := strconv.Itoa(v + 1)
|
|
responseText += "链接" + vs + ":\t" + link + "\n"
|
|
}
|
|
|
|
}
|
|
|
|
case "增加链接":
|
|
responseText = "请输入你要增加的链接(例: https://google.com),如果需要取消,请发送任意数字"
|
|
str := strconv.FormatInt(chatID, 10)
|
|
userState[str] = 60224466
|
|
userState[str+"_a"] = 0
|
|
userState[str+"_b"] = 0
|
|
// 隐藏键盘
|
|
removeKeyboard := tgbotapi.NewRemoveKeyboard(true)
|
|
msg := tgbotapi.NewMessage(update.Message.Chat.ID, responseText)
|
|
msg.ReplyMarkup = removeKeyboard
|
|
if _, err := bot.Send(msg); err != nil {
|
|
log.Println("Error sending message:", err)
|
|
}
|
|
continue
|
|
|
|
case "删除链接":
|
|
|
|
linkList, _ := GetAllLinks()
|
|
if len(linkList) == 0 {
|
|
|
|
responseText = "No links available"
|
|
} else {
|
|
responseText = "请发送你要删除链接的序号,如果需要取消,请发送任意数字\t()\n"
|
|
for v, link := range linkList {
|
|
vs := strconv.Itoa(v)
|
|
responseText += vs + ".\t" + link + "\n"
|
|
linkState[v] = link
|
|
}
|
|
|
|
}
|
|
|
|
str := strconv.FormatInt(chatID, 10)
|
|
userState[str] = 60224467
|
|
userState[str+"_a"] = 0
|
|
userState[str+"_b"] = 0
|
|
|
|
case "清空链接":
|
|
a := number1()
|
|
b := number2()
|
|
msgLink := fmt.Sprintf("为了您的安全输入验证: %d+%d=?", a, b)
|
|
responseText = msgLink
|
|
str := strconv.FormatInt(chatID, 10)
|
|
userState[str] = 60224465
|
|
userState[str+"_a"] = a
|
|
userState[str+"_b"] = b
|
|
|
|
default:
|
|
responseText = "请选择接下来的操作"
|
|
}
|
|
|
|
// 发送响应消息
|
|
msg := tgbotapi.NewMessage(update.Message.Chat.ID, responseText)
|
|
|
|
msg.ReplyMarkup = menu // 保持底部菜单
|
|
if _, err := bot.Send(msg); err != nil {
|
|
log.Println("Error sending message:", err)
|
|
}
|
|
|
|
// 检查用户状态
|
|
if state, exists := userState[chatIDString]; exists && state == 60224465 {
|
|
a := userState[chatIDString+"_a"] // 获取存储的 a 和 b
|
|
b := userState[chatIDString+"_b"]
|
|
|
|
// 验证用户输入
|
|
expectedAnswer := a + b
|
|
userAnswer, err := strconv.Atoi(userInput)
|
|
if err != nil {
|
|
msg := tgbotapi.NewMessage(chatID, "请输入计算结果")
|
|
inlineKeyboard := tgbotapi.NewInlineKeyboardMarkup(
|
|
tgbotapi.NewInlineKeyboardRow(
|
|
tgbotapi.NewInlineKeyboardButtonData("取消清空", "Cancel"),
|
|
),
|
|
)
|
|
msg.ReplyMarkup = inlineKeyboard
|
|
bot.Send(msg)
|
|
continue
|
|
}
|
|
|
|
if userAnswer == expectedAnswer {
|
|
msg := tgbotapi.NewMessage(chatID, "验证通过!开始删除操作...")
|
|
bot.Send(msg)
|
|
msgs := tgbotapi.NewMessage(chatID, ClearRedis())
|
|
bot.Send(msgs)
|
|
delete(userState, chatIDString) // 删除状态
|
|
delete(userState, chatIDString+"_a")
|
|
delete(userState, chatIDString+"_b")
|
|
} else {
|
|
delete(userState, chatIDString) // 删除状态
|
|
delete(userState, chatIDString+"_a")
|
|
delete(userState, chatIDString+"_b")
|
|
}
|
|
} else if state, exists := userState[chatIDString]; exists && state == 60224466 {
|
|
// 增加链接
|
|
if _, err := url.ParseRequestURI(userInput); err != nil {
|
|
delete(userState, chatIDString) // 删除状态
|
|
delete(userState, chatIDString+"_a")
|
|
delete(userState, chatIDString+"_b")
|
|
} else {
|
|
AddLink(userInput)
|
|
msg := tgbotapi.NewMessage(chatID, "链接增加成功")
|
|
bot.Send(msg)
|
|
delete(userState, chatIDString) // 删除状态
|
|
delete(userState, chatIDString+"_a")
|
|
delete(userState, chatIDString+"_b")
|
|
}
|
|
|
|
} else if state, exists := userState[chatIDString]; exists && state == 60224467 {
|
|
//删除链接
|
|
num, err := strconv.Atoi(userInput)
|
|
fmt.Println(len(linkState))
|
|
if err != nil {
|
|
//msg := tgbotapi.NewMessage(chatID, "请输入序号")
|
|
//inlineKeyboard := tgbotapi.NewInlineKeyboardMarkup(
|
|
// tgbotapi.NewInlineKeyboardRow(
|
|
// tgbotapi.NewInlineKeyboardButtonData("取消删除", "Cancel"),
|
|
// ),
|
|
//)
|
|
//msg.ReplyMarkup = inlineKeyboard
|
|
//bot.Send(msg)
|
|
} else {
|
|
if len(linkState) > num && num >= 0 {
|
|
DeleteLink(linkState[num])
|
|
delete(userState, chatIDString) // 删除状态
|
|
delete(userState, chatIDString+"_a")
|
|
delete(userState, chatIDString+"_b")
|
|
for key := range linkState {
|
|
delete(linkState, key)
|
|
}
|
|
msg := tgbotapi.NewMessage(chatID, "删除成功")
|
|
bot.Send(msg)
|
|
|
|
} else {
|
|
delete(userState, chatIDString) // 删除状态
|
|
delete(userState, chatIDString+"_a")
|
|
delete(userState, chatIDString+"_b")
|
|
for key := range linkState {
|
|
delete(linkState, key)
|
|
}
|
|
}
|
|
}
|
|
|
|
} else {
|
|
//Menus(&update, "请选择你要操作的内容")
|
|
}
|
|
|
|
}
|
|
|
|
} else if update.CallbackQuery != nil { // 按钮点击事件
|
|
callback := update.CallbackQuery
|
|
log.Printf("Button clicked: %s", callback.Data)
|
|
switch callback.Data {
|
|
case "Cancel":
|
|
str := strconv.FormatInt(callback.Message.Chat.ID, 10)
|
|
delete(userState, str) // 删除状态
|
|
delete(userState, str+"_a")
|
|
delete(userState, str+"_b")
|
|
if len(linkState) > 0 {
|
|
for key := range linkState {
|
|
delete(linkState, key)
|
|
}
|
|
}
|
|
msg := tgbotapi.NewMessage(update.CallbackQuery.Message.Chat.ID, "已取消")
|
|
if _, err := bot.Send(msg); err != nil {
|
|
log.Println("Failed to send message:", err)
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|