2022-12-21 18:33:00 +08:00
|
|
|
package channel
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
"message-pusher/model"
|
2026-02-11 09:44:32 +08:00
|
|
|
"time"
|
2023-05-12 09:58:51 +08:00
|
|
|
"unicode/utf8"
|
2022-12-21 18:33:00 +08:00
|
|
|
)
|
|
|
|
|
|
2023-05-11 19:57:16 +08:00
|
|
|
var TelegramMaxMessageLength = 4096
|
|
|
|
|
|
2022-12-21 18:33:00 +08:00
|
|
|
type telegramMessageRequest struct {
|
|
|
|
|
ChatId string `json:"chat_id"`
|
|
|
|
|
Text string `json:"text"`
|
|
|
|
|
ParseMode string `json:"parse_mode"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type telegramMessageResponse struct {
|
|
|
|
|
Ok bool `json:"ok"`
|
|
|
|
|
Description string `json:"description"`
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-05 10:43:56 +08:00
|
|
|
func SendTelegramMessage(message *model.Message, user *model.User, channel_ *model.Channel) error {
|
2023-04-16 09:55:23 +08:00
|
|
|
// https://core.telegram.org/bots/api#sendmessage
|
2026-02-11 11:25:42 +08:00
|
|
|
client, err := newHTTPClient(channel_.URL, 30*time.Second)
|
2026-02-11 09:44:32 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2022-12-21 18:33:00 +08:00
|
|
|
messageRequest := telegramMessageRequest{
|
2023-05-08 16:11:42 +08:00
|
|
|
ChatId: channel_.AccountId,
|
2022-12-21 18:33:00 +08:00
|
|
|
}
|
2023-05-06 10:16:55 +08:00
|
|
|
if message.To != "" {
|
|
|
|
|
messageRequest.ChatId = message.To
|
|
|
|
|
}
|
2023-05-08 16:11:42 +08:00
|
|
|
if message.Content == "" {
|
2022-12-21 18:33:00 +08:00
|
|
|
messageRequest.Text = message.Description
|
2023-05-08 16:11:42 +08:00
|
|
|
} else {
|
|
|
|
|
messageRequest.Text = message.Content
|
|
|
|
|
messageRequest.ParseMode = "markdown"
|
2022-12-21 18:33:00 +08:00
|
|
|
}
|
2023-05-11 19:57:16 +08:00
|
|
|
text := messageRequest.Text
|
|
|
|
|
idx := 0
|
|
|
|
|
for idx < len(text) {
|
|
|
|
|
nextIdx := idx + TelegramMaxMessageLength
|
|
|
|
|
if nextIdx > len(text) {
|
2023-05-12 09:58:51 +08:00
|
|
|
// we have reach the end, must be valid
|
2023-05-11 19:57:16 +08:00
|
|
|
nextIdx = len(text)
|
2023-05-12 09:58:51 +08:00
|
|
|
} else {
|
2023-05-20 11:36:28 +08:00
|
|
|
nextIdx = getNearestValidSplit(text, nextIdx, messageRequest.ParseMode)
|
2023-05-11 19:57:16 +08:00
|
|
|
}
|
|
|
|
|
messageRequest.Text = text[idx:nextIdx]
|
|
|
|
|
idx = nextIdx
|
|
|
|
|
jsonData, err := json.Marshal(messageRequest)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2026-02-11 09:44:32 +08:00
|
|
|
resp, err := client.Post(fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage", channel_.Secret), "application/json",
|
2023-05-11 19:57:16 +08:00
|
|
|
bytes.NewBuffer(jsonData))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
var res telegramMessageResponse
|
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&res)
|
2026-02-11 09:44:32 +08:00
|
|
|
_ = resp.Body.Close()
|
2023-05-11 19:57:16 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if !res.Ok {
|
|
|
|
|
return errors.New(res.Description)
|
|
|
|
|
}
|
2022-12-21 18:33:00 +08:00
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2023-05-12 09:58:51 +08:00
|
|
|
|
2023-05-20 11:36:28 +08:00
|
|
|
func getNearestValidSplit(s string, idx int, mode string) int {
|
|
|
|
|
if mode == "markdown" {
|
|
|
|
|
return getMarkdownNearestValidSplit(s, idx)
|
|
|
|
|
} else {
|
|
|
|
|
return getPlainTextNearestValidSplit(s, idx)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getPlainTextNearestValidSplit(s string, idx int) int {
|
2023-05-12 09:58:51 +08:00
|
|
|
if idx >= len(s) {
|
|
|
|
|
return idx
|
|
|
|
|
}
|
|
|
|
|
if idx == 0 {
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
isStartByte := utf8.RuneStart(s[idx])
|
|
|
|
|
if isStartByte {
|
|
|
|
|
return idx
|
|
|
|
|
} else {
|
2023-05-20 11:36:28 +08:00
|
|
|
return getPlainTextNearestValidSplit(s, idx-1)
|
2023-05-12 09:58:51 +08:00
|
|
|
}
|
|
|
|
|
}
|
2023-05-20 11:36:28 +08:00
|
|
|
|
|
|
|
|
func getMarkdownNearestValidSplit(s string, idx int) int {
|
|
|
|
|
if idx >= len(s) {
|
|
|
|
|
return idx
|
|
|
|
|
}
|
|
|
|
|
if idx == 0 {
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
for i := idx; i >= 0; i-- {
|
|
|
|
|
if s[i] == '\n' {
|
|
|
|
|
return i + 1
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// unable to find a '\n'
|
|
|
|
|
return idx
|
|
|
|
|
}
|