Filters, ChainRun, UserMention
This commit is contained in:
		@ -66,3 +66,12 @@ func (d *Dispatcher) HandleReply(handler UpdateHandler) *Dispatcher {
 | 
				
			|||||||
	d.handlers = append(d.handlers, newHandler)
 | 
						d.handlers = append(d.handlers, newHandler)
 | 
				
			||||||
	return d
 | 
						return d
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func ChainRun(u *Update, disps ...*Dispatcher) error {
 | 
				
			||||||
 | 
						for _, d := range disps {
 | 
				
			||||||
 | 
							if err := d.Run(u); err != nil {
 | 
				
			||||||
 | 
								return err
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										23
									
								
								filters.go
									
									
									
									
									
								
							
							
						
						
									
										23
									
								
								filters.go
									
									
									
									
									
								
							@ -39,3 +39,26 @@ func TextHasAny(subs ...string) FilterFn {
 | 
				
			|||||||
		return false
 | 
							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"
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -1,8 +1,11 @@
 | 
				
			|||||||
package nechotron
 | 
					package nechotron
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
	"regexp"
 | 
						"regexp"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"github.com/NicoNex/echotron/v3"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var chars = []string{"_", "*", "\\[", "\\]", "\\(", "\\)", "~", "`", ">", "#", "+", "-", "=", "|", "{", "}", ".", "!"}
 | 
					var chars = []string{"_", "*", "\\[", "\\]", "\\(", "\\)", "~", "`", ">", "#", "+", "-", "=", "|", "{", "}", ".", "!"}
 | 
				
			||||||
@ -12,3 +15,7 @@ var reg = regexp.MustCompile("[" + r + "]+")
 | 
				
			|||||||
func EscapeMd2(s string) string {
 | 
					func EscapeMd2(s string) string {
 | 
				
			||||||
	return reg.ReplaceAllString(s, "\\$0")
 | 
						return reg.ReplaceAllString(s, "\\$0")
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func UserMention(u *echotron.User) string {
 | 
				
			||||||
 | 
						return fmt.Sprintf("[%s](tg://user?id=%d)", EscapeMd2(u.FirstName), u.ID)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										49
									
								
								update.go
									
									
									
									
									
								
							
							
						
						
									
										49
									
								
								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)
 | 
						log.Fatalf("[%s] Can't get ChatID of update %v+", u.UpdateID, u.U)
 | 
				
			||||||
	return 0
 | 
						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) {
 | 
					func (u *Update) AnswerText(text string, options *echotron.MessageOptions) (echotron.APIResponseMessage, error) {
 | 
				
			||||||
	return u.Bot.SendMessage(text, u.ChatID(), options)
 | 
						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) {
 | 
					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) {
 | 
					func (u *Update) AnswerMarkdown(text string) (echotron.APIResponseMessage, error) {
 | 
				
			||||||
	return u.Bot.SendMessage(text, u.ChatID(), &echotron.MessageOptions{
 | 
						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 {
 | 
					func (u *Update) IsCallback() bool {
 | 
				
			||||||
	return u.CallbackQuery != nil
 | 
						return u.CallbackQuery != nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					func (u *Update) IsReply() bool {
 | 
				
			||||||
 | 
						return u.IsMessage() && u.Message.ReplyToMessage != nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
func (u *Update) IsButton(b *Button) bool {
 | 
					func (u *Update) IsButton(b *Button) bool {
 | 
				
			||||||
	if u.IsText() && u.Text() == b.text {
 | 
						if u.IsText() && u.Text() == b.text {
 | 
				
			||||||
		return true
 | 
							return true
 | 
				
			||||||
@ -192,6 +210,16 @@ func (u *Update) From() *echotron.User {
 | 
				
			|||||||
	return u.CallbackQuery.From
 | 
						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 {
 | 
					func (u *Update) Text() string {
 | 
				
			||||||
	if u.IsText() {
 | 
						if u.IsText() {
 | 
				
			||||||
		return u.Message.Text
 | 
							return u.Message.Text
 | 
				
			||||||
@ -209,25 +237,6 @@ func (u *Update) Entities() []*echotron.MessageEntity {
 | 
				
			|||||||
	return u.Message.CaptionEntities
 | 
						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) {
 | 
					func (u *Update) LogError(text string, e error, send bool) {
 | 
				
			||||||
	if text != "" {
 | 
						if text != "" {
 | 
				
			||||||
		text = "Ошибка: " + text
 | 
							text = "Ошибка: " + text
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user