diff --git a/dispatcher.go b/dispatcher.go index 6b127cd..8f1807c 100644 --- a/dispatcher.go +++ b/dispatcher.go @@ -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 +} diff --git a/filters.go b/filters.go index 1bfb9ce..70b7eab 100644 --- a/filters.go +++ b/filters.go @@ -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" +} diff --git a/markdown.go b/markdown.go index ebc7da5..6d4af6c 100644 --- a/markdown.go +++ b/markdown.go @@ -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) +} diff --git a/update.go b/update.go index 9a070df..53f9903 100644 --- a/update.go +++ b/update.go @@ -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, + 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