123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- package tg
- import (
- tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
- //"strings"
- re "regexp"
- "fmt"
- )
- type Message = tgbotapi.Message
- // Simple text message component type.
- type MessageCompo struct {
- Message Message
- ParseMode string
- Text string
- }
- var (
- escapeRe = re.MustCompile(`([_*\[\]()~`+"`"+`>#+-=|{}.!])`)
- NewRawMessage = tgbotapi.NewMessage
- )
- // Escape special characters in Markdown 2 and return the
- // resulting string.
- func Escape2(str string) string {
- return string(escapeRe.ReplaceAll([]byte(str), []byte("\\$1")))
- }
- // Call the function after the message was sent.
- func (compo *MessageCompo) Update(c Context) error {
- edit := tgbotapi.NewEditMessageText(
- c.Session.Id.ToApi(),
- compo.Message.MessageID,
- compo.Text,
- )
- msg, err := c.bot.api.Send(edit)
- if err != nil {
- return err
- }
- compo.Message = msg
- return nil
- }
- func (compo *MessageCompo) Delete(c *Context) {
- cfg := tgbotapi.NewDeleteMessage(c.Session.Id.ToApi(), compo.Message.MessageID)
- c.Bot.Api.Send(cfg)
- //c.Sendf("%q", err)
- }
- // Is only implemented to make it sendable and so we can put it
- // return of rendering functions.
- func (compo *MessageCompo) SetMessage(msg Message) {
- compo.Message = msg
- }
- // Return new message with the specified text.
- func Messagef(format string, v ...any) MessageCompo {
- ret := MessageCompo{}
- ret.Text = fmt.Sprintf(format, v...)
- ret.ParseMode = tgbotapi.ModeMarkdown
- return ret
- }
- // Return message with the specified parse mode.
- func (msg MessageCompo) withParseMode(mode string) MessageCompo {
- msg.ParseMode = mode
- return msg
- }
- // Set the default Markdown parsing mode.
- func (msg MessageCompo) MD() MessageCompo {
- return msg.withParseMode(tgbotapi.ModeMarkdown)
- }
- // Set the Markdown 2 parsing mode.
- func (msg MessageCompo) MD2() MessageCompo {
- return msg.withParseMode(tgbotapi.ModeMarkdownV2)
- }
- // Set the HTML parsing mode.
- func (msg MessageCompo) HTML() MessageCompo {
- return msg.withParseMode(tgbotapi.ModeHTML)
- }
- // 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,
- }
- }
- // 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.
- func (config MessageCompo) SendConfig(
- sid SessionId, bot *Bot,
- ) (SendConfig) {
- var (
- ret SendConfig
- text string
- )
- if config.Text == "" {
- text = ">"
- } else {
- text = config.Text
- }
- msg := tgbotapi.NewMessage(sid.ToApi(), text)
- msg.ParseMode = config.ParseMode
- ret.Chattable = msg
- return ret
- }
- // Empty serving to use messages in rendering.
- func (compo *MessageCompo) Serve(c Context) {}
- // Filter that skips everything. Messages cannot do anything with updates.
- func (compo *MessageCompo) Filter(_ Update) bool {
- // Skip everything
- return true
- }
|