Filters, ChainRun, UserMention

This commit is contained in:
nefrace 2023-01-24 23:16:58 +03:00
parent c0143707e9
commit 1377e03b5e
4 changed files with 69 additions and 21 deletions

View File

@ -66,3 +66,12 @@ func (d *Dispatcher) HandleReply(handler UpdateHandler) *Dispatcher {
d.handlers = append(d.handlers, newHandler)
return d
}
func ChainRun(u *Update, disps ...*Dispatcher) error {
for _, d := range disps {
if err := d.Run(u); err != nil {
return err
}
}
return nil
}

View File

@ -39,3 +39,26 @@ func TextHasAny(subs ...string) FilterFn {
return false
}
}
func IsPrivate(u *Update) bool {
return u.ChatID() > 0
}
func IsForum(u *Update) bool {
return u.Chat().IsForum
}
func IsReply(u *Update) bool {
return u.IsReply()
}
func IsUserAdmin(u *Update) bool {
if IsPrivate(u) {
return true
}
member, err := u.Bot.GetChatMember(u.ChatID(), u.From().ID)
if err != nil {
return false
}
return member.Result.Status == "administrator" || member.Result.Status == "creator"
}

View File

@ -1,8 +1,11 @@
package nechotron
import (
"fmt"
"regexp"
"strings"
"github.com/NicoNex/echotron/v3"
)
var chars = []string{"_", "*", "\\[", "\\]", "\\(", "\\)", "~", "`", ">", "#", "+", "-", "=", "|", "{", "}", ".", "!"}
@ -12,3 +15,7 @@ var reg = regexp.MustCompile("[" + r + "]+")
func EscapeMd2(s string) string {
return reg.ReplaceAllString(s, "\\$0")
}
func UserMention(u *echotron.User) string {
return fmt.Sprintf("[%s](tg://user?id=%d)", EscapeMd2(u.FirstName), u.ID)
}

View File

@ -73,6 +73,18 @@ func (u *Update) MessageID() int {
log.Fatalf("[%s] Can't get ChatID of update %v+", u.UpdateID, u.U)
return 0
}
func (u *Update) ThreadID() int64 {
if !u.Chat().IsForum {
return 0
}
if u.IsMessage() {
return int64(u.Message.ThreadID)
}
if u.IsCallback() {
return int64(u.CallbackQuery.Message.ThreadID)
}
return 0
}
func (u *Update) AnswerText(text string, options *echotron.MessageOptions) (echotron.APIResponseMessage, error) {
return u.Bot.SendMessage(text, u.ChatID(), options)
@ -83,12 +95,15 @@ func (u *Update) EditText(text string, options *echotron.MessageTextOptions) (ec
}
func (u *Update) AnswerPlain(text string) (echotron.APIResponseMessage, error) {
return u.Bot.SendMessage(text, u.ChatID(), &emptyOpts)
return u.Bot.SendMessage(text, u.ChatID(), &echotron.MessageOptions{
MessageThreadID: u.ThreadID(),
})
}
func (u *Update) AnswerMarkdown(text string) (echotron.APIResponseMessage, error) {
return u.Bot.SendMessage(text, u.ChatID(), &echotron.MessageOptions{
ParseMode: echotron.MarkdownV2,
MessageThreadID: u.ThreadID(),
})
}
@ -116,6 +131,9 @@ func (u *Update) IsMessage() bool {
func (u *Update) IsCallback() bool {
return u.CallbackQuery != nil
}
func (u *Update) IsReply() bool {
return u.IsMessage() && u.Message.ReplyToMessage != nil
}
func (u *Update) IsButton(b *Button) bool {
if u.IsText() && u.Text() == b.text {
return true
@ -192,6 +210,16 @@ func (u *Update) From() *echotron.User {
return u.CallbackQuery.From
}
func GetText(u *echotron.Message) string {
if u.Text != "" {
return u.Text
}
if u.Caption != "" {
return u.Caption
}
return ""
}
func (u *Update) Text() string {
if u.IsText() {
return u.Message.Text
@ -209,25 +237,6 @@ func (u *Update) Entities() []*echotron.MessageEntity {
return u.Message.CaptionEntities
}
func IsUserAdmin(u *Update) bool {
if IsPrivate(u) {
return true
}
member, err := u.Bot.GetChatMember(u.ChatID(), u.From().ID)
if err != nil {
return false
}
return member.Result.Status == "administrator" || member.Result.Status == "creator"
}
func IsPrivate(u *Update) bool {
return u.ChatID() > 0
}
func IsForum(u *Update) bool {
return u.Chat().IsForum
}
func (u *Update) LogError(text string, e error, send bool) {
if text != "" {
text = "Ошибка: " + text