message.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package tg
  2. import (
  3. tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
  4. //"strings"
  5. re "regexp"
  6. "fmt"
  7. )
  8. type Message = tgbotapi.Message
  9. // Simple text message component type.
  10. type MessageCompo struct {
  11. Message Message
  12. ParseMode string
  13. Text string
  14. }
  15. var (
  16. escapeRe = re.MustCompile(`([_*\[\]()~`+"`"+`>#+-=|{}.!])`)
  17. NewRawMessage = tgbotapi.NewMessage
  18. )
  19. // Escape special characters in Markdown 2 and return the
  20. // resulting string.
  21. func Escape2(str string) string {
  22. return string(escapeRe.ReplaceAll([]byte(str), []byte("\\$1")))
  23. }
  24. // Call the function after the message was sent.
  25. func (compo *MessageCompo) Update(c Context) error {
  26. edit := tgbotapi.NewEditMessageText(
  27. c.Session.Id.ToApi(),
  28. compo.Message.MessageID,
  29. compo.Text,
  30. )
  31. msg, err := c.bot.api.Send(edit)
  32. if err != nil {
  33. return err
  34. }
  35. compo.Message = msg
  36. return nil
  37. }
  38. func (compo *MessageCompo) Delete(c *Context) {
  39. cfg := tgbotapi.NewDeleteMessage(c.Session.Id.ToApi(), compo.Message.MessageID)
  40. c.Bot.Api.Send(cfg)
  41. //c.Sendf("%q", err)
  42. }
  43. // Is only implemented to make it sendable and so we can put it
  44. // return of rendering functions.
  45. func (compo *MessageCompo) SetMessage(msg Message) {
  46. compo.Message = msg
  47. }
  48. // Return new message with the specified text.
  49. func Messagef(format string, v ...any) MessageCompo {
  50. ret := MessageCompo{}
  51. ret.Text = fmt.Sprintf(format, v...)
  52. ret.ParseMode = tgbotapi.ModeMarkdown
  53. return ret
  54. }
  55. // Return message with the specified parse mode.
  56. func (msg MessageCompo) withParseMode(mode string) MessageCompo {
  57. msg.ParseMode = mode
  58. return msg
  59. }
  60. // Set the default Markdown parsing mode.
  61. func (msg MessageCompo) MD() MessageCompo {
  62. return msg.withParseMode(tgbotapi.ModeMarkdown)
  63. }
  64. // Set the Markdown 2 parsing mode.
  65. func (msg MessageCompo) MD2() MessageCompo {
  66. return msg.withParseMode(tgbotapi.ModeMarkdownV2)
  67. }
  68. // Set the HTML parsing mode.
  69. func (msg MessageCompo) HTML() MessageCompo {
  70. return msg.withParseMode(tgbotapi.ModeHTML)
  71. }
  72. // Transform the message component into one with reply keyboard.
  73. func (msg MessageCompo) Inline(inline Inline) InlineCompo {
  74. return InlineCompo{
  75. Inline: inline,
  76. MessageCompo: msg,
  77. }
  78. }
  79. // Transform the message component into one with reply keyboard.
  80. func (msg MessageCompo) Reply(reply Reply) ReplyCompo {
  81. return ReplyCompo{
  82. Reply: reply,
  83. MessageCompo: msg,
  84. }
  85. }
  86. // Transform the message component into the location one.
  87. func (msg MessageCompo) Location(
  88. lat, long float64,
  89. ) LocationCompo {
  90. ret := &LocationCompo{
  91. MessageCompo: msg,
  92. Location: Location{
  93. Latitude: lat,
  94. Longitude: long,
  95. },
  96. }
  97. return ret
  98. }
  99. // Implementing the Sendable interface.
  100. func (config MessageCompo) SendConfig(
  101. sid SessionId, bot *Bot,
  102. ) (SendConfig) {
  103. var (
  104. ret SendConfig
  105. text string
  106. )
  107. if config.Text == "" {
  108. text = ">"
  109. } else {
  110. text = config.Text
  111. }
  112. msg := tgbotapi.NewMessage(sid.ToApi(), text)
  113. msg.ParseMode = config.ParseMode
  114. ret.Chattable = msg
  115. return ret
  116. }
  117. // Empty serving to use messages in rendering.
  118. func (compo *MessageCompo) Serve(c Context) {}
  119. // Filter that skips everything. Messages cannot do anything with updates.
  120. func (compo *MessageCompo) Filter(_ Update) bool {
  121. // Skip everything
  122. return true
  123. }