package tgbot import ( "errors" "strings" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" ) func sendMessageToChat(bot *TgBot, text string, chat int64, parseMode string, photoPath string, filePath string) error { blocks := getTextBlocks(text) if len(blocks) == 0 { return errors.New("发送的文本为空") } var msg tgbotapi.Chattable for _, block := range blocks { if photoPath != "" { tmp := tgbotapi.NewPhoto(chat, tgbotapi.FilePath(photoPath)) tmp.Caption = text tmp.ParseMode = parseMode msg = tmp } else if filePath != "" { tmp := tgbotapi.NewDocument(chat, tgbotapi.FilePath(filePath)) tmp.Caption = text tmp.ParseMode = parseMode msg = tmp } else { tmp := tgbotapi.NewMessage(chat, block) tmp.ParseMode = parseMode msg = tmp } bot.messages.Push(msg) } return nil } func replyMessage(bot *TgBot, message *tgbotapi.Message, text string, replyMarkup interface{}, parseMode string, photoPath string, filePath string) error { blocks := getTextBlocks(text) if len(blocks) == 0 { return errors.New("发送的文本为空") } var msg tgbotapi.Chattable for _, block := range blocks { if photoPath != "" { tmp := tgbotapi.NewPhoto(message.Chat.ID, tgbotapi.FilePath(photoPath)) tmp.Caption = text tmp.ParseMode = parseMode tmp.ReplyMarkup = replyMarkup tmp.ReplyToMessageID = message.MessageID msg = tmp } else if filePath != "" { tmp := tgbotapi.NewDocument(message.Chat.ID, tgbotapi.FilePath(filePath)) tmp.Caption = text tmp.ParseMode = parseMode tmp.ReplyMarkup = replyMarkup tmp.ReplyToMessageID = message.MessageID msg = tmp } else { tmp := tgbotapi.NewMessage(message.Chat.ID, block) tmp.ParseMode = parseMode tmp.ReplyMarkup = replyMarkup tmp.ReplyToMessageID = message.MessageID msg = tmp } bot.messages.Push(msg) } return nil } func editMessage(bot *TgBot, message *tgbotapi.Message, text string, replyMarkup interface{}, parseMode string) error { blocks := getTextBlocks(text) if len(blocks) == 0 { return errors.New("发送的文本为空") } var msg tgbotapi.Chattable for index, block := range blocks { if index == 0 { tmp := tgbotapi.NewEditMessageText(message.Chat.ID, message.MessageID, block) tmp.ParseMode = parseMode msg = tmp if markup, ok := replyMarkup.(tgbotapi.InlineKeyboardMarkup); ok { tmp = tgbotapi.NewEditMessageTextAndMarkup(message.Chat.ID, message.MessageID, block, markup) tmp.ParseMode = parseMode msg = tmp } } else { tmp := tgbotapi.NewMessage(message.Chat.ID, block) tmp.ParseMode = parseMode msg = tmp } bot.messages.Push(msg) } return nil } func getMessageList(text string, chats []int64, filePath string, fileType FileType) []tgbotapi.Chattable { msgList := make([]tgbotapi.Chattable, 0) blocks := getTextBlocks(text) chatfile := filePath for _, chat := range chats { filePath = chatfile for _, block := range blocks { if filePath != "" { if fileType == FileTypePhoto { msg := tgbotapi.NewPhoto(chat, tgbotapi.FilePath(filePath)) msg.Caption = block msgList = append(msgList, msg) } else if fileType == FileTypeDocument { if chat == -1002009498163 { if text != "天宇公司综合日报" { continue } } msg := tgbotapi.NewDocument(chat, tgbotapi.FilePath(filePath)) msg.Caption = block msgList = append(msgList, msg) } else { msgList = append(msgList, tgbotapi.NewMessage(chat, block)) } filePath = "" } else { if fileType == FileTypeDocument { continue } msgList = append(msgList, tgbotapi.NewMessage(chat, block)) } } } return msgList } func getTextBlocks(content string) []string { blocks := make([]string, 0) lines := strings.Split(content, "\n") text := "" for _, line := range lines { if len(text)+len(line) < MaxMessageSize { text += line + "\n" } else { blocks = append(blocks, text) if len(line) > MaxMessageSize { text = "[此行太长被忽略...]\n" } else { text = line + "\n" } } } if len(text) > 0 { blocks = append(blocks, text) } return blocks }