2023-09-08 17:37:32 +03:00
|
|
|
package tg
|
|
|
|
|
|
|
|
import (
|
|
|
|
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
2023-10-02 21:45:21 +03:00
|
|
|
//"strings"
|
2023-10-05 17:51:24 +03:00
|
|
|
re "regexp"
|
2023-09-08 17:37:32 +03:00
|
|
|
)
|
2023-09-30 09:55:45 +03:00
|
|
|
type Message = tgbotapi.Message
|
2023-09-08 17:37:32 +03:00
|
|
|
|
2023-10-07 13:20:49 +03:00
|
|
|
// Simple text message component type.
|
2023-09-29 13:36:37 +03:00
|
|
|
type MessageCompo struct {
|
|
|
|
Message *Message
|
2023-09-20 22:48:35 +03:00
|
|
|
ParseMode string
|
2023-09-08 17:37:32 +03:00
|
|
|
Text string
|
|
|
|
}
|
|
|
|
|
2023-10-05 17:51:24 +03:00
|
|
|
var (
|
|
|
|
escapeRe = re.MustCompile(`([_*\[\]()~`+"`"+`>#+-=|{}.!])`)
|
2023-10-07 13:20:49 +03:00
|
|
|
NewRawMessage = tgbotapi.NewMessage
|
2023-10-05 17:51:24 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Escape special characters in Markdown 2 and return the
|
|
|
|
// resulting string.
|
|
|
|
func Escape2(str string) string {
|
|
|
|
return string(escapeRe.ReplaceAll([]byte(str), []byte("\\$1")))
|
|
|
|
}
|
|
|
|
|
2023-10-13 23:27:20 +03:00
|
|
|
func (compo *MessageCompo) Update(c *Context) {
|
|
|
|
edit := tgbotapi.NewEditMessageText(
|
|
|
|
c.Session.Id.ToApi(),
|
|
|
|
compo.Message.MessageID,
|
|
|
|
compo.Text,
|
|
|
|
)
|
|
|
|
msg, _ := c.Bot.Api.Send(edit)
|
|
|
|
compo.Message = &msg
|
|
|
|
}
|
|
|
|
|
2023-10-07 13:20:49 +03:00
|
|
|
// Is only implemented to make it sendable and so we can put it
|
|
|
|
// return of rendering functions.
|
2023-09-29 13:36:37 +03:00
|
|
|
func (compo *MessageCompo) SetMessage(msg *Message) {
|
|
|
|
compo.Message = msg
|
|
|
|
}
|
|
|
|
|
2023-09-16 14:34:17 +03:00
|
|
|
// Return new message with the specified text.
|
2023-09-29 13:36:37 +03:00
|
|
|
func NewMessage(text string) *MessageCompo {
|
|
|
|
ret := &MessageCompo{}
|
2023-09-08 17:37:32 +03:00
|
|
|
ret.Text = text
|
2023-09-20 22:48:35 +03:00
|
|
|
ret.ParseMode = tgbotapi.ModeMarkdown
|
2023-09-08 17:37:32 +03:00
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2023-10-07 13:20:49 +03:00
|
|
|
// Return message with the specified parse mode.
|
2023-09-29 13:36:37 +03:00
|
|
|
func (msg *MessageCompo) withParseMode(mode string) *MessageCompo {
|
2023-09-20 22:48:35 +03:00
|
|
|
msg.ParseMode = mode
|
|
|
|
return msg
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the default Markdown parsing mode.
|
2023-09-29 13:36:37 +03:00
|
|
|
func (msg *MessageCompo) MD() *MessageCompo {
|
2023-09-20 22:48:35 +03:00
|
|
|
return msg.withParseMode(tgbotapi.ModeMarkdown)
|
|
|
|
}
|
|
|
|
|
2023-09-29 13:36:37 +03:00
|
|
|
// Set the Markdown 2 parsing mode.
|
|
|
|
func (msg *MessageCompo) MD2() *MessageCompo {
|
2023-09-20 22:48:35 +03:00
|
|
|
return msg.withParseMode(tgbotapi.ModeMarkdownV2)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-09-29 13:36:37 +03:00
|
|
|
// Set the HTML parsing mode.
|
|
|
|
func (msg *MessageCompo) HTML() *MessageCompo {
|
2023-09-20 22:48:35 +03:00
|
|
|
return msg.withParseMode(tgbotapi.ModeHTML)
|
|
|
|
}
|
|
|
|
|
2023-09-29 13:36:37 +03:00
|
|
|
// Transform the message component into one with reply keyboard.
|
|
|
|
func (msg *MessageCompo) Inline(inline *Inline) *InlineCompo {
|
|
|
|
return &InlineCompo{
|
|
|
|
Inline: inline,
|
|
|
|
MessageCompo: msg,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Transform the message component into one with reply keyboard.
|
|
|
|
func (msg *MessageCompo) Reply(reply *Reply) *ReplyCompo {
|
|
|
|
return &ReplyCompo{
|
|
|
|
Reply: reply,
|
|
|
|
MessageCompo: msg,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-11 14:20:25 +03:00
|
|
|
// Transform the message component into the location one.
|
|
|
|
func (msg *MessageCompo) Location(
|
|
|
|
lat, long float64,
|
|
|
|
) *LocationCompo {
|
|
|
|
ret := &LocationCompo{
|
|
|
|
MessageCompo: msg,
|
|
|
|
Location: Location{
|
|
|
|
Latitude: lat,
|
|
|
|
Longitude: long,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implementing the Sendable interface.
|
2023-09-29 13:36:37 +03:00
|
|
|
func (config *MessageCompo) SendConfig(
|
2023-10-10 12:42:05 +03:00
|
|
|
sid SessionId, bot *Bot,
|
2023-09-16 14:34:17 +03:00
|
|
|
) (*SendConfig) {
|
2023-09-29 13:36:37 +03:00
|
|
|
var (
|
|
|
|
ret SendConfig
|
|
|
|
text string
|
|
|
|
)
|
|
|
|
|
|
|
|
if config.Text == "" {
|
|
|
|
text = ">"
|
|
|
|
} else {
|
|
|
|
text = config.Text
|
|
|
|
}
|
|
|
|
|
2023-10-02 21:45:21 +03:00
|
|
|
//text = strings.ReplaceAll(text, "-", "\\-")
|
|
|
|
|
2023-10-10 12:42:05 +03:00
|
|
|
msg := tgbotapi.NewMessage(sid.ToApi(), text)
|
2023-09-08 17:37:32 +03:00
|
|
|
ret.Message = &msg
|
2023-09-20 22:48:35 +03:00
|
|
|
ret.Message.ParseMode = config.ParseMode
|
2023-09-29 13:36:37 +03:00
|
|
|
|
2023-09-16 14:34:17 +03:00
|
|
|
return &ret
|
2023-09-08 17:37:32 +03:00
|
|
|
}
|
2023-09-29 13:36:37 +03:00
|
|
|
|
|
|
|
// Empty serving to use messages in rendering.
|
2023-10-07 13:20:49 +03:00
|
|
|
func (compo *MessageCompo) Serve(c *Context) {}
|
2023-09-29 13:36:37 +03:00
|
|
|
|
2023-10-07 13:20:49 +03:00
|
|
|
// Filter that skips everything. Messages cannot do anything with updates.
|
2023-09-29 13:36:37 +03:00
|
|
|
func (compo *MessageCompo) Filter(_ *Update) bool {
|
|
|
|
// Skip everything
|
|
|
|
return true
|
|
|
|
}
|